Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package praktikum8;
- import java.util.Scanner;
- public class AdjacencyMatrix {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // Input jumlah Vertex
- System.out.print("Masukkan jumlah vertex: ");
- int jumlahVertex = sc.nextInt();
- // Inisialisasi Adjacency Matrix
- int[][] adjacencyMatrix = new int[jumlahVertex][jumlahVertex];
- // Input jumlah Edge
- System.out.print("Masukkan jumlah edge: ");
- int jumlahEdge = sc.nextInt();
- // Input daftar Edge
- for (int i = 0; i < jumlahEdge; i++) {
- System.out.print("Masukkan edge ke-" + (i + 1) + ": ");
- int a = sc.nextInt();
- int b = sc.nextInt();
- adjacencyMatrix[a - 1][b - 1] = 1;
- adjacencyMatrix[b - 1][a - 1] = 1;
- }
- // Cetak Adjacency Matrix
- System.out.println("Adjacency Matrix: ");
- for (int i = 0; i < jumlahVertex; i++) {
- for (int j = 0; j < jumlahVertex; j++) {
- System.out.print(adjacencyMatrix[i][j]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement