What is a key limitation of trees that graphs overcome?
Trees cannot model relationships where nodes have multiple parents or cycles. Graphs can represent these complex connections.
Click to reveal answer
beginner
Why do graphs exist in data structures?
Graphs exist to represent complex relationships like networks, where connections are not strictly hierarchical and can include cycles or multiple links.
Click to reveal answer
beginner
Can a tree have cycles? Why or why not?
No, a tree cannot have cycles because it is a hierarchical structure with exactly one path between any two nodes.
Click to reveal answer
beginner
Give an example of a real-life situation where a graph is needed instead of a tree.
Social networks where people can be connected in many ways, including mutual friendships and groups, require graphs because trees cannot represent multiple parent connections or cycles.
Click to reveal answer
beginner
What does it mean when we say graphs can model 'non-hierarchical' relationships?
It means graphs can represent connections where nodes relate in many ways, not just in a parent-child order like trees, allowing cycles and multiple links.
Click to reveal answer
Which structure can represent cycles?
AGraph
BTree
CLinked List
DArray
✗ Incorrect
Graphs can have cycles; trees cannot because they are acyclic.
What is a key difference between trees and graphs?
AGraphs have only one root; trees have many
BGraphs are always smaller than trees
CTrees can have cycles; graphs cannot
DTrees have a strict hierarchy; graphs do not
✗ Incorrect
Trees have a strict parent-child hierarchy; graphs can represent any connections.
Why can't trees model multiple parents for a node?
ABecause trees are unordered
BBecause trees have cycles
CBecause trees allow only one parent per node
DBecause trees have no edges
✗ Incorrect
In trees, each node has exactly one parent except the root.
Which data structure is best to model a social network?
AQueue
BGraph
CStack
DTree
✗ Incorrect
Graphs can represent complex, non-hierarchical relationships like social networks.
What kind of relationship can graphs model that trees cannot?
AMultiple parents and cycles
BSingle parent only
CLinear sequences
DNo connections
✗ Incorrect
Graphs can model nodes with multiple parents and cycles, unlike trees.
Explain why trees cannot model cycles and multiple parent relationships, but graphs can.
Think about how nodes connect in trees versus graphs.
You got /5 concepts.
Describe a real-world example where a graph is necessary instead of a tree and why.
Consider networks where connections are complex and not just parent-child.
You got /4 concepts.
Practice
(1/5)
1. Why do we need graphs when trees already exist as data structures?
easy
A. Because graphs can represent connections between nodes in any pattern, including cycles and multiple links.
B. Because trees are faster to traverse than graphs.
C. Because trees can only store numbers, but graphs can store any data.
D. Because graphs use less memory than trees.
Solution
Step 1: Understand tree limitations
Trees are hierarchical and cannot have cycles or multiple parents for a node.
Step 2: Understand graph capabilities
Graphs allow cycles and multiple connections between nodes, modeling complex relationships.
Final Answer:
Because graphs can represent connections between nodes in any pattern, including cycles and multiple links. -> Option A
Quick Check:
Graphs model cycles and complex links = B [OK]
Hint: Graphs allow cycles; trees do not. Remember this difference [OK]
Common Mistakes:
Thinking trees can have cycles
Confusing data types stored with structure capabilities
Assuming graphs always use less memory
2. Which of the following is the correct way to declare an undirected graph using adjacency list in C++?
easy
A. int graph[n][n];
B. std::vector<int> graph[n];
C. std::map graph;
D. std::vector graph(n);
Solution
Step 1: Identify adjacency list declaration
Adjacency list uses vector of vectors or array of vectors to store neighbors.
Step 2: Check options
std::vector graph(n); uses vector of vectors with size n, suitable for adjacency list.
Final Answer:
std::vector graph(n); -> Option D
Quick Check:
Adjacency list = vector of vectors [OK]
Hint: Use vector of vectors for adjacency list in modern C++ [OK]
Common Mistakes:
Using map which is not adjacency list
Using 2D array which is adjacency matrix
Using vector<int> graph[n]; which is legacy C++
3. Consider the following C++ code snippet for an undirected graph:
std::vector graph(3);
graph[0].push_back(1);
graph[1].push_back(0);
graph[1].push_back(2);
graph[2].push_back(1);
for (int i = 0; i < 3; i++) {
std::cout << i << ": ";
for (int neighbor : graph[i]) {
std::cout << neighbor << " ";
}
std::cout << std::endl;
}
What is the output?
medium
A. 0: 1
1: 0
2: 1 2
B. 0: 1
1: 2
2: 1 0
C. 0: 1
1: 0 2
2: 1
D. 0: 0 1
1: 0 2
2: 1
Solution
Step 1: Trace graph construction
graph[0] has 1, graph[1] has 0 and 2, graph[2] has 1.
Step 2: Trace output loop
Print neighbors for each node: 0->1, 1->0 2, 2->1.