8
8
9
9
class Graph :
10
10
def __init__ (self , vertices , graph_type = 'directed' , weighted = False ):
11
+ '''
12
+ Initialises Adjacent Matrix with Zeros and other class variables.
13
+
14
+ Parameters:
15
+ vertices -- number of vertices
16
+ graph_type -- 'directed' or 'undirected'
17
+ weighted -- True or False
18
+ '''
11
19
self ._vertices = vertices
12
20
self ._type = graph_type
13
21
self ._weighted = weighted
@@ -16,22 +24,39 @@ def __init__(self, vertices, graph_type='directed', weighted = False):
16
24
self ._visited = [False ] * self ._vertices
17
25
18
26
def insert_edge (self , u , v , weight = 1 ):
27
+ '''
28
+ Adds an edge between the passed vertices (u,v) by allocating the weight at that index position.
29
+ If the graph is 'undirected', then same weights are also copied to (v,u).
30
+ '''
19
31
self ._adjMAT [u ][v ] = weight
20
32
if self ._type == 'undirected' :
21
33
self ._adjMAT [v ][u ] = weight
22
34
23
35
def remove_edge (self , u , v ):
36
+ '''
37
+ Removes an edge between the passed vertices (u,v) by making that index position as 0.
38
+ If the graph is 'undirected', then 0 is also copied to (v,u).
39
+ '''
24
40
self ._adjMAT [u ][v ] = 0
25
41
if self ._type == 'undirected' :
26
42
self ._adjMAT [v ][u ] = 0
27
43
28
44
def exist_edge (self , u , v ):
45
+ '''
46
+ Returns True if edge exists between vertices (u,v), else False.
47
+ '''
29
48
return self ._adjMAT [u ][v ] != 0
30
49
31
50
def vertex_count (self ):
51
+ '''
52
+ Returns number of vertices present in the Graph.
53
+ '''
32
54
return self ._vertices
33
55
34
56
def edge_count (self ):
57
+ '''
58
+ Returns number of edges present in the Graph.
59
+ '''
35
60
count = 0
36
61
for i in range (self ._vertices ):
37
62
for j in range (self ._vertices ):
@@ -40,11 +65,17 @@ def edge_count(self):
40
65
return count
41
66
42
67
def vertices (self ):
68
+ '''
69
+ Prints all the vertices.
70
+ '''
43
71
for i in range (self ._vertices ):
44
72
print (i , end = ' ' )
45
73
print ()
46
74
47
75
def edges (self ):
76
+ '''
77
+ Prints all the edges with weights if the graph is undirected.
78
+ '''
48
79
for i in range (self ._vertices ):
49
80
for j in range (self ._vertices ):
50
81
if self ._adjMAT [i ][j ] != 0 and self ._weighted == True :
@@ -53,21 +84,30 @@ def edges(self):
53
84
print (f'{ i } -- { j } ' )
54
85
55
86
def outdegree (self , v ):
87
+ '''
88
+ Returns the outdegree of the passed vertex v.
89
+ '''
56
90
count = 0
57
91
for j in range (self ._vertices ):
58
92
if self ._adjMAT [v ][j ] != 0 :
59
93
count += 1
60
94
return count
61
95
62
96
def indegree (self , v ):
97
+ '''
98
+ Returns the indegree of the passed vertex v.
99
+ '''
63
100
count = 0
64
101
for i in range (self ._vertices ):
65
102
if self ._adjMAT [i ][v ] != 0 :
66
103
count += 1
67
104
return count
68
105
69
- def BFS (self , start_vertext ):
70
- i = start_vertext
106
+ def BFS (self , start_vertex ):
107
+ '''
108
+ Breadth First Search
109
+ '''
110
+ i = start_vertex
71
111
q = Queue ()
72
112
visited = [False ] * self ._vertices
73
113
print (i , end = ' ' )
@@ -83,6 +123,9 @@ def BFS(self, start_vertext):
83
123
q .enqueue (j )
84
124
85
125
def DFS_iterative (self , start_vertex ):
126
+ '''
127
+ Depth First Search using Stack.
128
+ '''
86
129
i = start_vertex
87
130
s = Stack ()
88
131
visited = [False ] * self ._vertices
@@ -98,6 +141,9 @@ def DFS_iterative(self, start_vertex):
98
141
s .push (j )
99
142
100
143
def DFS_recursive (self , start_vertex ):
144
+ '''
145
+ Depth First Search using Recursion.
146
+ '''
101
147
if self ._visited [start_vertex ] == False :
102
148
print (start_vertex , end = ' ' )
103
149
self ._visited [start_vertex ] = True
@@ -107,8 +153,10 @@ def DFS_recursive(self, start_vertex):
107
153
self .DFS_recursive (j )
108
154
109
155
def display (self ):
156
+ '''
157
+ Displays Adjacency Matrix.
158
+ '''
110
159
n_edges = self .edge_count ()
111
-
112
160
if self ._type == 'undirected' :
113
161
n_edges = int (n_edges / 2 )
114
162
@@ -142,9 +190,17 @@ def display(self):
142
190
143
191
g .insert_edge (6 , 3 , 70 )
144
192
193
+ print ("\n Adjacency Matrix" )
145
194
g .display ()
195
+
196
+ print ("\n Edges" )
146
197
g .edges ()
147
- print ("DFS Iterative:" )
198
+
199
+ print ("\n BFS:" )
200
+ g .BFS (0 )
201
+
202
+ print ("\n DFS Iterative:" )
148
203
g .DFS_iterative (0 )
204
+
149
205
print ("\n DFS Iterative:" )
150
206
g .DFS_recursive (0 )
0 commit comments