Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import *
- def esCiclicoUtil(g, vertice, visitado, pila):
- visitado[vertice] = True
- pila[vertice] = True
- for v,w in g[vertice]:
- if not visitado[v]:
- if esCiclicoUtil(g, v, visitado, pila):
- return True
- elif pila[v]:
- return True
- pila[vertice] = False
- return False
- def esCiclico(g):
- visitado = [False] * len(g)
- pila = [False] * len(g)
- for vertice in range(len(g)):
- if not visitado[vertice]:
- if esCiclicoUtil(g, vertice, visitado, pila):
- return True
- return False
- def generarGrafoAciclicoConPesosL(n,m):
- g = [[] for i in range(n)]
- for j in range(m):
- v1 = randint(0, n - 1)
- v2 = randint(0, n - 1)
- w = randint(1, 10)
- adyacentes = [x[0] for x in g[v1]]
- g1 = g.copy()
- g1[v1] += [(v2,w)]
- while(v2 in adyacentes or v2 == v1 or esCiclico(g1)):
- v1 = randint(0, n - 1)
- v2 = randint(0, n - 1)
- adyacentes = [x[0] for x in g[v1]]
- g1 = g.copy()
- g1[v1] += [(v2,w)]
- g[v1] += [(v2,w)]
- return g
- g = generarGrafoAciclicoConPesosL(10,22)
- print(g)
- def CFCexhaustivo(g):
- CFC = [-i for i in range(1,len(g) + 1)]
- for vertice in range(len(g)):
- CFC[vertice] = vertice
- for v,w in g[vertice]:
- if CFC[vertice] != CFC[v]:
- for z in range(len(g)):
- if CFC[z] == CFC[v]:
- CFC[z] = CFC[vertice]
- print(CFC)
- CFCexhaustivo(g)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement