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 3eeff90

Browse files
BFS Implementation
1 parent f156551 commit 3eeff90

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

‎Data-Structures-and-Algorithms/Breadth-First-Search.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,27 @@
2020
Time Complexity: O(V + E)
2121
Space Complexity: O(V)
2222
V is number of vertices (nodes), E is number of edges
23-
'''
23+
'''
24+
25+
class Graph:
26+
def __init__(self):
27+
self.graph = {}
28+
29+
def insertEdge(self, v1, v2):
30+
if v1 in self.graph:
31+
self.graph[v1].append(v2)
32+
else:
33+
self.graph[v1] = [v2]
34+
35+
def BSF(self, start_node):
36+
visited, queue = {start_node}, [start_node]
37+
38+
while queue:
39+
cur = queue.pop(0)
40+
print(cur, end = " ")
41+
42+
if cur in self.graph:
43+
for node in self.graph[cur]:
44+
if node not in visited:
45+
queue.append(node)
46+
visited.add(node)

0 commit comments

Comments
(0)

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