-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_shortest_path.py
71 lines (57 loc) · 2.58 KB
/
find_shortest_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Given a weighted directed graph represented as an adjacency list, write a function to find the shortest path between two nodes, A and B.
The weight of each edge is a positive integer, and there are no negative cycles in the graph.
Function Signature: def find_shortest_path(graph: dict, A: str, B: str) -> List[str]:
Input:
graph: A dictionary representing the adjacency list of the graph. The keys are strings representing the nodes,
and the values are lists of tuples (neighbor_node, weight) representing the neighboring nodes and their corresponding edge weights.
A: The starting node from which to find the shortest path.
B: The destination node to which to find the shortest path.
Output:
Return a list of nodes representing the shortest path from node A to node B. If there is no path from A to B, return an empty list.
graph = {
'A': [('B', 3), ('C', 2)],
'B': [('C', 1), ('D', 4)],
'C': [('D', 2)],
'D': [('E', 1)],
'E': [('B', 3)],
}
find_shortest_path(graph, 'A', 'D') # Output: ['A', 'C', 'D']
find_shortest_path(graph, 'B', 'E') # Output: ['B', 'D', 'E']
find_shortest_path(graph, 'A', 'F') # Output: []
"""
from heapq import heappush, heappop
def find_shortest_path(graph, A, B):
# Initialize distances and predecessors dictionaries
distances = {node: float('inf') for node in graph}
predecessors = {node: None for node in graph}
# Priority queue to store nodes with their current distance from A
priority_queue = [(0, A)]
distances[A] = 0
while priority_queue:
current_distance, current_node = heappop(priority_queue)
if current_node == B:
# Reconstruct the path from A to B using predecessors
path = []
while current_node is not None:
path.insert(0, current_node)
current_node = predecessors[current_node]
return path
for neighbor, weight in graph[current_node]:
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
predecessors[neighbor] = current_node
heappush(priority_queue, (distance, neighbor))
# If there is no path from A to B, return an empty list
return []
# Example graph
graph = {
'A': [('B', 3), ('C', 2)],
'B': [('C', 1), ('D', 4)],
'C': [('D', 2)],
'D': [('E', 1)],
'E': [('B', 3)],
}
print(find_shortest_path(graph, 'A', 'D')) # Output: ['A', 'C', 'D']
print(find_shortest_path(graph, 'B', 'E')) # Output: ['B', 'D', 'E']
print(find_shortest_path(graph, 'A', 'F')) # Output: []