0
0
DSA Typescriptprogramming~30 mins

Adjacency Matrix Representation in DSA Typescript - Build from Scratch

Choose your learning style9 modes available
Adjacency Matrix Representation
📖 Scenario: You are working on a simple social network app. Each person can be friends with others. We want to represent these friendships using a grid where rows and columns show people, and a 1 means they are friends, 0 means not friends.
🎯 Goal: Build an adjacency matrix to show friendships between 4 people named Alice, Bob, Charlie, and Diana. Then, print the matrix to see the friendship connections clearly.
📋 What You'll Learn
Create a 4x4 adjacency matrix with exact values for friendships
Use a variable to store the number of people
Fill the matrix with 1s and 0s to represent friendships
Print the adjacency matrix row by row
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in social networks, maps, and computer networks to show connections between nodes clearly.
💼 Career
Understanding adjacency matrices helps in roles like software development, data analysis, and network engineering where relationships between entities must be managed.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a variable called adjacencyMatrix as a 2D array of numbers with these exact rows: [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0].
DSA Typescript
Hint

Use const adjacencyMatrix: number[][] = [...] to create the 2D array with the exact rows.

2
Add the number of people variable
Create a variable called numPeople and set it to 4 to represent the number of people in the network.
DSA Typescript
Hint

Use const numPeople: number = 4; to store the number of people.

3
Loop through the matrix to print each row
Use a for loop with variable i from 0 to numPeople - 1 to go through each row of adjacencyMatrix. Inside the loop, create a variable rowString that joins the numbers in adjacencyMatrix[i] with spaces.
DSA Typescript
Hint

Use for (let i = 0; i < numPeople; i++) and inside it, const rowString = adjacencyMatrix[i].join(' ').

4
Print the adjacency matrix rows
Inside the for loop, add a console.log(rowString) statement to print each row of the adjacency matrix.
DSA Typescript
Hint

Use console.log(rowString) inside the loop to print each row.