-
Notifications
You must be signed in to change notification settings - Fork 3
/
Undirected Graph Representations.py
134 lines (99 loc) · 2.73 KB
/
Undirected Graph Representations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = {}
self.matrix = []
for i in range(self.V):
self.matrix.append([0 for i in range(self.V)])
def add_edge(self, src, dest):
##for adjacency list
if self.graph.get(src) == None:
self.graph[src]=[dest]
else:
self.graph[src]=self.graph.get(src)+[dest]
if self.graph.get(dest) == None:
self.graph[dest]=[src]
else:
self.graph[dest]=self.graph.get(dest)+[src]
##for adjacency matrix
key_list=list(self.graph.keys())
i1 = key_list.index(src)
i2 = key_list.index(dest)
self.matrix[i1][i2] = 1
self.matrix[i2][i1] = 1
def print_adj_list(self):
print("Adjacency List: ")
print()
for i in self.graph:
print("Adjacency list of vertex: {}\n head ".format(i), end="")
for j in self.graph[i]:
print(" -> {}".format(j), end="")
print(' \n')
def print_adj_matrix(self):
print("Adjacency Matrix: ")
print()
for i in self.matrix:
print(i)
V = 5
graph = Graph(V)
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(1, 3)
graph.add_edge(2, 3)
graph.add_edge(2, 4)
graph.add_edge(3, 4)
graph.print_adj_list()
graph.print_adj_matrix()
print()
print("---------------------------")
print()
V = 5
graph = Graph(V)
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'C')
graph.add_edge('B', 'D')
graph.add_edge('C', 'D')
graph.add_edge('C', 'E')
graph.add_edge('D', 'E')
graph.print_adj_list()
graph.print_adj_matrix()
## OUTPUT:
'''
Adjacency List:
Adjacency list of vertex: 0
head -> 1 -> 2
Adjacency list of vertex: 1
head -> 0 -> 2 -> 3
Adjacency list of vertex: 2
head -> 0 -> 1 -> 3 -> 4
Adjacency list of vertex: 3
head -> 1 -> 2 -> 4
Adjacency list of vertex: 4
head -> 2 -> 3
Adjacency Matrix:
[0, 1, 1, 0, 0]
[1, 0, 1, 1, 0]
[1, 1, 0, 1, 1]
[0, 1, 1, 0, 1]
[0, 0, 1, 1, 0]
---------------------------
Adjacency List:
Adjacency list of vertex: A
head -> B -> C
Adjacency list of vertex: B
head -> A -> C -> D
Adjacency list of vertex: C
head -> A -> B -> D -> E
Adjacency list of vertex: D
head -> B -> C -> E
Adjacency list of vertex: E
head -> C -> D
Adjacency Matrix:
[0, 1, 1, 0, 0]
[1, 0, 1, 1, 0]
[1, 1, 0, 1, 1]
[0, 1, 1, 0, 1]
[0, 0, 1, 1, 0]
'''