📖 Scenario: You are working on a simple social network app. You want to represent friendships between 4 friends using a matrix. Each friend is a node, and a friendship is a connection between nodes.
🎯 Goal: Create an adjacency matrix to show which friends are connected. Then, print the matrix to see the connections clearly.
📋 What You'll Learn
Create a 4x4 adjacency matrix named adjMatrix with exact values
Add a variable size to store the number of friends (4)
Use a nested for loop with variables i and j to print the matrix row by row
Print each row as a space-separated string of 0s and 1s
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in social networks, maps, and computer networks to show connections between points.
💼 Career
Understanding adjacency matrices helps in roles like software development, data analysis, and network engineering where relationships between entities matter.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a 4x4 adjacency matrix called adjMatrix with these exact rows: [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]
DSA Javascript
Hint
Use a constant named adjMatrix and assign a 2D array with the exact rows shown.
2
Add the size variable
Create a constant called size and set it to 4 to represent the number of friends.
DSA Javascript
Hint
Use const size = 4; to store the number of friends.
3
Print the adjacency matrix
Use a nested for loop with variables i and j to iterate over adjMatrix and print each row as a space-separated string of 0s and 1s.
DSA Javascript
Hint
Use two for loops: outer with i, inner with j. Build a string for each row and print it.
4
Print the final adjacency matrix output
Run the program to print the adjacency matrix rows exactly as space-separated 0s and 1s.
DSA Javascript
Hint
Check the console output matches the exact rows of the adjacency matrix.
Practice
(1/5)
1. What does an adjacency matrix represent in a graph?
easy
A. A 2D array showing connections between nodes
B. A list of nodes in the graph
C. A sequence of edges in order
D. A tree structure of nodes
Solution
Step 1: Understand adjacency matrix structure
An adjacency matrix is a 2D array where rows and columns represent nodes.
Step 2: Identify what the matrix stores
Each cell shows if there is a connection (edge) between two nodes.
Final Answer:
A 2D array showing connections between nodes -> Option A
Quick Check:
Adjacency matrix = 2D array of connections [OK]
Hint: Adjacency matrix is always a 2D array of node links [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of connections
Assuming it is a sequence or tree
2. Which of the following is the correct way to declare a 3x3 adjacency matrix in JavaScript?
easy
A. const matrix = [0,0,0];
B. const matrix = [[0,0,0],[0,0,0],[0,0,0]];
C. const matrix = new Array(3);
D. const matrix = {0:0,1:0,2:0};
Solution
Step 1: Understand adjacency matrix as 2D array
It must be an array of arrays, each inner array representing a row.
Step 2: Check each option's structure
const matrix = [[0,0,0],[0,0,0],[0,0,0]]; is a 3x3 array filled with zeros, correct for adjacency matrix initialization.
Final Answer:
const matrix = [[0,0,0],[0,0,0],[0,0,0]]; -> Option B