📖 Scenario: You are working with a small network of cities connected by roads. You want to represent which cities are directly connected to each other using a simple table.
🎯 Goal: Create an adjacency matrix to represent connections between cities, then print the matrix to see which cities are connected.
📋 What You'll Learn
Create a 3x3 adjacency matrix with exact values
Add a variable for the number of cities
Use nested loops to fill the adjacency matrix
Print the adjacency matrix row by row
💡 Why This Matters
🌍 Real World
Adjacency matrices are used in network routing, social networks, and mapping connections between entities.
💼 Career
Understanding adjacency matrices is essential for roles in software development, data analysis, and network engineering.
Progress0 / 4 steps
1
Create the adjacency matrix
Create a 3x3 two-dimensional slice of integers called adjMatrix with these exact values: [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
DSA Go
Hint
Use a slice of slices to create a 2D matrix in Go.
2
Add the number of cities variable
Add an integer variable called numCities and set it to 3
DSA Go
Hint
Use numCities := 3 to store the number of cities.
3
Print the adjacency matrix
Use nested for loops with variables i and j to iterate over adjMatrix and print each row as space-separated values
DSA Go
Hint
Use fmt.Print inside the inner loop and fmt.Println after the inner loop to print rows.
4
Display the adjacency matrix
Write a print statement to display the adjacency matrix rows exactly as:
0 1 0
1 0 1
0 1 0
DSA Go
Hint
Use the same nested loops and fmt.Print statements to print the matrix again.
Practice
(1/5)
1. What does an adjacency matrix represent in graph data structures?
easy
A. A 2D array showing connections between nodes
B. A list of nodes with their neighbors
C. A sequence of edges in order
D. A tree structure of 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 there is a connection (edge) between two nodes.
Final Answer:
A 2D array showing connections between nodes -> Option A
Quick Check:
Adjacency matrix = 2D array [OK]
Hint: Adjacency matrix is always a 2D grid of node connections [OK]
Common Mistakes:
Confusing adjacency matrix with adjacency list
Thinking it stores edges as a list
Assuming it represents a tree only
2. Which of the following is the correct way to declare a 3x3 adjacency matrix in Go?
easy
A. var matrix [3][3]int
B. matrix := make([]int, 3)
C. var matrix [][]int = make([][]int, 3)
D. matrix := [3]int{3}
Solution
Step 1: Understand Go array declaration
In Go, a fixed-size 2D array is declared as [rows][cols]type.
Step 2: Check each option
var matrix [3][3]int declares a 3x3 int array correctly. matrix := make([]int, 3) is a 1D slice. var matrix [][]int = make([][]int, 3) is a slice of slices but incomplete. matrix := [3]int{3} is a 1D array with wrong initialization.
Final Answer:
var matrix [3][3]int -> Option A
Quick Check:
Fixed 2D array = [3][3]int [OK]
Hint: Use [rows][cols]type for fixed 2D arrays in Go [OK]
Common Mistakes:
Using slices instead of arrays for fixed size
Incorrect initialization syntax
Confusing 1D and 2D declarations
3. Given the following Go code, what is the printed adjacency matrix?
package main
import "fmt"
func main() {
var graph [3][3]int
graph[0][1] = 1
graph[1][2] = 1
graph[2][0] = 1
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
fmt.Print(graph[i][j], " ")
}
fmt.Println()
}
}
medium
A. 0 0 1
1 0 0
0 1 0
B. 1 0 0
0 1 0
0 0 1
C. 0 1 0
0 0 1
1 0 0
D. 0 1 1
1 0 1
1 1 0
Solution
Step 1: Trace assignments to the matrix
graph[0][1] = 1 means node 0 connects to 1; graph[1][2] = 1 means node 1 connects to 2; graph[2][0] = 1 means node 2 connects to 0.
Step 2: Print matrix row by row
Row 0: 0 1 0; Row 1: 0 0 1; Row 2: 1 0 0.
Final Answer:
0 1 0
0 0 1
1 0 0
-> Option C
Quick Check:
Edges set match matrix rows [OK]
Hint: Map edges to matrix cells row-wise [OK]
Common Mistakes:
Mixing row and column indices
Assuming symmetric matrix for directed graph
Forgetting zero initialization
4. Identify the error in this Go code snippet for creating an adjacency matrix:
var graph [][]int
for i := 0; i < 3; i++ {
graph[i] = make([]int, 3)
}
medium
A. graph should be a 1D slice
B. make([]int, 3) is incorrect syntax
C. Loop should run from 1 to 3
D. graph is not initialized before assignment
Solution
Step 1: Check graph slice initialization
graph is declared but not initialized with length, so graph[i] access causes runtime error.
Step 2: Fix initialization
graph should be initialized with length 3 before assigning inner slices, e.g., graph = make([][]int, 3).
Final Answer:
graph is not initialized before assignment -> Option D
Quick Check:
Initialize outer slice before indexing [OK]
Hint: Initialize slices before indexing to avoid runtime errors [OK]
Common Mistakes:
Forgetting to initialize outer slice
Confusing slice length and capacity
Incorrect loop ranges
5. You have an undirected graph with 4 nodes. Which code snippet correctly updates the adjacency matrix to add an edge between node 1 and node 3?