What is the main difference between a graph and a tree?
A tree is a special type of graph with no cycles and exactly one path between any two nodes, while a graph can have cycles and multiple paths between nodes.
Click to reveal answer
beginner
Why can't shortest path problems be solved using only trees?
Because shortest path problems often involve graphs with cycles and multiple paths, which trees do not have. Trees have only one path between nodes, so they don't represent all possible routes.
Click to reveal answer
beginner
What does having multiple paths between nodes in a graph allow us to do in shortest path problems?
It allows us to compare different routes and choose the one with the smallest total distance or cost.
Click to reveal answer
intermediate
How does the presence of cycles in a graph affect shortest path calculations?
Cycles mean that paths can loop back to the same node, so algorithms must avoid infinite loops and carefully select the shortest route.
Click to reveal answer
intermediate
In Rust, which data structure is more suitable for representing graphs when solving shortest path problems?
An adjacency list or adjacency matrix is suitable for representing graphs in Rust, as they can efficiently store nodes and edges including cycles and multiple paths.
Click to reveal answer
Why is shortest path considered a graph problem rather than a tree problem?
ABecause graphs can have multiple paths and cycles, trees cannot
BBecause trees are always weighted, graphs are not
CBecause trees have more nodes than graphs
DBecause shortest path only works on trees
✗ Incorrect
Shortest path problems require exploring multiple paths and possibly cycles, which graphs support but trees do not.
What is a key property of trees that limits their use in shortest path problems?
AThey have cycles
BThey have exactly one path between any two nodes
CThey have weighted edges only
DThey allow multiple paths between nodes
✗ Incorrect
Trees have exactly one path between any two nodes, so they don't allow comparing multiple routes.
Which of these is true about graphs?
AGraphs can have cycles and multiple paths between nodes
BGraphs always have exactly one path between nodes
CGraphs never have cycles
DGraphs are a type of tree
✗ Incorrect
Graphs can have cycles and multiple paths, which is why they are used for shortest path problems.
What must shortest path algorithms handle when working with graphs?
AOnly trees
BOnly one path per node pair
CNo edges
DCycles and multiple paths
✗ Incorrect
Shortest path algorithms must handle cycles and multiple paths to find the best route.
Which data structure is commonly used in Rust to represent graphs for shortest path problems?
ALinked list
BStack
CAdjacency list
DQueue
✗ Incorrect
Adjacency lists efficiently represent graphs with nodes and edges, including cycles and multiple paths.
Think about how many ways you can travel between two places.
You got /4 concepts.
Describe how cycles in graphs affect shortest path algorithms.
Consider what happens if you keep going around in circles.
You got /4 concepts.
Practice
(1/5)
1. Why is finding the shortest path usually considered a graph problem rather than a tree problem?
easy
A. Because graphs can have cycles and multiple paths between nodes, unlike trees.
B. Because trees always have weighted edges, making shortest path trivial.
C. Because trees do not have nodes connected by edges.
D. Because shortest path algorithms only work on graphs with no edges.
Solution
Step 1: Understand the structure of trees and graphs
Trees are a special type of graph with no cycles and exactly one path between any two nodes.
Step 2: Analyze shortest path requirements
Shortest path problems need to consider multiple paths and cycles, which only general graphs can have.
Final Answer:
Because graphs can have cycles and multiple paths between nodes, unlike trees. -> Option A
Quick Check:
Graph complexity = shortest path problem [OK]
Hint: Shortest path needs multiple paths; trees have only one [OK]
Common Mistakes:
Thinking trees have cycles
Assuming shortest path is trivial in trees
Confusing edges with nodes
2. Which Rust code snippet correctly declares a graph using adjacency lists for shortest path problems?
easy
A. let graph: Vec> = vec![vec![]; n];
B. let graph: Vec<(usize, usize)> = vec![(0, 0); n];
C. let graph: Vec<usize> = vec![0; n];
D. let graph: HashMap = HashMap::new();
Solution
Step 1: Identify adjacency list structure
An adjacency list for a weighted graph stores a vector of neighbors with weights, e.g., Vec>.
Step 2: Match Rust syntax
let graph: Vec> = vec![vec![]; n]; correctly initializes a vector of vectors of tuples (neighbor, weight) with size n.
Final Answer:
let graph: Vec> = vec![vec![]; n]; -> Option A
Quick Check:
Adjacency list = Vec of Vec of (node, weight) [OK]
Hint: Adjacency list = Vec> in Rust [OK]
Common Mistakes:
Using flat Vec instead of nested Vec
Confusing HashMap with adjacency list
Wrong tuple types for edges
3. Consider this Rust code snippet for a graph with edges: (0->1:2), (0->2:5), (1->2:1). What is the shortest path distance from node 0 to 2 using Dijkstra's algorithm?
medium
A. 5
B. 2
C. 6
D. 3
Solution
Step 1: List all paths from 0 to 2
Paths: 0->2 with cost 5, 0->1->2 with cost 2+1=3.
Step 2: Choose the minimum cost path
Shortest path cost is 3 via 0->1->2.
Final Answer:
3 -> Option D
Quick Check:
Minimum path cost = 3 [OK]
Hint: Check all paths, pick smallest sum [OK]
Common Mistakes:
Choosing direct edge without checking alternate paths
Adding weights incorrectly
Ignoring intermediate nodes
4. This Rust code tries to find shortest path but fails. What is the main bug?
let mut dist = vec![usize::MAX; n];
dist[0] = 0;
for i in 0..n {
for &(v, w) in &graph[i] {
if dist[i] + w < dist[v] {
dist[v] = dist[i] + w;
}
}
}
medium
A. It uses usize::MAX which causes panic on addition.
B. It does not handle cycles properly, needs multiple iterations.
C. It initializes dist with zeros instead of max values.
D. It updates dist[v] without checking if v is visited.
Solution
Step 1: Analyze the loop structure
The code runs only one pass over all edges, which is insufficient for graphs with cycles or longer paths.