File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #defining graph vertices and edges
2+ graph = dict ()
3+ graph ['A' ] = ['B' , 'C' ]
4+ graph ['B' ] = ['E' ,'C' , 'A' ]
5+ graph ['C' ] = ['A' , 'B' , 'E' ,'F' ]
6+ graph ['E' ] = ['B' , 'C' ]
7+ graph ['F' ] = ['C' ]
8+
9+ matrix_elements = sorted (graph .keys ())
10+ cols = rows = len (matrix_elements )
11+
12+ #adjacency matrix
13+ adjacency_matrix = [[0 for x in range (rows )] for y in range (cols )]
14+ edges_list = []
15+
16+ for key in matrix_elements :
17+ for neighbour in graph .get (key ):
18+ edges_list .append ((key , neighbour ))
19+
20+
21+ #printing the edges list
22+ print (edges_list )
23+
24+
25+ #Now filling the adjacency matrix
26+ for edge in edges_list :
27+ node1 = matrix_elements .index (edge [0 ])
28+ node2 = matrix_elements .index (edge [1 ])
29+ adjacency_matrix [node1 ][node2 ] = 1
30+
31+ #print adjacency matrix
32+ print (adjacency_matrix )
You can’t perform that action at this time.
0 commit comments