🛤️Praktikum 2

Representasi Graf dengan Matriks

Deskripsi

Pada praktikum 2 ini akan diimplementasikan graph menggunakan matriks untuk merepresentasikan graph adjacency.

Langkah Praktikum

  • Buatlah kelas GraphArray. Tambahkan atribut vertices dan adjacencyMatrix.

public class GraphArray {
    private final int vertices;
    private final int[][] adjacencyMatrix;
  • Tambakan konstruktor untuk kelas GraphArray.

public GraphArray(int vertices) {
    this.vertices = vertices;
    adjacencyMatrix = new int[vertices + 1][vertices + 1];
}
  • Untuk membuat suatu lintasan maka dibuat method makeEdge() sebagai berikut.

public void makeEdge(int source, int destination, int edge) {
    try {
        adjacencyMatrix[source][destination] = edge;
    } catch (ArrayIndexOutOfBoundsException index) {
        System.out.println("The vertices does not exists");
    }
}
  • Untuk menampilkan suatu lintasan diperlukan pembuatan method getEdge() berikut.

public int getEdge(int source, int destination) {
    try {
        return adjacencyMatrix[source][destination];
    } catch (ArrayIndexOutOfBoundsException index) {
        System.out.println("The vertices does not exists");
    }
    return -1;
}
  • Buatlah kelas MainGraphArray untuk menjalankan kelas GraphArray. Tambahkan main method seperti berikut.

public static void main(String[] args) {
    int v,e, count = 1, to = 0, from = 0;
    Scanner sc = new Scanner(System.in);
    GraphArray graph;

    try {
        System.out.println("Enter the number of vertices: ");
        v = sc.nextInt();
        System.out.println("Enter the number of edges: ");
        e = sc.nextInt();

        graph = new GraphArray(v);

        System.out.println("Enter the edges: <to> <from>");
        while (count <= e) {
            to = sc.nextInt();
            from = sc.nextInt();

            graph.makeEdge(to, from, 1);
            count++;
        }

        System.out.println("The adjacency matrix for the given graph is: ");
        System.out.print("  ");
        for (int i = 1; i <= v; i++)
            System.out.print(i + " ");
        System.out.println();

        for (int i = 1; i <= v; i++) {
            System.out.print(i + " ");
            for (int j = 1; j <= v; j++)
                System.out.print(graph.getEdge(i, j) + " ");
            System.out.println();
        }
    } catch (Exception E) {
        System.out.println("Somthing went wrong: "+ E.getMessage());
    }
    sc.close();
}
  • Jalankan kelas MainGraphArray.

  • Amati hasilnya!

Enter the number of vertices: 
5
Enter the number of edges: 
6
Enter the edges: <to> <from>
1 2
1 5
2 3
2 4
2 5
3 4
The adjacency matrix for the given graph is: 
  1 2 3 4 5 
1 0 1 0 0 1 
2 0 0 1 1 1 
3 0 0 0 1 0 
4 0 0 0 0 0 
5 0 0 0 0 0 

Pertanyaan

  1. Apakah perbedaan degree/derajat pada directed dan undirected graph?

  2. Apakah kegunaan method getEdge() ?

  3. Termasuk jenis graph apakah uji coba pada praktikum 2?

  4. Mengapa pada method main harus menggunakan try-catch exception?

  5. Pada implementasi graph menggunakan adjacency matriks. Kenapa jumlah vertices harus ditambahkan dengan 1 pada indeks array berikut?

public GraphArray(int vertices) {
    this.vertices = vertices;
    adjacencyMatrix = new int[vertices + 1][vertices + 1];
}

Last updated