📖 Scenario: You are working on a simple social network app. Each person can be friends with others. You want to represent these friendships using a matrix where rows and columns represent people, and the value at each position shows if two people are friends.
🎯 Goal: Build an adjacency matrix to represent friendships between 4 people. Then, check if two specific people are friends.
📋 What You'll Learn
Create a 4x4 adjacency matrix with exact friendship connections
Add a variable to select two people to check friendship
Write logic to check if those two people are friends using the matrix
Print the friendship status clearly
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in social networks, maps, and computer networks to represent connections between entities.
💼 Career
Understanding adjacency matrices helps in roles like software development, data analysis, and network engineering where graph data structures are common.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a 2D integer array called adjacencyMatrix with 4 rows and 4 columns. Fill it exactly with these values: 0, 1, 0, 0 1, 0, 1, 1 0, 1, 0, 0 0, 1, 0, 0
DSA C#
Hint
Use a 2D array with 4 rows and 4 columns. Each inner curly braces {} is a row.
2
Select two people to check friendship
Create two integer variables called personA and personB. Set personA to 0 and personB to 1 to represent the first and second person.
DSA C#
Hint
Use two integer variables to hold the indexes of the people you want to check.
3
Check if the two people are friends
Create a boolean variable called areFriends. Set it to true if adjacencyMatrix[personA, personB] equals 1, otherwise false.
DSA C#
Hint
Use the matrix value at [personA, personB] to decide if they are friends.
4
Print the friendship status
Write a Console.WriteLine statement to print exactly: "Person 0 and Person 1 are friends: True" using the areFriends variable.
DSA C#
Hint
Use string interpolation with $"..." to include variables in the printed message.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A queue of nodes to visit
B. A list of nodes in the graph
C. A 2D array showing edges between nodes
D. A stack of visited nodes
Solution
Step 1: Understand adjacency matrix concept
An adjacency matrix is a 2D array where rows and columns represent nodes.
Step 2: Identify what the matrix stores
Each cell shows if an edge exists between two nodes (1 for edge, 0 for no edge).
Final Answer:
A 2D array showing edges between nodes -> Option C
Quick Check:
Adjacency matrix = 2D array of edges [OK]
Hint: Adjacency matrix is always a 2D array showing connections [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of edges
Mixing up matrix with traversal order
2. Which of the following is the correct way to declare an adjacency matrix for 4 nodes in C#?
easy
A. int[] matrix = new int[4];
B. int[,] matrix = new int[4,4];
C. int[][] matrix = new int[4];
D. int matrix = new int[4,4];
Solution
Step 1: Identify correct 2D array syntax in C#
In C#, a 2D array is declared as int[,] with size [rows, columns].
Step 2: Check each option
int[,] matrix = new int[4,4]; uses int[,] with size [4,4], correct for adjacency matrix of 4 nodes.
Final Answer:
int[,] matrix = new int[4,4]; -> Option B
Quick Check:
2D array syntax = int[,] [OK]
Hint: Use int[,] for 2D arrays in C# [OK]
Common Mistakes:
Using jagged array syntax instead of 2D array
Declaring 1D array for adjacency matrix
Missing comma in array declaration
3. Given the following adjacency matrix for a graph with 3 nodes: