Advertisement
Yesver08

AdjacencyMatrix.java

Nov 12th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package praktikum8;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class AdjacencyMatrix {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         // Input jumlah Vertex
  10.         System.out.print("Masukkan jumlah vertex: ");
  11.         int jumlahVertex = sc.nextInt();
  12.  
  13.         // Inisialisasi Adjacency Matrix
  14.         int[][] adjacencyMatrix = new int[jumlahVertex][jumlahVertex];
  15.  
  16.         // Input jumlah Edge
  17.         System.out.print("Masukkan jumlah edge: ");
  18.         int jumlahEdge = sc.nextInt();
  19.  
  20.         // Input daftar Edge
  21.         for (int i = 0; i < jumlahEdge; i++) {
  22.             System.out.print("Masukkan edge ke-" + (i + 1) + ": ");
  23.             int a = sc.nextInt();
  24.             int b = sc.nextInt();
  25.             adjacencyMatrix[a - 1][b - 1] = 1;
  26.             adjacencyMatrix[b - 1][a - 1] = 1;
  27.         }
  28.  
  29.         // Cetak Adjacency Matrix
  30.         System.out.println("Adjacency Matrix: ");
  31.         for (int i = 0; i < jumlahVertex; i++) {
  32.             for (int j = 0; j < jumlahVertex; j++) {
  33.                 System.out.print(adjacencyMatrix[i][j]);
  34.             }
  35.             System.out.println();
  36.         }
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement