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 as a data structure?
Graphs exist to represent complex relationships between objects where connections are not strictly hierarchical, such as social networks, road maps, or web 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 a single path between any two nodes. Cycles would break this hierarchy.
Click to reveal answer
beginner
Give an example of a real-life situation where a graph is needed instead of a tree.
A social network where people can be friends with multiple others, including mutual friendships, requires a graph because it has cycles and multiple connections.
Click to reveal answer
beginner
What does it mean when we say graphs can model 'many-to-many' relationships?
It means nodes can connect to multiple other nodes in any direction, unlike trees where each child has only one parent.
Click to reveal answer
Which data structure can represent cycles and multiple parents for nodes?
AGraph
BTree
CArray
DStack
✗ Incorrect
Graphs can represent cycles and multiple parents, while trees cannot.
Why can't trees model social networks well?
ABecause trees use too much memory
BBecause trees are too slow
CBecause trees do not allow cycles or multiple parents
DBecause trees only store numbers
✗ Incorrect
Social networks have cycles and multiple connections, which trees cannot represent.
What kind of relationship does a tree represent?
AHierarchical with one parent per child
BMany-to-many
CRandom connections
DCyclic connections
✗ Incorrect
Trees represent hierarchical relationships where each child has exactly one parent.
Which of these is an example where graphs are necessary?
ACompany organizational chart
BRoad maps with intersections
CFile directory structure
DBinary search tree
✗ Incorrect
Road maps have intersections and cycles, which require graphs to model.
What is a cycle in graph terms?
AA disconnected node
BA node with no children
CA node with multiple parents
DA path that starts and ends at the same node
✗ Incorrect
A cycle is a path where you can start at a node and return to it by following edges.
Explain why trees cannot model many-to-many relationships and how graphs solve this problem.
Think about how connections between nodes differ in trees and graphs.
You got /4 concepts.
Describe a real-world example where using a graph is necessary instead of a tree and why.
Consider social networks, maps, or web links.
You got /3 concepts.
Practice
(1/5)
1. Why do we need graphs when we already have trees in data structures?
easy
A. Because trees can only store numbers, not other data types.
B. Because trees are faster to traverse than graphs.
C. Because graphs are simpler and have no edges.
D. Because graphs can represent connections between nodes in any pattern, including cycles.
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 any connections, including cycles and multiple edges between nodes.
Final Answer:
Because graphs can represent connections between nodes in any pattern, including cycles. -> Option D
Quick Check:
Graphs model cycles, trees cannot [OK]
Hint: Graphs allow cycles; trees do not [OK]
Common Mistakes:
Thinking trees can have cycles
Confusing data types with structure
Assuming graphs are simpler than trees
2. Which of the following is the correct way to declare an adjacency list for a graph in TypeScript?
easy
A. const graph: number = [];
B. const graph: number[][] = [];
C. const graph: string = {};
D. const graph: boolean[] = {};
Solution
Step 1: Identify adjacency list type
An adjacency list is an array of arrays, where each inner array holds connected node indices.
Step 2: Match TypeScript syntax
TypeScript syntax for adjacency list is number[][] for array of number arrays.
Final Answer:
const graph: number[][] = []; -> Option B
Quick Check:
Adjacency list = number[][] [OK]
Hint: Adjacency list is array of arrays [OK]
Common Mistakes:
Using single number type for adjacency list
Using object {} instead of array []
Wrong type annotations
3. Consider the following TypeScript code representing a graph with cycles:
A. It returns false immediately if node is visited, missing cycles.
B. It never marks nodes as visited.
C. It uses wrong data types for visited and stack arrays.
D. It does not check neighbors of the node.
Solution
Step 1: Analyze early return on visited node
Returning false immediately when node is visited skips cycle detection on back edges.
Step 2: Understand correct cycle detection logic
Should only return false if node visited and not in recursion stack; else return true if in stack.
Final Answer:
It returns false immediately if node is visited, missing cycles. -> Option A
Quick Check:
Early return on visited node causes missed cycles [OK]
Hint: Do not return false immediately on visited node [OK]
Common Mistakes:
Returning false too early
Confusing visited and recursion stack
Ignoring recursion stack check
5. You want to model a social network where users can follow each other in any pattern, including mutual follows and cycles. Which data structure best models this, and why can't a tree be used?
hard
A. Use a graph because it supports cycles and multiple connections; trees cannot model cycles or multiple parents.
B. Use a tree because it is simpler and supports cycles naturally.
C. Use a linked list because it models one-way connections well.
D. Use an array because it stores all users efficiently.
Solution
Step 1: Identify social network connection patterns
Users can follow each other in any pattern, including cycles and mutual connections.
Step 2: Compare data structures
Trees cannot represent cycles or multiple parents; graphs can represent any connection pattern.
Final Answer:
Use a graph because it supports cycles and multiple connections; trees cannot model cycles or multiple parents. -> Option A
Quick Check:
Graphs model social networks; trees cannot [OK]
Hint: Social networks need graphs for cycles and mutual links [OK]
Common Mistakes:
Choosing tree for cyclic connections
Confusing linked list with graph
Ignoring cycles in social networks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.