Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Graph:
- def __init__( self, adj ):
- self.adj = adj
- self.v = len(adj)
- self.vlist = []
- def add_edge( self, a, b ):
- self.adj[a][b] = 1
- self.adj[b][a] = 1
- if a == b:
- self.adj[a][b] = 2
- def DFS( self, start, visited = [] ):
- vis = []
- if ( len(visited) == 0 ):
- for i in range( self.v ):
- vis.append(False)
- else:
- vis = visited
- self.vlist.append(start)
- vis[start] = True
- for i in range( self.v ):
- if( self.adj[start][i] == 1 and
- (not vis[i])):
- self.DFS(i,vis)
- G_1 =[[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
- [0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
- [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
- [1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
- [0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
- [0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
- [0, 0, 0, 0, 0, 0, 2, 1, 0, 0],
- [0, 0, 0, 1, 0, 1, 1, 0, 0, 0],
- [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
- [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]]
- dop_G_1 = G_1
- for i in range( len( G_1 )):
- for j in range( len( G_1 )):
- e = 1
- if i == j:
- e = 2
- if G_1[i][j] == e:
- dop_G_1[i][j] = 0
- if G_1[i][j] == 0:
- dop_G_1[i][j] = e
- G_0 = [ [ 0 for _ in range(len(G_1)) ] for _ in range( len(G_1) ) ]
- a = Graph( dop_G_1 )
- b = Graph( G_0 )
- a.DFS(0)
- for i in range( len( a.vlist ) - 1 ):
- b.add_edge( a.vlist[i], a.vlist[i+1] )
- print( *b.adj, sep='\n' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement