📖 Scenario: You are working with a network of connected points represented as a tree. Each point is connected by edges without any cycles. You want to find the longest path between any two points in this network, called the diameter of the tree.
🎯 Goal: Build a Rust program that uses dynamic programming on trees to find the diameter (longest path) of a given tree represented by edges.
📋 What You'll Learn
Create a tree using adjacency lists from given edges
Use a helper variable to track the maximum diameter found
Implement a depth-first search (DFS) function to compute the longest paths
Print the diameter of the tree after computation
💡 Why This Matters
🌍 Real World
Finding the diameter of a tree is useful in network design, biology (e.g., evolutionary trees), and computer graphics to understand the longest distance between nodes.
💼 Career
Understanding tree algorithms and dynamic programming is essential for software engineers working on graph problems, optimization, and system design.
Progress0 / 4 steps
1
Create the tree adjacency list
Create a variable called edges as a vector of tuples with these exact pairs: (0, 1), (1, 2), (1, 3), (3, 4). Then create a variable called n and set it to 5 representing the number of nodes. Finally, create a vector of vectors called tree with n empty vectors to hold adjacency lists.
DSA Rust
Hint
Use vec![] to create empty adjacency lists for each node.
2
Build the adjacency list from edges
Add a for loop iterating over edges with variables u and v. Inside the loop, add v to tree[u] and u to tree[v] to build the undirected tree. Also, create a mutable variable called max_diameter and set it to 0 to track the longest path found.
DSA Rust
Hint
Remember to add edges both ways because the tree is undirected.
3
Implement DFS to find diameter
Write a recursive function called dfs that takes parameters node: usize, parent: usize, tree: &Vec>, and max_diameter: &mut usize. Inside, create a vector depths to store depths of children. For each child in tree[node] not equal to parent, recursively call dfs and collect depths. Update max_diameter with the sum of the two largest depths if greater. Return the maximum depth plus one.
DSA Rust
Hint
Use recursion to explore children and keep track of the two longest depths to update the diameter.
4
Print the diameter of the tree
Write a println! statement to print the value of max_diameter.
DSA Rust
Hint
The diameter of this tree is 4 edges long, so printing max_diameter should output 4.
Practice
(1/5)
1. What does the diameter of a tree represent in the context of DP on Trees?
easy
A. The longest path between any two nodes in the tree
B. The number of nodes in the tree
C. The height of the tree from the root
D. The sum of all edge weights in the tree
Solution
Step 1: Understand the definition of tree diameter
The diameter is the longest path between any two nodes, not just from the root or sum of edges.
Step 2: Differentiate from other tree properties
Height is from root to leaf, sum of edges is total weight, number of nodes is size, none equal diameter.
Final Answer:
The longest path between any two nodes in the tree -> Option A
Quick Check:
Diameter = Longest path [OK]
Hint: Diameter = longest path between any two nodes [OK]
Common Mistakes:
Confusing diameter with tree height
Thinking diameter is total nodes count
Assuming diameter is sum of all edges
2. Which Rust syntax correctly declares a recursive function to compute the diameter of a tree represented as adjacency list Vec?