Skip to content

Commit e4cc0cb

Browse files
committed
Graphs.py created
1 parent 000dc44 commit e4cc0cb

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

DataStructures/Section8/Graphs.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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)

0 commit comments

Comments
 (0)