What is an adjacency list in graph representation?
An adjacency list stores each node with a list of its connected neighbors. It is like a list of friends for each person in a social network.
Click to reveal answer
beginner
What is an adjacency matrix in graph representation?
An adjacency matrix is a 2D grid where rows and columns represent nodes. A cell is marked if there is a connection between the nodes at that row and column.
Click to reveal answer
intermediate
When is it better to use an adjacency list over a matrix?
Use an adjacency list when the graph has fewer edges (sparse graph). It saves space and is faster to find neighbors.
Click to reveal answer
intermediate
When should you choose an adjacency matrix?
Choose an adjacency matrix when the graph is dense (many edges) or when you need to quickly check if two nodes are connected.
Click to reveal answer
intermediate
What is a key difference in memory use between adjacency list and matrix?
Adjacency lists use memory proportional to edges, so less for sparse graphs. Matrices always use memory proportional to the square of nodes, which can be large.
Click to reveal answer
Which graph representation is more memory efficient for a graph with 1000 nodes and 10,000 edges?
ADepends on node values
BAdjacency matrix
CBoth use the same memory
DAdjacency list
✗ Incorrect
Adjacency list uses memory proportional to edges, which is less for sparse graphs like this.
Which representation allows faster check if two nodes are directly connected?
AAdjacency list
BAdjacency matrix
CBoth are equally fast
DDepends on graph size
✗ Incorrect
Adjacency matrix allows O(1) time to check connection by looking at a cell.
For a dense graph, which representation is generally preferred?
AAdjacency matrix
BNone
CEdge list
DAdjacency list
✗ Incorrect
Adjacency matrix is better for dense graphs because it stores all connections explicitly.
Which representation is easier to iterate over all neighbors of a node?
ABoth are equally easy
BAdjacency matrix
CAdjacency list
DDepends on node degree
✗ Incorrect
Adjacency list directly stores neighbors, making iteration simple and efficient.
If you want to add or remove edges frequently, which representation is better?
AAdjacency list
BAdjacency matrix
CBoth are equally good
DDepends on number of nodes
✗ Incorrect
Adjacency lists allow easier and faster updates for adding or removing edges.
Explain when and why you would choose an adjacency list over an adjacency matrix for graph representation.
Think about graphs with few connections compared to nodes.
You got /4 concepts.
Describe the advantages of using an adjacency matrix and situations where it is preferred.
Consider graphs with many edges and quick lookups.
You got /4 concepts.
Practice
(1/5)
1. Which data structure is generally better for representing a graph with many nodes but few edges?
easy
A. Adjacency Matrix
B. Adjacency List
C. Array
D. Stack
Solution
Step 1: Understand graph density
A graph with many nodes but few edges is called a sparse graph.
Step 2: Choose efficient representation for sparse graphs
Adjacency lists store only existing edges, saving space for sparse graphs.
Final Answer:
Adjacency List -> Option B
Quick Check:
Sparse graph = Adjacency List [OK]
Hint: Sparse graphs use adjacency lists for space efficiency [OK]
Common Mistakes:
Choosing adjacency matrix for sparse graphs wastes space
Confusing adjacency list with adjacency matrix
Thinking arrays or stacks represent graphs directly
2. Which of the following is the correct syntax to initialize an empty adjacency list for a graph with 3 nodes in JavaScript?
easy
A. const graph = [[], [], []];
B. const graph = [[0,0,0],[0,0,0],[0,0,0]];
C. const graph = new Array(3).fill([]);
D. const graph = {};
Solution
Step 1: Understand adjacency list structure
An adjacency list is an array where each element is an array of neighbors.
Step 2: Check each option
const graph = [[], [], []]; creates separate empty arrays for each node. const graph = new Array(3).fill([]); fills with the same array reference, causing bugs.