mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Changed how the Visited nodes are tracked (#3811)
Updated the code to track visited Nodes with Set data structure instead of Lists to bring down the lookup time in visited from O(N) to O(1) as doing O(N) lookup each time in the visited List will become significantly slow when the graph grows
This commit is contained in:
parent
b55e132b80
commit
fa364dfd27
@ -1,8 +1,6 @@
|
|||||||
"""Breadth-first search shortest path implementations.
|
"""Breadth-first search shortest path implementations.
|
||||||
|
|
||||||
doctest:
|
doctest:
|
||||||
python -m doctest -v bfs_shortest_path.py
|
python -m doctest -v bfs_shortest_path.py
|
||||||
|
|
||||||
Manual test:
|
Manual test:
|
||||||
python bfs_shortest_path.py
|
python bfs_shortest_path.py
|
||||||
"""
|
"""
|
||||||
@ -19,22 +17,19 @@ graph = {
|
|||||||
|
|
||||||
def bfs_shortest_path(graph: dict, start, goal) -> str:
|
def bfs_shortest_path(graph: dict, start, goal) -> str:
|
||||||
"""Find shortest path between `start` and `goal` nodes.
|
"""Find shortest path between `start` and `goal` nodes.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
graph (dict): node/list of neighboring nodes key/value pairs.
|
graph (dict): node/list of neighboring nodes key/value pairs.
|
||||||
start: start node.
|
start: start node.
|
||||||
goal: target node.
|
goal: target node.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Shortest path between `start` and `goal` nodes as a string of nodes.
|
Shortest path between `start` and `goal` nodes as a string of nodes.
|
||||||
'Not found' string if no path found.
|
'Not found' string if no path found.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
>>> bfs_shortest_path(graph, "G", "D")
|
>>> bfs_shortest_path(graph, "G", "D")
|
||||||
['G', 'C', 'A', 'B', 'D']
|
['G', 'C', 'A', 'B', 'D']
|
||||||
"""
|
"""
|
||||||
# keep track of explored nodes
|
# keep track of explored nodes
|
||||||
explored = []
|
explored = set()
|
||||||
# keep track of all the paths to be checked
|
# keep track of all the paths to be checked
|
||||||
queue = [[start]]
|
queue = [[start]]
|
||||||
|
|
||||||
@ -61,7 +56,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
|
|||||||
return new_path
|
return new_path
|
||||||
|
|
||||||
# mark node as explored
|
# mark node as explored
|
||||||
explored.append(node)
|
explored.add(node)
|
||||||
|
|
||||||
# in case there's no path between the 2 nodes
|
# in case there's no path between the 2 nodes
|
||||||
return "So sorry, but a connecting path doesn't exist :("
|
return "So sorry, but a connecting path doesn't exist :("
|
||||||
@ -69,16 +64,13 @@ def bfs_shortest_path(graph: dict, start, goal) -> str:
|
|||||||
|
|
||||||
def bfs_shortest_path_distance(graph: dict, start, target) -> int:
|
def bfs_shortest_path_distance(graph: dict, start, target) -> int:
|
||||||
"""Find shortest path distance between `start` and `target` nodes.
|
"""Find shortest path distance between `start` and `target` nodes.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
graph: node/list of neighboring nodes key/value pairs.
|
graph: node/list of neighboring nodes key/value pairs.
|
||||||
start: node to start search from.
|
start: node to start search from.
|
||||||
target: node to search for.
|
target: node to search for.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Number of edges in shortest path between `start` and `target` nodes.
|
Number of edges in shortest path between `start` and `target` nodes.
|
||||||
-1 if no path exists.
|
-1 if no path exists.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
>>> bfs_shortest_path_distance(graph, "G", "D")
|
>>> bfs_shortest_path_distance(graph, "G", "D")
|
||||||
4
|
4
|
||||||
@ -92,7 +84,7 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int:
|
|||||||
if start == target:
|
if start == target:
|
||||||
return 0
|
return 0
|
||||||
queue = [start]
|
queue = [start]
|
||||||
visited = [start]
|
visited = set(start)
|
||||||
# Keep tab on distances from `start` node.
|
# Keep tab on distances from `start` node.
|
||||||
dist = {start: 0, target: -1}
|
dist = {start: 0, target: -1}
|
||||||
while queue:
|
while queue:
|
||||||
@ -103,7 +95,7 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int:
|
|||||||
)
|
)
|
||||||
for adjacent in graph[node]:
|
for adjacent in graph[node]:
|
||||||
if adjacent not in visited:
|
if adjacent not in visited:
|
||||||
visited.append(adjacent)
|
visited.add(adjacent)
|
||||||
queue.append(adjacent)
|
queue.append(adjacent)
|
||||||
dist[adjacent] = dist[node] + 1
|
dist[adjacent] = dist[node] + 1
|
||||||
return dist[target]
|
return dist[target]
|
||||||
|
Loading…
Reference in New Issue
Block a user