What is an adjacency list in graph representation?
An adjacency list is a way to represent a graph where each node stores a list of its neighboring nodes. It shows which nodes are directly connected to each node.
Click to reveal answer
intermediate
How does an adjacency list differ from an adjacency matrix?
An adjacency list stores neighbors only, using less space for sparse graphs. An adjacency matrix uses a 2D array to store connections, using more space but allowing faster edge checks.
Click to reveal answer
beginner
Show the adjacency list for a graph with edges: (1-2), (1-3), (2-4).
1: [2, 3] 2: [1, 4] 3: [1] 4: [2]
Click to reveal answer
intermediate
Why is adjacency list preferred for sparse graphs?
Because it only stores existing edges, saving memory and making it faster to iterate neighbors compared to adjacency matrix which stores all possible edges.
Click to reveal answer
beginner
What data structure is commonly used to implement adjacency lists in Python?
A dictionary where keys are nodes and values are lists of neighbors is commonly used for adjacency lists in Python.
Click to reveal answer
Which data structure best represents an adjacency list?
A2D array with 0s and 1s
BDictionary with lists of neighbors
CSingle list of all edges
DStack of nodes
✗ Incorrect
An adjacency list uses a dictionary (or map) where each node points to a list of its neighbors.
What is the main advantage of adjacency list over adjacency matrix?
AUses less memory for sparse graphs
BFaster to check if edge exists
CSimpler to implement
DAlways uses fixed size
✗ Incorrect
Adjacency lists save memory by storing only existing edges, which is efficient for sparse graphs.
In an undirected graph, if node 1 is connected to node 2, how is this represented in adjacency list?
ANeither list contains the other
BOnly 1's list contains 2
C1's list contains 2 and 2's list contains 1
DOnly 2's list contains 1
✗ Incorrect
In undirected graphs, edges are stored in both nodes' adjacency lists.
What is the time complexity to find all neighbors of a node in adjacency list?
AO(number of edges squared)
BO(number of nodes)
CO(1)
DO(number of neighbors)
✗ Incorrect
Neighbors are stored in a list, so accessing all neighbors takes time proportional to their count.
Which graph type benefits most from adjacency list representation?
ASparse graphs
BComplete graphs
CGraphs with no edges
DGraphs with equal edges and nodes
✗ Incorrect
Sparse graphs have fewer edges, so adjacency lists save space by storing only existing edges.
Explain how to represent a graph using an adjacency list and why it is useful.
Think about how you store friends for each person in a group.
You got /3 concepts.
Describe the difference between adjacency list and adjacency matrix with examples.
Compare a contact list vs a full table of connections.
You got /3 concepts.
Practice
(1/5)
1. What does an adjacency list represent in graph data structures?
easy
A. A list of all nodes without connections
B. A matrix showing distances between nodes
C. A list of nodes sorted by their values
D. A list where each node stores its connected neighbors
Solution
Step 1: Understand adjacency list concept
An adjacency list stores each node with a list of nodes it connects to directly.
Step 2: Compare options with definition
Only A list where each node stores its connected neighbors correctly describes this structure as neighbors stored per node.
Final Answer:
A list where each node stores its connected neighbors -> Option D
Quick Check:
Adjacency list = neighbors per node [OK]
Hint: Adjacency list = nodes with their direct neighbors [OK]
Common Mistakes:
Confusing adjacency list with adjacency matrix
Thinking it stores distances instead of neighbors
Assuming it lists nodes without connections
2. Which of the following is the correct Python syntax to initialize an empty adjacency list for a graph with 3 nodes?
easy
A. graph = {0: [], 1: [], 2: []}
B. graph = [[], [], []]
C. graph = {0: None, 1: None, 2: None}
D. graph = [0, 1, 2]
Solution
Step 1: Understand adjacency list initialization
An adjacency list is usually a dictionary with keys as nodes and values as lists of neighbors.
Step 2: Check each option
graph = {0: [], 1: [], 2: []} correctly creates a dict with empty lists for each node. graph = [[], [], []] is a list of lists but lacks node keys. graph = {0: None, 1: None, 2: None} uses None instead of lists. graph = [0, 1, 2] is just a list of nodes, not adjacency lists.
Final Answer:
graph = {0: [], 1: [], 2: []} -> Option A
Quick Check:
Dict with empty lists = adjacency list init [OK]
Hint: Use dict with nodes as keys and empty lists as values [OK]