Advertisement
Yesver08

AdjacencyList.java

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