Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package praktikum8;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class AdjacencyList {
- 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 List
- LinkedList[] adjacencyList = new LinkedList[jumlahVertex];
- for (int i = 0; i < jumlahVertex; i++) {
- adjacencyList[i] = new LinkedList<Integer>();
- }
- // 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();
- adjacencyList[a - 1].add(b);
- adjacencyList[b - 1].add(a);
- }
- // Cetak Adjacency List
- System.out.println("Adjacency List: ");
- for (int i = 0; i < jumlahVertex; i++) {
- System.out.println((i + 1) + " -> " + adjacencyList[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement