📖 Scenario: Imagine you are helping a city planner represent roads between different neighborhoods. Some neighborhoods are connected directly by roads, and some are not. You want to store this information in a way that helps answer questions like "Is there a direct road between neighborhood A and B?" or "Which neighborhoods are connected to neighborhood C?"
🎯 Goal: You will create two ways to represent the connections between neighborhoods: an adjacency list and an adjacency matrix. Then, you will see how to choose which one to use based on the number of connections.
📋 What You'll Learn
Create an adjacency list for 4 neighborhoods with given connections
Create an adjacency matrix for the same neighborhoods and connections
Count the total number of roads (edges) in the graph
Print both adjacency list and adjacency matrix representations
💡 Why This Matters
🌍 Real World
City planners, network engineers, and game developers use adjacency lists and matrices to represent connections between points like roads, computers, or game characters.
💼 Career
Understanding when to use adjacency lists or matrices helps software developers and data scientists efficiently store and process graph data in real-world applications.
Progress0 / 4 steps
1
Create an adjacency list for neighborhoods
Create a variable called adjacencyList as an object where keys are neighborhood names 'A', 'B', 'C', and 'D'. Set the values as arrays of connected neighborhoods exactly as: 'A': ['B', 'C'], 'B': ['A', 'D'], 'C': ['A'], 'D': ['B'].
DSA Typescript
Hint
Think of the adjacency list as a map where each neighborhood points to a list of neighborhoods it connects to directly.
2
Create an adjacency matrix for neighborhoods
Create a variable called adjacencyMatrix as a 2D array of numbers representing connections between neighborhoods 'A', 'B', 'C', and 'D' in order. Use 1 for a direct road and 0 for no road. The matrix should be exactly: [[0,1,1,0],[1,0,0,1],[1,0,0,0],[0,1,0,0]].
DSA Typescript
Hint
Think of the adjacency matrix as a grid where rows and columns represent neighborhoods in order A, B, C, D. Put 1 if connected, else 0.
3
Count total roads using adjacency list
Create a variable called totalRoads and set it to 0. Use a for loop with variables neighborhood and connections to iterate over Object.entries(adjacencyList). For each connections array, add its length to totalRoads. After the loop, divide totalRoads by 2 to avoid double counting roads.
DSA Typescript
Hint
Each road is counted twice in the adjacency list (once from each neighborhood), so divide by 2 at the end.
4
Print adjacency list, matrix, and total roads
Use console.log to print the adjacencyList, then the adjacencyMatrix, and finally the totalRoads variable. Print each on its own line.
DSA Typescript
Hint
Use console.log three times to print each variable on its own line.
Practice
(1/5)
1. Which data structure is generally more space-efficient for representing a graph with very few edges compared to the number of vertices?
easy
A. Adjacency List
B. Adjacency Matrix
C. Edge List
D. Incidence Matrix
Solution
Step 1: Understand graph density
Sparse graphs have few edges compared to vertices, so storing all possible edges wastes space.
Step 2: Compare space usage of adjacency list and matrix
Adjacency list stores only existing edges, using less space for sparse graphs. Adjacency matrix always uses space for all possible edges.
Final Answer:
Adjacency List -> Option A
Quick Check:
Sparse graph = Adjacency List [OK]
Hint: Sparse graph? Use adjacency list for less space [OK]
Common Mistakes:
Choosing adjacency matrix for sparse graphs
Confusing edge count with vertex count
Ignoring space complexity differences
2. Which of the following is the correct TypeScript syntax to declare an adjacency matrix for a graph with 3 vertices initialized with zeros?
easy
A. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0));
B. const matrix: number[] = [0,0,0];
C. const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]];
D. const matrix: number[][] = Array(3).map(() => Array(3).fill(0));
Solution
Step 1: Understand adjacency matrix type
An adjacency matrix is a 2D array (array of arrays) of numbers representing edges.
Step 2: Check initialization correctness
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; explicitly creates a 3x3 matrix with zeros. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0)); uses fill with the same inner array reference, causing bugs. const matrix: number[][] = Array(3).map(() => Array(3).fill(0)); uses map on empty array, which won't work as expected. const matrix: number[] = [0,0,0]; is 1D array, not matrix.
Final Answer:
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; -> Option C
Hint: Avoid fill with nested arrays; use map to create separate arrays [OK]
Common Mistakes:
Thinking fill creates separate inner arrays
Ignoring shared reference problem
Assuming matrix is 1D array
5. You need to represent a dense graph with 1000 vertices where you frequently check if an edge exists between any two vertices. Which representation is best and why?
hard
A. Adjacency List, because it uses less space for dense graphs
B. Adjacency Matrix, because it allows O(1) edge existence checks
C. Edge List, because it is fastest for dense graphs
D. Incidence Matrix, because it uses less space than adjacency matrix
Solution
Step 1: Understand graph density and operation needs
Dense graph means many edges; checking edge existence frequently requires fast lookup.
Step 2: Compare adjacency list and matrix for edge checks
Adjacency matrix allows direct O(1) access to check if edge exists between two vertices. Adjacency list requires searching neighbors, which can be slower in dense graphs.
Step 3: Consider space tradeoff
Though adjacency matrix uses more space, for dense graphs this is acceptable and beneficial for speed.
Final Answer:
Adjacency Matrix, because it allows O(1) edge existence checks -> Option B