Advertisement
qberik

Untitled

Dec 11th, 2022
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1.  
  2.  
  3. class Graph:
  4.  
  5. def __init__( self, adj ):
  6. self.adj = adj
  7. self.v = len(adj)
  8. self.vlist = []
  9.  
  10. def add_edge( self, a, b ):
  11. self.adj[a][b] = 1
  12. self.adj[b][a] = 1
  13. if a == b:
  14. self.adj[a][b] = 2
  15.  
  16. def BFS( self, start ):
  17. visited = [False] * self.v
  18. q = [start]
  19. visited[start] = True
  20.  
  21. while q:
  22. vis = q[0]
  23. self.vlist.append(vis)
  24. q.pop(0)
  25. for i in range( self.v ):
  26. if( self.adj[vis][i] != 0 and
  27. (not visited[i])):
  28. q.append(i)
  29. visited[i] = True
  30.  
  31.  
  32.  
  33.  
  34. G_1 =[[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
  35. [0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
  36. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  37. [1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
  38. [0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
  39. [0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
  40. [0, 0, 0, 0, 0, 0, 2, 1, 0, 0],
  41. [0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
  42. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
  43. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]]
  44.  
  45.  
  46. dop_G_1 = G_1
  47.  
  48. for i in range( len( G_1 )):
  49. for j in range( len( G_1 )):
  50. e = 1
  51. if i == j:
  52. e = 2
  53. if G_1[i][j] == e:
  54. dop_G_1[i][j] = 0
  55. if G_1[i][j] == 0:
  56. dop_G_1[i][j] = e
  57.  
  58.  
  59.  
  60.  
  61. G_0 = [ [ 0 for _ in range(len(G_1)) ] for _ in range( len(G_1) ) ]
  62.  
  63. a = Graph( dop_G_1 )
  64. b = Graph( G_0 )
  65.  
  66. a.BFS(0)
  67.  
  68. for i in range( len( a.vlist ) - 1 ):
  69. b.add_edge( a.vlist[i], a.vlist[i+1] )
  70.  
  71. print( *b.adj, sep='\n' )
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement