Skip to content

Commit 62d4418

Browse files
authored
Fix mypy error and add more doctest on bfs_shortest_path (TheAlgorithms#4512)
1 parent 3ea5a13 commit 62d4418

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

graphs/bfs_shortest_path.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Manual test:
55
python bfs_shortest_path.py
66
"""
7-
graph = {
7+
demo_graph = {
88
"A": ["B", "C", "E"],
99
"B": ["A", "D", "E"],
1010
"C": ["A", "F", "G"],
@@ -15,7 +15,7 @@
1515
}
1616

1717

18-
def bfs_shortest_path(graph: dict, start, goal) -> str:
18+
def bfs_shortest_path(graph: dict, start, goal) -> list[str]:
1919
"""Find shortest path between `start` and `goal` nodes.
2020
Args:
2121
graph (dict): node/list of neighboring nodes key/value pairs.
@@ -25,8 +25,12 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
2525
Shortest path between `start` and `goal` nodes as a string of nodes.
2626
'Not found' string if no path found.
2727
Example:
28-
>>> bfs_shortest_path(graph, "G", "D")
28+
>>> bfs_shortest_path(demo_graph, "G", "D")
2929
['G', 'C', 'A', 'B', 'D']
30+
>>> bfs_shortest_path(demo_graph, "G", "G")
31+
['G']
32+
>>> bfs_shortest_path(demo_graph, "G", "Unknown")
33+
[]
3034
"""
3135
# keep track of explored nodes
3236
explored = set()
@@ -35,7 +39,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
3539

3640
# return path if start is goal
3741
if start == goal:
38-
return "That was easy! Start = goal"
42+
return [start]
3943

4044
# keeps looping until all possible paths have been checked
4145
while queue:
@@ -59,7 +63,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
5963
explored.add(node)
6064

6165
# in case there's no path between the 2 nodes
62-
return "So sorry, but a connecting path doesn't exist :("
66+
return []
6367

6468

6569
def bfs_shortest_path_distance(graph: dict, start, target) -> int:
@@ -72,11 +76,11 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int:
7276
Number of edges in shortest path between `start` and `target` nodes.
7377
-1 if no path exists.
7478
Example:
75-
>>> bfs_shortest_path_distance(graph, "G", "D")
79+
>>> bfs_shortest_path_distance(demo_graph, "G", "D")
7680
4
77-
>>> bfs_shortest_path_distance(graph, "A", "A")
81+
>>> bfs_shortest_path_distance(demo_graph, "A", "A")
7882
0
79-
>>> bfs_shortest_path_distance(graph, "A", "H")
83+
>>> bfs_shortest_path_distance(demo_graph, "A", "Unknown")
8084
-1
8185
"""
8286
if not graph or start not in graph or target not in graph:
@@ -102,5 +106,5 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int:
102106

103107

104108
if __name__ == "__main__":
105-
print(bfs_shortest_path(graph, "G", "D")) # returns ['G', 'C', 'A', 'B', 'D']
106-
print(bfs_shortest_path_distance(graph, "G", "D")) # returns 4
109+
print(bfs_shortest_path(demo_graph, "G", "D")) # returns ['G', 'C', 'A', 'B', 'D']
110+
print(bfs_shortest_path_distance(demo_graph, "G", "D")) # returns 4

0 commit comments

Comments
 (0)