-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_matrix.java
More file actions
32 lines (27 loc) · 1.13 KB
/
input_matrix.java
File metadata and controls
32 lines (27 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
public class input_matrix {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input number of vertices
System.out.print("Enter the number of vertices: ");
int numberOfVertices = scanner.nextInt();
// Initialize the adjacency matrix
int[][] adjacencyMatrix = new int[numberOfVertices][numberOfVertices];
// Input edges
System.out.println("Enter the adjacency matrix:");
for (int i = 0; i < numberOfVertices; i++) {
for (int j = 0; j < numberOfVertices; j++) {
System.out.print("Edge from " + i + " to " + j + " (0 if no edge, weight otherwise): ");
adjacencyMatrix[i][j] = scanner.nextInt();
}
}
// Print the adjacency matrix
System.out.println("\nAdjacency Matrix:");
for (int i = 0; i < numberOfVertices; i++) {
for (int j = 0; j < numberOfVertices; j++) {
System.out.print(adjacencyMatrix[i][j] + " ");
}
System.out.println();
}
}
}