📖 Scenario: You are working on a simple social network app. You want to represent friendships between users. Some users have many friends, others have only a few. You want to learn how to store these friendships efficiently and understand when to use different methods.
🎯 Goal: Build two ways to represent friendships between 5 users: one using an adjacency list and one using an adjacency matrix. Then, decide which method is better for this social network.
📋 What You'll Learn
Create an adjacency list for 5 users with specific friendships
Create an adjacency matrix for the same 5 users with the same friendships
Add a variable to count total friendships
Write code to count friendships from the adjacency list
Print the adjacency list, adjacency matrix, and total friendships
💡 Why This Matters
🌍 Real World
Social networks, recommendation systems, and maps often use adjacency lists or matrices to represent connections between entities.
💼 Career
Understanding these structures helps in roles like software engineering, data science, and network analysis where graph data is common.
Progress0 / 4 steps
1
Create the adjacency list
Create a variable called adjList as an object representing friendships between 5 users (1 to 5). Use these exact friendships: 1 is friends with 2 and 3, 2 is friends with 1 and 4, 3 is friends with 1, 4 is friends with 2 and 5, 5 is friends with 4.
DSA Javascript
Hint
Use an object with keys 1 to 5. Each key has an array of friends.
2
Create the adjacency matrix
Create a variable called adjMatrix as a 2D array (array of arrays) representing the same friendships for users 1 to 5. Use 1 for friendship and 0 for no friendship. The rows and columns represent users 1 to 5 in order.
DSA Javascript
Hint
Create a 5x5 array with 1s where users are friends and 0s elsewhere.
3
Count total friendships using adjacency list
Create a variable called totalFriendships and set it to 0. Then use a for loop with variables user and friends to iterate over Object.entries(adjList). Inside the loop, add the length of friends to totalFriendships.
DSA Javascript
Hint
Use Object.entries to get user and friends array, then add friends.length to totalFriendships.
4
Print the results
Write three console.log statements to print the adjList, adjMatrix, and totalFriendships variables in that order.
DSA Javascript
Hint
Use console.log to print each variable on its own line.
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.