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 neighbors. 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 for each node in lists, saving space for sparse graphs. An adjacency matrix uses a 2D array showing connections between all pairs, using more space.
Click to reveal answer
beginner
In JavaScript, which data structure is commonly used to implement an adjacency list?
A JavaScript object or Map is used, where keys are node labels and values are arrays (lists) of neighbors.
Click to reveal answer
intermediate
What is the time complexity to find all neighbors of a node using an adjacency list?
It is O(k), where k is the number of neighbors of that node, because you directly access the list of neighbors.
Click to reveal answer
beginner
Why is adjacency list preferred for sparse graphs?
Because it only stores existing edges, saving memory compared to adjacency matrix which stores all possible edges even if many are missing.
Click to reveal answer
What does an adjacency list store for each node?
AA matrix of all nodes
BThe weight of the node
CThe total number of nodes
DA list of its neighbors
✗ Incorrect
An adjacency list stores a list of neighbors for each node.
Which data structure is best for representing a graph with few edges?
AAdjacency matrix
BArray
CAdjacency list
DStack
✗ Incorrect
Adjacency list is efficient for sparse graphs with few edges.
In JavaScript, what is a common way to store an adjacency list?
AObject with arrays as values
B2D array only
CString of nodes
DNumber of edges
✗ Incorrect
An object with arrays as values stores neighbors for each node.
What is the time complexity to check if node A is connected to node B using adjacency list?
AO(1)
BO(k) where k is neighbors of A
CO(n^2)
DO(n)
✗ Incorrect
You check node A's neighbors list, which takes O(k) time.
Which of these is NOT true about adjacency lists?
AThey cannot represent directed graphs
BThey store neighbors for each node
CThey use more memory than adjacency matrix for dense graphs
DThey save space for sparse graphs
✗ Incorrect
Adjacency lists can represent directed graphs by storing neighbors accordingly.
Explain how an adjacency list represents a graph and why it is useful.
Think about how you would list friends for each person in a group.
You got /4 concepts.
Describe how you would implement an adjacency list in JavaScript for a graph with nodes labeled as strings.
Imagine a phone book where each name has a list of contacts.
You got /4 concepts.
Practice
(1/5)
1. What does an adjacency list represent in a graph data structure?
easy
A. A list where each node stores its neighbors
B. A matrix showing connections between nodes
C. A list of all nodes without connections
D. A list of edges sorted by weight
Solution
Step 1: Understand adjacency list concept
An adjacency list stores each node with a list of its neighbors or connected nodes.
Step 2: Compare with other graph representations
An adjacency matrix is a 2D array, not a list of neighbors. Other options describe different structures.
Final Answer:
A list where each node stores its neighbors -> Option A
Quick Check:
Adjacency list = neighbors list [OK]
Hint: Adjacency list always stores neighbors for each node [OK]
Common Mistakes:
Confusing adjacency list with adjacency matrix
Thinking it stores edges sorted by weight
Assuming it lists nodes without connections only
2. Which of the following is the correct JavaScript syntax to initialize an empty adjacency list for a graph?
easy
A. const graph = [];
B. const graph = new Map();
C. const graph = {};
D. const graph = new Set();
Solution
Step 1: Identify suitable data structure for adjacency list
An adjacency list can be implemented using a Map to store nodes and their neighbors efficiently.
Step 2: Check syntax correctness
new Map() creates an empty map. Arrays or objects can be used but Map is preferred for graph keys. Set is for unique values, not key-value pairs.
Final Answer:
const graph = new Map(); -> Option B
Quick Check:
Use Map() for adjacency list initialization [OK]
Hint: Use Map() to store nodes and neighbors in adjacency list [OK]
Common Mistakes:
Using Set which does not store key-value pairs
Using empty array which is less clear for key-value
Using empty object but Map is better for keys
3. Given the following adjacency list representation in JavaScript, what will be the output of console.log(graph.get('A'));?
Assuming graph.get returns undefined if key exists
Thinking push cannot modify array in Map
Confusing push with set method
5. You want to represent a directed graph with nodes 'X', 'Y', 'Z' where edges are X->Y, Y->Z, and Z->X. Which adjacency list correctly represents this graph in JavaScript?
hard
A. const graph = new Map([['X', ['Y']], ['Y', ['Z']], ['Z', ['X']]]);
B. const graph = new Map([['X', ['Y', 'Z']], ['Y', ['X']], ['Z', ['Y']]]);
C. const graph = new Map([['X', ['Z']], ['Y', ['X']], ['Z', ['Y']]]);
D. const graph = new Map([['X', ['Y']], ['Y', ['X', 'Z']], ['Z', []]]);
Solution
Step 1: Understand directed edges
Edges are X->Y, Y->Z, Z->X, so each node points only to its direct neighbors.