Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 42bbe42

Browse files
Three Hundred - Fifty Commit: Implement BFS() function
1 parent ccdfe5e commit 42bbe42

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

‎Section_12(Graphs)/(1)_graph.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,18 @@ def indegree(self, v):
100100
def display_adjacent_matrix(self):
101101
print(self._adjacent_matrix)
102102

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+

‎Section_12(Graphs)/graph.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,19 @@ def indegree(self, v):
5353

5454
# Helper function to display adjacency matrix
5555
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)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /