Advertisement
Faldi767

LatihanGraph

Nov 11th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package latihangraph;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author faldi
  13.  */
  14. public class LatihanGraph {
  15.     public final int MAKS_VERTS = 30;
  16.     private Vertex vertexList[];
  17.     public int adjMat[][];
  18.     public int nVerts;
  19.     public LatihanGraph() {
  20.         vertexList = new Vertex[MAKS_VERTS];
  21.         adjMat = new int[MAKS_VERTS][MAKS_VERTS];
  22.         nVerts = 0;
  23.         for(int j = 0;j<MAKS_VERTS;j++) {
  24.             for(int k = 0;k<MAKS_VERTS;k++) {
  25.                 adjMat[j][k] = 0;
  26.             }
  27.         }
  28.     }
  29.     public void addVertex(String lab) {
  30.         vertexList[nVerts++] = new Vertex(lab);
  31.     }
  32.     public void addEdge(int start, int end) {
  33.         adjMat[start][end] = 1;
  34.         adjMat[end][start] = 1;
  35.     }
  36.     public void displayVertex(int v) {
  37.         System.out.print(vertexList[v].label);
  38.     }
  39.     public void displayEdge(int v1, int v2) {
  40.         System.out.print("Vertex Start = " + v1 + "\nVertex End = " + v2 + "\n");
  41.         System.out.print(vertexList[v1].label);
  42.         System.out.print(" -----> ");
  43.         System.out.print(vertexList[v2].label);
  44.     }
  45.     public void displayAdj(int x, int v) {
  46.         System.out.print(adjMat[x][v]);
  47.     }
  48.     class Vertex {
  49.         public String label;
  50.         public boolean wasVisited;
  51.         public Vertex(String lab) {
  52.             label = lab;
  53.             wasVisited = false;
  54.         }
  55.     }
  56.     public static void main(String[] args) {
  57.         LatihanGraph theGraph = new LatihanGraph();
  58.         theGraph.addVertex("Andhika");
  59.         theGraph.addVertex("Ega");
  60.         theGraph.addVertex("Novita");
  61.         theGraph.addVertex("Bagus");
  62.         theGraph.addVertex("Raka");
  63.         theGraph.addVertex("Nadhia");
  64.         theGraph.addEdge(0, 1);
  65.         theGraph.addEdge(0, 3);
  66.         theGraph.addEdge(1, 3);
  67.         theGraph.addEdge(1, 2);
  68.         theGraph.addEdge(2, 4);
  69.         theGraph.addEdge(3, 4);
  70.         theGraph.addEdge(4, 5);
  71.         System.out.println("Nama-nama Vertex : ");
  72.         System.out.print("index 0 : ");
  73.         theGraph.displayVertex(0);
  74.         System.out.print("\nindex 1 : ");
  75.         theGraph.displayVertex(1);
  76.         System.out.print("\nindex 2 : ");
  77.         theGraph.displayVertex(2);
  78.         System.out.print("\nindex 3 : ");
  79.         theGraph.displayVertex(3);
  80.         System.out.print("\nindex 4 : ");
  81.         theGraph.displayVertex(4);
  82.         System.out.print("\nindex 5 : ");
  83.         theGraph.displayVertex(5);
  84.         System.out.println("\n-----------------------");
  85.         LatihanGraph a = new LatihanGraph();
  86.         Scanner s = new Scanner(System.in);
  87.         System.out.print("1. Input Vertex Index = ");
  88.         int v1;
  89.         v1 = s.nextInt();
  90.         System.out.print("2. Input Vertex Index = ");
  91.         int v2;
  92.         v2 = s.nextInt();
  93.         System.out.print("\n");
  94.         theGraph.displayEdge(v1, v2);
  95.         System.out.print("\n\nAdjecency Matriks adalah = ");
  96.         theGraph.displayAdj(v1, v2);
  97.         System.out.print("\n");
  98.     }
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement