Bird
Raised Fist0
DSA Javascriptprogramming~30 mins

Adjacency Matrix Representation in DSA Javascript - Build from Scratch

Choose your learning style10 modes available
Adjacency Matrix Representation
📖 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

  1. Step 1: Understand adjacency matrix structure

    An adjacency matrix is a 2D array where rows and columns represent nodes.
  2. Step 2: Identify what the matrix stores

    Each cell shows if there is a connection (edge) between two nodes.
  3. Final Answer:

    A 2D array showing connections between nodes -> Option A
  4. 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

  1. Step 1: Understand adjacency matrix as 2D array

    It must be an array of arrays, each inner array representing a row.
  2. 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.
  3. Final Answer:

    const matrix = [[0,0,0],[0,0,0],[0,0,0]]; -> Option B
  4. Quick Check:

    2D array syntax = const matrix = [[0,0,0],[0,0,0],[0,0,0]]; [OK]
Hint: Adjacency matrix needs nested arrays for rows and columns [OK]
Common Mistakes:
  • Using a single array instead of 2D array
  • Using object instead of array
  • Creating array without inner arrays
3. What is the output of the following code?
const matrix = [
  [0, 1, 0],
  [1, 0, 1],
  [0, 1, 0]
];
console.log(matrix[1][2]);
medium
A. 1
B. 0
C. undefined
D. Error

Solution

  1. Step 1: Identify indices in matrix

    matrix[1] is the second row: [1, 0, 1].
  2. Step 2: Access element at column 2

    matrix[1][2] is the third element in second row, which is 1.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    matrix[1][2] = 1 [OK]
Hint: Remember matrix[row][column] indexing starts at 0 [OK]
Common Mistakes:
  • Confusing row and column indices
  • Assuming 1-based indexing
  • Expecting undefined for valid indices
4. Find the error in this adjacency matrix update code:
const matrix = [
  [0, 0],
  [0, 0]
];
matrix[0][1] = 1;
matrix[1][2] = 1;
medium
A. Matrix should be 1D array
B. Cannot assign value to matrix[0][1]
C. Index 2 is out of bounds for matrix[1]
D. No error, code is correct

Solution

  1. Step 1: Check matrix dimensions

    Matrix has 2 rows and 2 columns (indices 0 and 1 only).
  2. Step 2: Identify invalid index access

    matrix[1][2] tries to access third element in second row, which does not exist.
  3. Final Answer:

    Index 2 is out of bounds for matrix[1] -> Option C
  4. Quick Check:

    Accessing invalid column index = Error [OK]
Hint: Check array length before accessing indices [OK]
Common Mistakes:
  • Ignoring array size limits
  • Assuming dynamic resizing on assignment
  • Confusing row and column indices
5. Given a graph with 4 nodes, how would you represent an undirected edge between node 1 and node 3 in an adjacency matrix?
hard
A. Set matrix[1][1] = 1 and matrix[3][3] = 1
B. Set matrix[1][3] = 1 only
C. Set matrix[3][1] = 1 only
D. Set matrix[1][3] = 1 and matrix[3][1] = 1

Solution

  1. Step 1: Understand undirected edge representation

    Undirected edges are symmetric, so both directions must be marked.
  2. Step 2: Mark both connections in matrix

    Set matrix[1][3] = 1 and matrix[3][1] = 1 to show connection both ways.
  3. Final Answer:

    Set matrix[1][3] = 1 and matrix[3][1] = 1 -> Option D
  4. Quick Check:

    Undirected edge = symmetric matrix entries [OK]
Hint: Undirected edges need two symmetric entries set [OK]
Common Mistakes:
  • Setting only one direction for undirected edge
  • Setting diagonal elements instead of edge
  • Confusing node indices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard