File tree 2 files changed +31
-1
lines changed
2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -100,3 +100,18 @@ def indegree(self, v):
100
100
def display_adjacent_matrix (self ):
101
101
print (self ._adjacent_matrix )
102
102
103
+ def BFS (self , source_vertex ):
104
+ i = source_vertex
105
+ q = LinkedListQueue ()
106
+ visited = [0 ] * self ._vertices
107
+ print (i , end = ' ' )
108
+ visited [i ] = 1
109
+ q .enqueue (i )
110
+ while not q .is_empty ():
111
+ i = q .dequeue ()
112
+ for j in range (self ._vertices ):
113
+ if self ._adjacent_matrix [i ][j ] == 1 and visited [j ] == 0 :
114
+ print (j , end = ' ' )
115
+ visited [j ] = 1
116
+ q .enqueue (j )
117
+
Original file line number Diff line number Diff line change @@ -53,4 +53,19 @@ def indegree(self, v):
53
53
54
54
# Helper function to display adjacency matrix
55
55
def display_adjacent_matrix (self ):
56
- print (self ._adjacent_matrix )
56
+ print (self ._adjacent_matrix )
57
+
58
+ def BFS (self , source_vertex ):
59
+ i = source_vertex
60
+ q = LinkedListQueue ()
61
+ visited = [0 ] * self ._vertices
62
+ print (i , end = ' ' )
63
+ visited [i ] = 1
64
+ q .enqueue (i )
65
+ while not q .is_empty ():
66
+ i = q .dequeue ()
67
+ for j in range (self ._vertices ):
68
+ if self ._adjacent_matrix [i ][j ] == 1 and visited [j ] == 0 :
69
+ print (j , end = ' ' )
70
+ visited [j ] = 1
71
+ q .enqueue (j )
You can’t perform that action at this time.
0 commit comments