Skip to content

Fix mypy error and add more doctest on bfs_shortest_path #4512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions graphs/bfs_shortest_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Manual test:
python bfs_shortest_path.py
"""
graph = {
demo_graph = {
"A": ["B", "C", "E"],
"B": ["A", "D", "E"],
"C": ["A", "F", "G"],
Expand All @@ -15,7 +15,7 @@
}


def bfs_shortest_path(graph: dict, start, goal) -> str:
def bfs_shortest_path(graph: dict, start, goal) -> list[str]:
"""Find shortest path between `start` and `goal` nodes.
Args:
graph (dict): node/list of neighboring nodes key/value pairs.
Expand All @@ -25,8 +25,12 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
Shortest path between `start` and `goal` nodes as a string of nodes.
'Not found' string if no path found.
Example:
>>> bfs_shortest_path(graph, "G", "D")
>>> bfs_shortest_path(demo_graph, "G", "D")
['G', 'C', 'A', 'B', 'D']
>>> bfs_shortest_path(demo_graph, "G", "G")
['G']
>>> bfs_shortest_path(demo_graph, "G", "Unknown")
[]
"""
# keep track of explored nodes
explored = set()
Expand All @@ -35,7 +39,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:

# return path if start is goal
if start == goal:
return "That was easy! Start = goal"
return [start]

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

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


def bfs_shortest_path_distance(graph: dict, start, target) -> int:
Expand All @@ -72,11 +76,11 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int:
Number of edges in shortest path between `start` and `target` nodes.
-1 if no path exists.
Example:
>>> bfs_shortest_path_distance(graph, "G", "D")
>>> bfs_shortest_path_distance(demo_graph, "G", "D")
4
>>> bfs_shortest_path_distance(graph, "A", "A")
>>> bfs_shortest_path_distance(demo_graph, "A", "A")
0
>>> bfs_shortest_path_distance(graph, "A", "H")
>>> bfs_shortest_path_distance(demo_graph, "A", "Unknown")
-1
"""
if not graph or start not in graph or target not in graph:
Expand All @@ -102,5 +106,5 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int:


if __name__ == "__main__":
print(bfs_shortest_path(graph, "G", "D")) # returns ['G', 'C', 'A', 'B', 'D']
print(bfs_shortest_path_distance(graph, "G", "D")) # returns 4
print(bfs_shortest_path(demo_graph, "G", "D")) # returns ['G', 'C', 'A', 'B', 'D']
print(bfs_shortest_path_distance(demo_graph, "G", "D")) # returns 4