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, including cycles and many-to-many relationships.
Click to reveal answer
beginner
Why do graphs exist in data structures?
Graphs exist to represent complex relationships between objects that are not strictly hierarchical, such as social networks, road maps, or web links, which trees cannot model effectively.
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 intersections in many ways, including loops, cannot be represented by a tree but can be modeled by a graph.
Click to reveal answer
intermediate
What does it mean when a graph has cycles, and why can't trees have them?
A cycle means you can start at one node and follow edges to return to the same node. Trees cannot have cycles because they are strictly hierarchical with one unique path between nodes.
Click to reveal answer
intermediate
How do graphs model many-to-many relationships differently from trees?
Graphs allow nodes to connect to multiple other nodes in any pattern, including multiple parents or loops, while trees restrict each node to one parent and no cycles.
Click to reveal answer
Which data structure can represent cycles in relationships?
AArray
BGraph
CTree
DStack
✗ Incorrect
Graphs can have cycles, but trees cannot because trees have a strict hierarchical structure without loops.
Why can't trees model many-to-many relationships?
ABecause trees have cycles
BBecause trees allow multiple roots
CBecause each node has only one parent
DBecause trees are unordered
✗ Incorrect
In trees, each node has exactly one parent (except the root), so many-to-many relationships are not possible.
Which real-world example is best modeled by a graph rather than a tree?
ACity road network
BFamily tree
CCompany hierarchy
DFile system folders
✗ Incorrect
City road networks have loops and multiple connections, which graphs can model but trees cannot.
What is a defining feature of a tree compared to a graph?
AHas cycles
BIs unordered
CAllows multiple parents per node
DHas exactly one root and no cycles
✗ Incorrect
Trees have one root and no cycles, making them hierarchical and acyclic.
Graphs are used when data relationships are:
AComplex with cycles and many-to-many connections
BLinear
CStrictly hierarchical
DFlat and sequential
✗ Incorrect
Graphs handle complex relationships including cycles and many-to-many connections, unlike trees.
Explain why trees cannot model cycles and many-to-many relationships, and how graphs solve this problem.
Think about how nodes connect in trees versus graphs.
You got /4 concepts.
Describe a real-life situation where a graph is necessary instead of a tree and why.
Consider road maps or social networks.
You got /3 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 in any pattern, including cycles and multiple parents.
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 have a strict hierarchy where each node has only one parent and no cycles.
Step 2: Understand graph capabilities
Graphs allow nodes to connect in any way, including cycles and multiple parents, which trees cannot model.
Final Answer:
Because graphs can represent connections between nodes in any pattern, including cycles and multiple parents. -> Option A
Quick Check:
Graphs model complex connections [OK]
Hint: Graphs allow cycles and multiple parents; trees do not [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 adjacency list for a graph in Go?
easy
A. var graph [][]int = make(map[int][]int)
B. var graph []int = make([]int, 10)
C. var graph map[int]int = make(map[int]int)
D. var graph map[int][]int = make(map[int][]int)
Solution
Step 1: Understand adjacency list structure
An adjacency list maps each node (int) to a list of connected nodes ([]int).
Step 2: Match Go syntax
map[int][]int is the correct type, and make(map[int][]int) initializes it properly.
Final Answer:
var graph map[int][]int = make(map[int][]int) -> Option D
Quick Check:
Adjacency list = map[int][]int [OK]
Hint: Adjacency list is map from node to slice of nodes [OK]
Common Mistakes:
Using slice instead of map for adjacency list
Wrong map value type (int instead of []int)
Incorrect initialization syntax
3. What will be the output of the following Go code snippet modeling a graph with cycles?
B. Appending to a nil slice in map causes runtime error.
C. No bug; code correctly builds the graph adjacency list.
D. Keys must be strings, not integers.
Solution
Step 1: Check map initialization
map[int][]int is initialized with make, so it's ready for use.
Step 2: Understand append behavior on nil slices
Appending to a nil slice returns a new slice; map entries get updated correctly.
Final Answer:
No bug; code correctly builds the graph adjacency list. -> Option C
Quick Check:
Appending to nil slice in map is safe [OK]
Hint: Appending to nil slice in map is safe in Go [OK]
Common Mistakes:
Thinking map must be pre-filled with empty slices
Assuming append on nil slice causes error
Believing map keys must be strings
5. You want to model a social network where users can follow multiple users and cycles exist (e.g., A follows B, B follows C, C follows A). Why is a graph a better choice than a tree, and how would you represent this in Go?
hard
A. Because trees allow cycles; use a tree structure with parent pointers.
B. Because trees cannot represent cycles or multiple parents; use a map[int][]int adjacency list to represent followers.
C. Because trees are faster; use a slice of structs to represent users.
D. Because graphs use less memory; use a map[int]int to store followers.
Solution
Step 1: Understand why trees are insufficient
Trees do not allow cycles or multiple parents, which are common in social networks.
Step 2: Choose graph representation in Go
An adjacency list using map[int][]int maps each user to the list of users they follow, allowing cycles and multiple connections.
Final Answer:
Because trees cannot represent cycles or multiple parents; use a map[int][]int adjacency list to represent followers. -> Option B
Quick Check:
Graphs model cycles and multiple parents [OK]
Hint: Use graph adjacency list for cycles and multiple parents [OK]
Common Mistakes:
Choosing tree despite cycles in data
Using map[int]int which can't store multiple followers
Assuming trees allow cycles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.