Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package latihangraph;
- import java.util.Scanner;
- /**
- *
- * @author faldi
- */
- public class LatihanGraph {
- public final int MAKS_VERTS = 30;
- private Vertex vertexList[];
- public int adjMat[][];
- public int nVerts;
- public LatihanGraph() {
- vertexList = new Vertex[MAKS_VERTS];
- adjMat = new int[MAKS_VERTS][MAKS_VERTS];
- nVerts = 0;
- for(int j = 0;j<MAKS_VERTS;j++) {
- for(int k = 0;k<MAKS_VERTS;k++) {
- adjMat[j][k] = 0;
- }
- }
- }
- public void addVertex(String lab) {
- vertexList[nVerts++] = new Vertex(lab);
- }
- public void addEdge(int start, int end) {
- adjMat[start][end] = 1;
- adjMat[end][start] = 1;
- }
- public void displayVertex(int v) {
- System.out.print(vertexList[v].label);
- }
- public void displayEdge(int v1, int v2) {
- System.out.print("Vertex Start = " + v1 + "\nVertex End = " + v2 + "\n");
- System.out.print(vertexList[v1].label);
- System.out.print(" -----> ");
- System.out.print(vertexList[v2].label);
- }
- public void displayAdj(int x, int v) {
- System.out.print(adjMat[x][v]);
- }
- class Vertex {
- public String label;
- public boolean wasVisited;
- public Vertex(String lab) {
- label = lab;
- wasVisited = false;
- }
- }
- public static void main(String[] args) {
- LatihanGraph theGraph = new LatihanGraph();
- theGraph.addVertex("Andhika");
- theGraph.addVertex("Ega");
- theGraph.addVertex("Novita");
- theGraph.addVertex("Bagus");
- theGraph.addVertex("Raka");
- theGraph.addVertex("Nadhia");
- theGraph.addEdge(0, 1);
- theGraph.addEdge(0, 3);
- theGraph.addEdge(1, 3);
- theGraph.addEdge(1, 2);
- theGraph.addEdge(2, 4);
- theGraph.addEdge(3, 4);
- theGraph.addEdge(4, 5);
- System.out.println("Nama-nama Vertex : ");
- System.out.print("index 0 : ");
- theGraph.displayVertex(0);
- System.out.print("\nindex 1 : ");
- theGraph.displayVertex(1);
- System.out.print("\nindex 2 : ");
- theGraph.displayVertex(2);
- System.out.print("\nindex 3 : ");
- theGraph.displayVertex(3);
- System.out.print("\nindex 4 : ");
- theGraph.displayVertex(4);
- System.out.print("\nindex 5 : ");
- theGraph.displayVertex(5);
- System.out.println("\n-----------------------");
- LatihanGraph a = new LatihanGraph();
- Scanner s = new Scanner(System.in);
- System.out.print("1. Input Vertex Index = ");
- int v1;
- v1 = s.nextInt();
- System.out.print("2. Input Vertex Index = ");
- int v2;
- v2 = s.nextInt();
- System.out.print("\n");
- theGraph.displayEdge(v1, v2);
- System.out.print("\n\nAdjecency Matriks adalah = ");
- theGraph.displayAdj(v1, v2);
- System.out.print("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement