Discover how a simple table can turn a messy friendship list into a clear, quick-to-check network!
Why Adjacency Matrix Representation in DSA Typescript?
Imagine you want to keep track of friendships in a group of 5 people. You try to write down who is friends with whom on a piece of paper, but it quickly becomes messy and hard to check if two people are friends.
Writing down connections manually is slow and confusing. You might forget some friendships or make mistakes when checking if two people are connected. It's hard to find out who is friends with whom quickly.
An adjacency matrix is like a neat table where each row and column represents a person. If two people are friends, you mark a 1 in the table at their row and column. This makes it easy to see all connections at a glance and check friendships quickly.
const friendships = [['Alice', 'Bob'], ['Bob', 'Eve'], ['Alice', 'Eve']]; function areFriends(name1, name2) { for (const pair of friendships) { if ((pair[0] === name1 && pair[1] === name2) || (pair[1] === name1 && pair[0] === name2)) { return true; } } return false; }
const adjacencyMatrix = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ]; function areFriends(index1, index2) { return adjacencyMatrix[index1][index2] === 1; }
With adjacency matrices, you can quickly check connections and understand the whole network easily.
Social media platforms use adjacency matrices to quickly find if two users are connected or to suggest new friends.
Manual tracking of connections is slow and error-prone.
Adjacency matrix uses a simple table to represent connections clearly.
This method makes checking and managing connections fast and easy.
Practice
Adjacency matrix is a way to show connections between nodes. What does a 1 in the matrix mean?Solution
Step 1: Understand adjacency matrix basics
An adjacency matrix uses rows and columns to represent nodes. Each cell shows if two nodes connect.Step 2: Interpret the value 1 in the matrix
A value of 1 means there is a direct edge between the nodes represented by that row and column.Final Answer:
There is a direct edge between two nodes -> Option DQuick Check:
1 means edge exists [OK]
- Thinking 1 means no edge
- Confusing node identity with connection
- Assuming matrix shows weights always
Solution
Step 1: Check TypeScript array declaration syntax
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; uses explicit 2D array with nested arrays and zeros, which is valid syntax and creates independent rows.Step 2: Identify issues in other options
const matrix: number[3][3] = new Array(3).fill(new Array(3).fill(0)); uses invalid TypeScript syntax for fixed-size arrays. const matrix = Array(3).map(Array(3).fill(0)); passes an array to map() instead of a function, causing a runtime error. const matrix: number[][] = new Array(3).fill(new Array(3).fill(0)); uses fill with the same inner array reference, causing shared rows.Final Answer:
const matrix: number[][] = [[0,0,0],[0,0,0],[0,0,0]]; -> Option AQuick Check:
Explicit nested arrays with zeros [OK]
- Using fill with same inner array causing shared rows
- Passing non-function to map()
- Wrong type annotations
const matrix = [ [0, 1, 0], [0, 0, 1], [1, 0, 0] ]; console.log(matrix[2][0]);
What will be the output?
Solution
Step 1: Locate matrix[2][0]
Row 2 is [1, 0, 0], so element at index 0 is 1.Step 2: Confirm output of console.log
console.log prints the value 1 found at matrix[2][0].Final Answer:
1 -> Option AQuick Check:
matrix[2][0] = 1 [OK]
- Confusing row and column indices
- Expecting zero instead of one
- Thinking index out of range
const size = 3; const matrix = new Array(size).fill(new Array(size).fill(0)); matrix[0][1] = 1; console.log(matrix);
What is the problem with this code?
Solution
Step 1: Understand fill with reference types
Using fill with new Array(size).fill(0) creates one inner array shared by all rows.Step 2: Effect of modifying matrix[0][1]
Changing matrix[0][1] changes the shared inner array, so all rows show the change.Final Answer:
All rows point to the same inner array, so all rows change when one changes -> Option CQuick Check:
Shared inner array causes unexpected updates [OK]
- Assuming fill creates independent arrays
- Ignoring shared reference behavior
- Expecting only one row to change
Solution
Step 1: Check matrix initialization for unique rows
const size = 4; const matrix = Array.from({ length: size }, () => Array(size).fill(0)); matrix[1][3] = 1; matrix[3][1] = 1; uses Array.from with a function to create unique inner arrays, avoiding shared references.Step 2: Verify setting edges for undirected graph
Setting matrix[1][3] and matrix[3][1] to 1 correctly represents an undirected edge.Step 3: Identify issues in other options
Options B and C use fill causing shared inner arrays. const size = 4; const matrix = Array(size).map(() => Array(size).fill(0)); matrix[1][3] = 1; initializes correctly but only sets matrix[1][3] = 1, missing matrix[3][1] = 1 for undirected graph.Final Answer:
const size = 4; const matrix = Array.from({ length: size }, () => Array(size).fill(0)); matrix[1][3] = 1; matrix[3][1] = 1; -> Option BQuick Check:
Use Array.from with function for unique rows [OK]
- Using fill causing shared inner arrays
- Using map on empty array without initialization
- Setting only one direction for undirected edge
Ready to prove it?
Pass the test and this pattern moves unknown
