📖 Scenario: You are working on a simple social network where each person can be friends with others. We 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 store the number of people
Write code to check if person 1 and person 3 are friends
Print the result as 1 (friends) or 0 (not friends)
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in social networks, maps, and computer networks to represent connections clearly and quickly.
💼 Career
Understanding adjacency matrices helps in roles involving graph algorithms, network analysis, and data structure optimization.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a 2D integer array called adjMatrix with 4 rows and 4 columns. Initialize it with these exact values: row 0: {0, 1, 0, 0}, row 1: {1, 0, 1, 1}, row 2: {0, 1, 0, 0}, row 3: {0, 1, 0, 0}.
DSA C++
Hint
Use a 2D array with 4 rows and 4 columns. Each row is enclosed in curly braces with commas separating values.
2
Add the number of people variable
Create an integer variable called numPeople and set it to 4.
DSA C++
Hint
Just create a simple integer variable and assign 4 to it.
3
Check friendship between person 1 and person 3
Create an integer variable called areFriends and set it to the value in adjMatrix[1][3].
DSA C++
Hint
Access the matrix at row 1 and column 3 to get the friendship value.
4
Print the friendship result
Write a std::cout statement to print the value of areFriends.
DSA C++
Hint
Use std::cout << areFriends << std::endl; to print the result.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A sequence of visited nodes
B. A list of nodes in the graph
C. A 2D array showing connections between nodes
D. A stack used for graph traversal
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 there is a connection (edge) between two nodes.
Final Answer:
A 2D array showing connections between nodes -> Option C
Quick Check:
Adjacency matrix = 2D array of connections [OK]
Hint: Adjacency matrix = grid showing edges between nodes [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores node values instead of edges
Mixing it up with traversal order
2. Which of the following is the correct way to declare a 4x4 adjacency matrix in C++?
easy
A. int matrix(4,4);
B. int matrix[][] = new int[4][4];
C. int matrix[4];
D. int matrix[4][4];
Solution
Step 1: Recall C++ array declaration syntax
In C++, a 2D array is declared as type name[size1][size2].
Step 2: Check each option
int matrix[4][4]; uses correct syntax: int matrix[4][4]; int matrix(4,4); is invalid syntax, C is 1D array, D is invalid in C++.
Final Answer:
int matrix[4][4]; -> Option D
Quick Check:
2D array declaration = int matrix[4][4]; [OK]
Hint: Use int name[rows][cols] for 2D arrays in C++ [OK]
Common Mistakes:
Using parentheses instead of brackets
Declaring only one dimension
Trying Java-style new keyword in C++
3. Given the following adjacency matrix for a graph with 3 nodes:
Write a function in C++ that returns true if there is an edge from node u to node v, false otherwise. Which of these is correct?
hard
A. bool hasEdge(int u, int v) { return graph[v][u] == 1; }
B. bool hasEdge(int u, int v) { return graph[u][v] == 1; }
C. bool hasEdge(int u, int v) { return graph[u][v] == 0; }
D. bool hasEdge(int u, int v) { return graph[u] == graph[v]; }
Solution
Step 1: Understand adjacency matrix indexing for edges
graph[u][v] == 1 means edge from node u to node v exists.
Step 2: Check each function option
bool hasEdge(int u, int v) { return graph[u][v] == 1; } correctly returns true if edge exists. bool hasEdge(int u, int v) { return graph[v][u] == 1; } reverses indices, C checks for no edge, D compares rows incorrectly.
Final Answer:
bool hasEdge(int u, int v) { return graph[u][v] == 1; } -> Option B
Quick Check:
Check graph[u][v] == 1 for edge presence [OK]
Hint: Check graph[u][v] == 1 for edge from u to v [OK]
Common Mistakes:
Swapping u and v indices
Checking for zero instead of one
Comparing entire rows instead of single cell
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.