What is a key limitation of trees that graphs overcome?
Trees cannot model relationships where nodes have multiple parents or cycles. Graphs allow multiple connections and cycles, representing more complex relationships.
Click to reveal answer
beginner
Why do graphs exist in data structures?
Graphs exist to represent complex relationships between items where connections are not strictly hierarchical, such as social networks, road maps, or web links.
Click to reveal answer
beginner
What real-life example shows why trees are not enough and graphs are needed?
A city map with roads connecting multiple places in many ways needs a graph because places can connect in loops and multiple paths, which trees cannot represent.
Click to reveal answer
beginner
Can trees have cycles? Why or why not?
No, trees cannot have cycles because they are defined as connected structures with no loops, ensuring a single path between any two nodes.
Click to reveal answer
beginner
How does a graph differ from a tree in terms of node connections?
In a graph, nodes can connect to any number of other nodes, including forming cycles and multiple parents, while in a tree, each node has exactly one parent except the root.
Click to reveal answer
Which structure can represent cycles and multiple parents?
AGraph
BTree
CLinked List
DArray
✗ Incorrect
Graphs can represent cycles and multiple parents, unlike trees which cannot have cycles and have a strict parent-child hierarchy.
Why can't trees model all real-world relationships?
ABecause they have too many nodes
BBecause they do not allow cycles or multiple parents
CBecause they are too complex
DBecause they are unordered
✗ Incorrect
Trees do not allow cycles or multiple parents, limiting their ability to model complex relationships.
What kind of data structure would best model a social network?
ATree
BStack
CGraph
DQueue
✗ Incorrect
Social networks have complex connections with cycles and multiple links, best modeled by graphs.
In a tree, how many parents can a node have?
AOne or zero
BMultiple
CNone
DUnlimited
✗ Incorrect
Each node in a tree has exactly one parent except the root, which has none.
Which of these is NOT a feature of graphs?
AFlexible connections
BNodes can have multiple parents
CCan have cycles
DStrict hierarchical structure
✗ Incorrect
Graphs do not require a strict hierarchical structure; that is a feature of trees.
Explain why trees cannot model certain relationships and how graphs solve this problem.
Think about how connections between nodes differ in trees and graphs.
You got /5 concepts.
Describe a real-world example where a graph is necessary instead of a tree and why.
Consider maps, social networks, or web links.
You got /4 concepts.
Practice
(1/5)
1. Why do we need graphs when we already have trees in data structures?
easy
A. Because graphs can represent connections between nodes that trees cannot, like cycles and multiple parents.
B. Because trees are faster to traverse than graphs.
C. Because trees use less memory than graphs.
D. Because graphs only store numbers, while trees store strings.
Solution
Step 1: Understand tree structure limitations
Trees have a strict hierarchy where each node has only one parent and no cycles.
Step 2: Identify graph capabilities
Graphs allow nodes to have multiple connections, including cycles and multiple parents, which trees cannot model.
Final Answer:
Because graphs can represent connections between nodes that trees cannot, like cycles and multiple parents. -> Option A
Quick Check:
Graphs model cycles and multiple parents [OK]
Hint: Graphs allow cycles and multiple parents; trees do not [OK]
Common Mistakes:
Thinking trees can have cycles
Confusing memory use with structure capability
Assuming graphs only store numbers
2. Which of the following Rust code snippets correctly declares an undirected graph using adjacency lists?
easy
A. let graph: Vec<usize> = vec![0, 1, 2];
B. let graph: Vec = vec![vec![], vec![], vec![]];
C. let graph: HashMap = HashMap::new();
D. let graph: Vec<(usize, usize)> = vec![(0, 1), (1, 2)];
Solution
Step 1: Identify adjacency list structure
An adjacency list is a vector of vectors where each inner vector holds neighbors of a node.
Step 2: Match code to adjacency list
let graph: Vec = vec![vec![], vec![], vec![]]; declares a vector of vectors of usize, suitable for adjacency lists.
Final Answer:
let graph: Vec = vec![vec![], vec![], vec![]]; -> Option B
Quick Check:
Adjacency list = Vec [OK]
Hint: Adjacency list uses Vec of Vec for neighbors [OK]
Common Mistakes:
Using Vec<usize> instead of Vec
Using HashMap with wrong types
Using edge list instead of adjacency list
3. Consider this Rust code that builds a graph and prints neighbors of node 1:
graph[1] has neighbors pushed: 0 and 2, so graph[1] = [0, 2].
Step 2: Print neighbors of node 1
Printing graph[1] outputs [0, 2].
Final Answer:
[0, 2] -> Option D
Quick Check:
Neighbors of node 1 = [0, 2] [OK]
Hint: Check which neighbors were pushed to node 1's list [OK]
Common Mistakes:
Confusing node indices
Assuming neighbors include the node itself
Mixing order of neighbors
4. This Rust code tries to add an edge to a graph but causes a runtime error:
let mut graph = vec![vec![], vec![]];
graph[0].push(1);
graph[1].push(0);
graph[2].push(1);
What is the error and how to fix it?
medium
A. Index out of bounds error; fix by increasing graph size to include index 2.
B. Type mismatch error; fix by changing Vec to HashMap.
C. Syntax error; fix by adding semicolons.
D. Logic error; fix by removing graph[1].push(0).
Solution
Step 1: Identify cause of runtime error
graph has length 2, so graph[2] access is out of bounds causing panic.
Step 2: Fix by resizing graph
Increase graph size to at least 3 elements to allow graph[2].push(1).
Final Answer:
Index out of bounds error; fix by increasing graph size to include index 2. -> Option A
Quick Check:
Accessing graph[2] with size 2 causes error [OK]
Hint: Check vector size before indexing [OK]
Common Mistakes:
Ignoring vector size limits
Confusing syntax and runtime errors
Removing unrelated lines
5. You want to model a social network where users can be friends with multiple people and friendships can be mutual or one-way. Why is a graph a better choice than a tree for this model?
hard
A. Because trees can only store numbers, not user data.
B. Because trees are faster to search in social networks.
C. Because graphs allow cycles and multiple connections, representing mutual and one-way friendships.
D. Because graphs use less memory than trees.
Solution
Step 1: Understand social network connections
Users can have multiple friends, and friendships can be mutual (cycles) or one-way (directed edges).
Step 2: Compare tree and graph capabilities
Trees cannot represent cycles or multiple parents, but graphs can model these complex relationships.
Step 3: Conclude best data structure
Graphs are better because they allow cycles and multiple connections needed for social networks.
Final Answer:
Because graphs allow cycles and multiple connections, representing mutual and one-way friendships. -> Option C
Quick Check:
Social network needs cycles and multiple edges [OK]
Hint: Social networks need cycles and multiple edges; use graphs [OK]
Common Mistakes:
Thinking trees can model mutual friendships
Confusing data type storage with structure capability
Assuming trees use less memory always
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.