-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from Grafica import Grafica | ||
from ErdosRenyi import * | ||
import math | ||
|
||
def num_parejas_conectadas(G,u): | ||
resultado = 0 | ||
vecinos, _ = G.vecinos(u) | ||
for v in vecinos: | ||
if v < u: | ||
continue | ||
for w in vecinos: | ||
if w < v: | ||
continue | ||
if G.conectados(v,w): | ||
resultado += 1 | ||
return resultado | ||
|
||
def binomial(n, k): | ||
return math.factorial(n)/(math.factorial(k)*math.factorial(n-k)) | ||
|
||
def num_triangulos(G): | ||
resultado = 0 | ||
for u in G.vertices(): | ||
resultado += num_parejas_conectadas(G,u) | ||
return resultado | ||
|
||
def num_eles(G): | ||
resultado = 0 | ||
for u in G.vertices(): | ||
resultado += binomial(G.grado(u), 2) | ||
return resultado | ||
|
||
|
||
def clustering(G): | ||
return 3*num_triangulos(G)/num_eles(G) | ||
|
||
|
||
def clustering_local(G, v): | ||
num_vecinos_conectados = num_parejas_conectadas(G, v) | ||
return num_vecinos_conectados/binomial(G.grado(v), 2) | ||
|
||
|
||
if __name__ == "__main__": | ||
G = GNP(1000,0.2) | ||
print(clustering(G)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class DisjointSets(): | ||
def __init__(self, n): | ||
self.padres = [-1]*n | ||
|
||
def es_raiz(self, nodo): | ||
return self.padres[nodo] == -1 | ||
|
||
def raiz(self, nodo): | ||
if self.es_raiz(nodo): | ||
return nodo | ||
|
||
r = self.raiz(self.padres[nodo]) | ||
self.padres[nodo] = r | ||
|
||
return r | ||
|
||
def misma_componente(self, a, b): | ||
return self.raiz(a) == self.raiz(b) | ||
|
||
def juntar(self, a, b): | ||
ra = self.raiz(a) | ||
rb = self.raiz(b) | ||
self.padres[ra] = rb | ||
|
||
if __name__ == "__main__": | ||
D = DisjointSets(6) | ||
|
||
print("3 es raiz?", D.es_raiz(3)) | ||
|
||
D.juntar(0,1) | ||
D.juntar(1,2) | ||
|
||
print("misma_componente 0 y 1?", D.misma_componente(0,1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from Grafica import Grafica | ||
import random | ||
|
||
def GNP(n,p): | ||
G = Grafica(n) | ||
for u in range(n): | ||
for v in range(u+1, n): | ||
r = random.random() | ||
if r < p: | ||
G.conectar(u,v) | ||
return G | ||
|
||
def generar_parejas(n): | ||
parejas = [] | ||
for u in range(n): | ||
for v in range(u+1, n): | ||
parejas.append([u, v]) | ||
return parejas | ||
|
||
def generar_grafica(n, A): | ||
G = Grafica(n) | ||
for u,v in A: | ||
G.conectar(u,v) | ||
return G | ||
|
||
def GNM(n,m): | ||
A = generar_parejas(n) | ||
random.shuffle(A) | ||
return generar_grafica(n, A[:m]) | ||
|
||
if __name__ == "__main__": | ||
G=GNM(20,40) | ||
for a in G.aristas(): | ||
print(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from utils import unzip | ||
|
||
class Grafica(): | ||
def __init__(self, num_verts): | ||
self.num_verts = num_verts | ||
self.lista_vecinos = [[] for i in range(num_verts)] | ||
self.pesos = [[] for i in range(num_verts)] | ||
self.vecinos_ordenados = True | ||
|
||
def conectar(self, x, y, w = 1.): | ||
self.lista_vecinos[x].append(y) | ||
self.lista_vecinos[y].append(x) | ||
self.pesos[x].append(w) | ||
self.pesos[y].append(w) | ||
self.vecinos_ordenados = False | ||
|
||
def vecinos(self, x): | ||
return self.lista_vecinos[x], self.pesos[x] | ||
|
||
def vertices(self): | ||
return range(self.num_verts) | ||
|
||
def grado(self, x): | ||
return len(self.lista_vecinos[x]) | ||
|
||
def conectados(self, x, y): | ||
self.ordenar_vecinos() | ||
return busqueda_binaria(y, self.lista_vecinos[x]) | ||
|
||
def ordenar_vecinos(self): | ||
if self.vecinos_ordenados: | ||
return | ||
for u in self.vertices(): | ||
self.lista_vecinos[u], self.pesos[u] = unzip(sorted(list(zip(*self.vecinos(u))))) | ||
self.vecinos_ordenados = True | ||
|
||
def aristas(self): | ||
A = [] | ||
for u in range(self.num_verts): | ||
for v, w in zip(*self.vecinos(u)): | ||
if u < v: | ||
A.append((w, u, v)) | ||
return A | ||
|
||
if __name__ == "__main__": | ||
G = Grafica(5) | ||
G.conectar(0,1,2) | ||
G.conectar(0,2,3) | ||
G.conectar(1,4,1) | ||
G.conectar(1,2,3) | ||
G.conectar(2,3,4) | ||
G.conectar(3,4,1) | ||
G.ordenar_vecinos() | ||
for i in G.vertices(): | ||
print(G.vecinos(i)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from DisjointSets import DisjointSets | ||
from Grafica import Grafica | ||
from Prim import prim | ||
|
||
def aristas_sin_ciclos(aristas, D): | ||
mst = [] | ||
for w, u, v in aristas: | ||
if D.misma_componente(u, v): | ||
continue | ||
mst.append((u, v, w)) | ||
D.juntar(u, v) | ||
return mst | ||
|
||
def kruskal(G): | ||
aristas = G.aristas() | ||
aristas.sort() | ||
D = DisjointSets(G.num_verts) | ||
return aristas_sin_ciclos(aristas, D) | ||
|
||
|
||
if __name__ == "__main__": | ||
G = Grafica(5) | ||
G.conectar(0,1,2) | ||
G.conectar(0,2,3) | ||
G.conectar(1,4,1) | ||
G.conectar(1,2,3) | ||
G.conectar(2,3,4) | ||
G.conectar(3,4,1) | ||
print("Prim:") | ||
for arista in prim(G): | ||
print(arista) | ||
|
||
print("Kruskal:") | ||
for arista in kruskal(G): | ||
print(arista) | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import random | ||
import heapq | ||
import Grafica | ||
|
||
|
||
def extraer_menor(aristas): | ||
return heapq.heappop(aristas) | ||
|
||
def add_links(G, AE, v, vertices_explorados): | ||
for x, w in zip(*G.vecinos(v)): | ||
if vertices_explorados[x] == 0: | ||
heapq.heappush(AE,(w, v, x)) | ||
|
||
def prim(G): | ||
n = G.num_verts | ||
v = random.randint(0, n-1) | ||
|
||
vertices_explorados = [0]*n | ||
vertices_explorados[v] = 1 | ||
|
||
AE = [(w, v, x) for x, w in zip(*G.vecinos(v))] | ||
heapq.heapify(AE) | ||
arbol = [] | ||
|
||
while AE: | ||
w,u,v = extraer_menor(AE) | ||
if vertices_explorados[v] == 0: | ||
arbol.append((u,v,w)) | ||
vertices_explorados[v] = 1 | ||
add_links(G, AE, v, vertices_explorados) | ||
return arbol | ||
|
||
|
||
# Quiero hacer esto: | ||
|
||
if __name__ == "__main__": | ||
G = Grafica(5) | ||
G.conectar(0,1,2) | ||
G.conectar(0,2,3) | ||
G.conectar(1,4,1) | ||
G.conectar(1,2,3) | ||
G.conectar(2,3,4) | ||
G.conectar(3,4,1) | ||
for arista in prim(G): | ||
print(arista) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
|
||
class SimpleTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
"""Call before every test case.""" | ||
pass | ||
|
||
def tearDown(self): | ||
"""Call after every test case.""" | ||
pass | ||
|
||
def testA(self): | ||
"""Test case A. note that all test method names must begin with 'test.'""" | ||
assert 5+8 == 13, "python no sabe sumar" | ||
|
||
if __name__ == "__main__": | ||
unittest.main() # run all tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def unzip(L): | ||
return [l[0] for l in L],[l[1] for l in L] | ||
|
||
def busqueda_binaria(L, x): | ||
primero = 0 | ||
ultimo = len(L) | ||
while primero+1 < ultimo: | ||
medio = (ultimo + primero)//2 | ||
if x < L[medio]: | ||
ultimo = medio | ||
else: | ||
primero = medio | ||
return L[primero] == x | ||
|
||
if __name__ == "__main__": | ||
print(busqueda_binaria([1,1,2,2,2,3,3,4], 3)) | ||
print(busqueda_binaria([1,1,2,2,2,3,3,4], 4)) | ||
print(busqueda_binaria([1,1,2,2,2,4,4,4], 3)) | ||
print(busqueda_binaria([1,1,2,2,2,3,3,4], 8)) | ||
|