Skip to content

Commit b2a2b16

Browse files
authored
update graphs/breadth_first_search.py (TheAlgorithms#3908)
* update graphs/breadth_first_search.py - update naming style to snake_case - add type hints * add doctests
1 parent 38ebad7 commit b2a2b16

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

Diff for: graphs/breadth_first_search.py

+53-21
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,61 @@
22

33
""" Author: OMKAR PATHAK """
44

5+
from typing import Set
6+
57

68
class Graph:
7-
def __init__(self):
9+
def __init__(self) -> None:
810
self.vertices = {}
911

10-
def printGraph(self):
11-
"""prints adjacency list representation of graaph"""
12-
for i in self.vertices.keys():
12+
def print_graph(self) -> None:
13+
"""
14+
prints adjacency list representation of graaph
15+
>>> g = Graph()
16+
>>> g.print_graph()
17+
>>> g.add_edge(0, 1)
18+
>>> g.print_graph()
19+
0 : 1
20+
"""
21+
for i in self.vertices:
1322
print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
1423

15-
def addEdge(self, fromVertex, toVertex):
16-
"""adding the edge between two vertices"""
17-
if fromVertex in self.vertices.keys():
18-
self.vertices[fromVertex].append(toVertex)
24+
def add_edge(self, from_vertex: int, to_vertex: int) -> None:
25+
"""
26+
adding the edge between two vertices
27+
>>> g = Graph()
28+
>>> g.print_graph()
29+
>>> g.add_edge(0, 1)
30+
>>> g.print_graph()
31+
0 : 1
32+
"""
33+
if from_vertex in self.vertices:
34+
self.vertices[from_vertex].append(to_vertex)
1935
else:
20-
self.vertices[fromVertex] = [toVertex]
36+
self.vertices[from_vertex] = [to_vertex]
2137

22-
def BFS(self, startVertex):
38+
def bfs(self, start_vertex: int) -> Set[int]:
39+
"""
40+
>>> g = Graph()
41+
>>> g.add_edge(0, 1)
42+
>>> g.add_edge(0, 1)
43+
>>> g.add_edge(0, 2)
44+
>>> g.add_edge(1, 2)
45+
>>> g.add_edge(2, 0)
46+
>>> g.add_edge(2, 3)
47+
>>> g.add_edge(3, 3)
48+
>>> sorted(g.bfs(2))
49+
[0, 1, 2, 3]
50+
"""
2351
# initialize set for storing already visited vertices
2452
visited = set()
2553

2654
# create a first in first out queue to store all the vertices for BFS
2755
queue = []
2856

2957
# mark the source node as visited and enqueue it
30-
visited.add(startVertex)
31-
queue.append(startVertex)
58+
visited.add(start_vertex)
59+
queue.append(start_vertex)
3260

3361
while queue:
3462
vertex = queue.pop(0)
@@ -42,18 +70,22 @@ def BFS(self, startVertex):
4270

4371

4472
if __name__ == "__main__":
73+
from doctest import testmod
74+
75+
testmod(verbose=True)
76+
4577
g = Graph()
46-
g.addEdge(0, 1)
47-
g.addEdge(0, 2)
48-
g.addEdge(1, 2)
49-
g.addEdge(2, 0)
50-
g.addEdge(2, 3)
51-
g.addEdge(3, 3)
52-
53-
g.printGraph()
78+
g.add_edge(0, 1)
79+
g.add_edge(0, 2)
80+
g.add_edge(1, 2)
81+
g.add_edge(2, 0)
82+
g.add_edge(2, 3)
83+
g.add_edge(3, 3)
84+
85+
g.print_graph()
5486
# 0 : 1 -> 2
5587
# 1 : 2
5688
# 2 : 0 -> 3
5789
# 3 : 3
5890

59-
assert sorted(g.BFS(2)) == [0, 1, 2, 3]
91+
assert sorted(g.bfs(2)) == [0, 1, 2, 3]

0 commit comments

Comments
 (0)