Advertisement
Guest User

Componentes Fuertemente Conexos (CFC)

a guest
Nov 26th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. from random import *
  2.  
  3. def esCiclicoUtil(g, vertice, visitado, pila):
  4. visitado[vertice] = True
  5. pila[vertice] = True
  6. for v,w in g[vertice]:
  7. if not visitado[v]:
  8. if esCiclicoUtil(g, v, visitado, pila):
  9. return True
  10. elif pila[v]:
  11. return True
  12. pila[vertice] = False
  13. return False
  14.  
  15. def esCiclico(g):
  16. visitado = [False] * len(g)
  17. pila = [False] * len(g)
  18. for vertice in range(len(g)):
  19. if not visitado[vertice]:
  20. if esCiclicoUtil(g, vertice, visitado, pila):
  21. return True
  22. return False
  23.  
  24. def generarGrafoAciclicoConPesosL(n,m):
  25. g = [[] for i in range(n)]
  26. for j in range(m):
  27. v1 = randint(0, n - 1)
  28. v2 = randint(0, n - 1)
  29. w = randint(1, 10)
  30. adyacentes = [x[0] for x in g[v1]]
  31. g1 = g.copy()
  32. g1[v1] += [(v2,w)]
  33. while(v2 in adyacentes or v2 == v1 or esCiclico(g1)):
  34. v1 = randint(0, n - 1)
  35. v2 = randint(0, n - 1)
  36. adyacentes = [x[0] for x in g[v1]]
  37. g1 = g.copy()
  38. g1[v1] += [(v2,w)]
  39. g[v1] += [(v2,w)]
  40. return g
  41.  
  42. g = generarGrafoAciclicoConPesosL(10,22)
  43. print(g)
  44.  
  45. def CFCexhaustivo(g):
  46. CFC = [-i for i in range(1,len(g) + 1)]
  47. for vertice in range(len(g)):
  48. CFC[vertice] = vertice
  49. for v,w in g[vertice]:
  50. if CFC[vertice] != CFC[v]:
  51. for z in range(len(g)):
  52. if CFC[z] == CFC[v]:
  53. CFC[z] = CFC[vertice]
  54. print(CFC)
  55.  
  56. CFCexhaustivo(g)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement