Skip to content

Commit fadb3da

Browse files
Add complex graph example to Dijkstra implementation
1 parent 90639c4 commit fadb3da

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

graph/dijkstra.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,37 @@ def dijkstra(graph: Dict[int, List[Tuple[int, int]]], start: int) -> Dict[int, i
3333

3434
# Example usage
3535
if __name__ == "__main__":
36-
# Example graph
37-
graph = {
36+
# Simple graph example
37+
simple_graph = {
3838
0: [(1, 4), (2, 1)],
3939
1: [(3, 1)],
4040
2: [(1, 2), (3, 5)],
4141
3: [(4, 3)],
4242
4: []
4343
}
4444

45+
print("Simple Graph Example:")
4546
start_node = 0
46-
shortest_paths = dijkstra(graph, start_node)
47+
shortest_paths = dijkstra(simple_graph, start_node)
48+
print(f"Shortest paths from node {start_node}:")
49+
for node, distance in shortest_paths.items():
50+
print(f"To node {node}: {distance}")
51+
52+
# More complex graph example
53+
complex_graph = {
54+
0: [(1, 4), (2, 2)],
55+
1: [(2, 1), (3, 5)],
56+
2: [(3, 8), (4, 10)],
57+
3: [(4, 2), (5, 6)],
58+
4: [(5, 3)],
59+
5: [(6, 1)],
60+
6: [(4, 4), (7, 2)],
61+
7: []
62+
}
63+
64+
print("\nComplex Graph Example:")
65+
start_node = 0
66+
shortest_paths = dijkstra(complex_graph, start_node)
4767
print(f"Shortest paths from node {start_node}:")
4868
for node, distance in shortest_paths.items():
4969
print(f"To node {node}: {distance}")

0 commit comments

Comments
 (0)