📖 Scenario: Imagine you are managing a small network of 4 computers. You want to represent which computers are directly connected to each other using a simple table.
🎯 Goal: You will build an adjacency matrix in Java to show the connections between these 4 computers. Each cell in the matrix will be 1 if two computers are connected, and 0 if they are not.
📋 What You'll Learn
Create a 4x4 integer matrix called adjMatrix initialized with zeros
Create a variable called numComputers and set it to 4
Set the connections between computers using the adjacency matrix
Print the adjacency matrix in a readable format
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in computer networks to represent direct connections between devices, helping in routing and network analysis.
💼 Career
Understanding adjacency matrices is important for software engineers working with graphs, networks, and algorithms in fields like telecommunications and social network analysis.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a 2D integer array called adjMatrix with 4 rows and 4 columns, initialized with zeros.
DSA Java
Hint
Use new int[4][4] to create a 4 by 4 matrix filled with zeros.
2
Add the number of computers
Create an integer variable called numComputers and set it to 4.
DSA Java
Hint
Declare int numComputers = 4; to store the number of computers.
3
Set connections in the adjacency matrix
Set the following connections in adjMatrix: computer 0 connected to 1 and 2, computer 1 connected to 0 and 3, computer 2 connected to 0, and computer 3 connected to 1. Use adjMatrix[row][column] = 1; to mark connections.
DSA Java
Hint
Use adjMatrix[row][column] = 1; to mark each connection.
4
Print the adjacency matrix
Use nested for loops with variables i and j to print the adjMatrix row by row, separating values with spaces.
DSA Java
Hint
Use two for loops to print each row and column, and System.out.print to print values with spaces.
Practice
(1/5)
1. What does an adjacency matrix represent in graph theory?
easy
A. A 2D array showing edges between nodes
B. A list of nodes in the graph
C. A sequence of visited 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 an edge exists between two nodes (1 for edge, 0 for no edge).
Final Answer:
A 2D array showing edges between nodes -> Option A
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 it up with traversal order
2. Which of the following is the correct way to declare an adjacency matrix for a graph with 4 nodes in Java?
easy
A. int[] matrix = new int[4][4];
B. int matrix = new int[4];
C. int[][] matrix = new int[4][4];
D. int[][] matrix = new int[4];
Solution
Step 1: Recall Java 2D array declaration syntax
In Java, a 2D array is declared as type[][] name = new type[rows][columns];
Step 2: Match with graph nodes count
For 4 nodes, we need a 4x4 matrix, so new int[4][4] is correct.
Final Answer:
int[][] matrix = new int[4][4]; -> Option C
Quick Check:
2D array for 4 nodes = new int[4][4] [OK]
Hint: 2D array syntax: type[][] name = new type[size][size]; [OK]
Common Mistakes:
Using single brackets for 2D array
Declaring 1D array instead of 2D
Incorrect array initialization syntax
3. Given the following adjacency matrix for a graph with 3 nodes:
int[][] matrix = {
{0, 1, 0},
{1, 0, 1},
{0, 1, 0}
}; What is the output of the code snippet?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 1) {
System.out.print(i + "->" + j + " ");
}
}
}