-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs_weighted_tree.py
62 lines (46 loc) · 1.63 KB
/
dfs_weighted_tree.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
from collections import defaultdict
from typing import Optional, Dict, List, Tuple
class Node:
def __init__(self, val: int) -> None:
self.val = val
self.neighbors: List[Tuple[Node, int]] = [] # List of (neighbor, weight) pairs
def dfs(
node: Node, parent: Optional[Node], distances: Dict[int, int], current_dist: int
) -> None:
"""
DFS to calculate shortest paths from root to all nodes
Args:
node: Current node
parent: Parent node to avoid cycles
distances: Dictionary to store shortest distances
current_dist: Current distance from root to this node
"""
# Update distance to current node
distances[node.val] = current_dist
# Explore all neighbors
for neighbor, weight in node.neighbors:
if neighbor != parent:
dfs(neighbor, node, distances, current_dist + weight)
def build_tree() -> Node:
"""Helper function to build a sample weighted tree"""
# Create nodes
nodes = [Node(i) for i in range(5)]
# Add edges with weights
edges = [(0, 1, 4), (0, 2, 1), (1, 3, 3), (2, 4, 2)] # (from, to, weight)
# Build adjacency lists
for u, v, w in edges:
nodes[u].neighbors.append((nodes[v], w))
nodes[v].neighbors.append((nodes[u], w))
return nodes[0] # Return root
def main():
# Build sample tree
root = build_tree()
# Calculate shortest paths from root
distances = {}
dfs(root, None, distances, 0)
# Print results
print("Shortest distances from root:")
for node, dist in sorted(distances.items()):
print(f"Node {node}: {dist}")
if __name__ == "__main__":
main()