document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Tugas : Graph Adjacency Matrices
  3.  *
  4.  * Rafael Asi Kristanto Tambunan
  5.  * 5025201168
  6.  * Teknik Informatika
  7.  */
  8.  
  9. class Graph {
  10.     private int numOfNodes;
  11.     private boolean directed;
  12.     private boolean weighted;
  13.     private float[][] matrix;
  14.     private boolean[][] isVisited;
  15.  
  16.     public Graph(int numOfNodes, boolean directed, boolean weighted) {
  17.         this.directed = directed;
  18.         this.weighted = weighted;
  19.         this.numOfNodes = numOfNodes;
  20.  
  21.         matrix = new float[numOfNodes][numOfNodes];
  22.         isVisited = new boolean[numOfNodes][numOfNodes];
  23.     }
  24.  
  25.     public void addEdge(int source, int destination, float weight) {
  26.  
  27.         float valueToAdd = weight;
  28.  
  29.         if (!weighted) {
  30.             valueToAdd = 1;
  31.         }
  32.  
  33.         matrix[source][destination] = valueToAdd;
  34.         isVisited[source][destination] = true;
  35.  
  36.         if (!directed) {
  37.             matrix[destination][source] = valueToAdd;
  38.             isVisited[destination][source] = true;
  39.         }
  40.     }
  41.  
  42.     public void printMatrix() {
  43.         for (int i = 0; i < numOfNodes; i++) {
  44.             for (int j = 0; j < numOfNodes; j++) {
  45.                 if (isVisited[i][j])
  46.                     System.out.format("%8s", String.valueOf(matrix[i][j]));
  47.                 else System.out.format("%8s", "/  ");
  48.             }
  49.             System.out.println();
  50.         }
  51.     }
  52.  
  53.     public void printEdges() {
  54.         for (int i = 0; i < numOfNodes; i++) {
  55.             System.out.print("Node " + i + " is connected to: ");
  56.             for (int j = 0; j < numOfNodes; j++) {
  57.                 if (isVisited[i][j]) {
  58.                     System.out.print(j + " ");
  59.                 }
  60.             }
  61.             System.out.println();
  62.         }
  63.     }
  64.  
  65.     public boolean hasEdge(int source, int destination) {
  66.         return isVisited[source][destination];
  67.     }
  68.  
  69.     public Float getEdgeValue(int source, int destination) {
  70.         if (!weighted || !isVisited[source][destination])
  71.             return null;
  72.         return matrix[source][destination];
  73.     }
  74. }
  75.  
  76. public class AdjacencyMatrices {
  77.     public static void main(String[] args) {
  78.         Graph graph = new Graph(5, false, true);
  79.        
  80.         graph.addEdge(0, 1, 7);
  81.         graph.addEdge(0, 2, 14);
  82.         graph.addEdge(0, 3, 5);
  83.         graph.addEdge(1, 0, 13);
  84.         graph.addEdge(1, 2, 4);
  85.         graph.addEdge(1, 3, -5);
  86.         graph.addEdge(1, 4, 6);
  87.         graph.addEdge(2, 3, 3);
  88.         graph.addEdge(2, 4, 8);
  89.         graph.addEdge(3, 2, 12);
  90.         graph.addEdge(3, 3, 0);
  91.        
  92.         System.out.println("The Matrix :");
  93.         graph.printMatrix();
  94.  
  95.         System.out.println();
  96.         System.out.println();
  97.  
  98.         System.out.println("Connected Node : ");
  99.         graph.printEdges();
  100.  
  101.         System.out.println();
  102.         System.out.println("Does an edge from 1 to 0 exist?");
  103.         if (graph.hasEdge(0,1)) {
  104.             System.out.println("Yes");
  105.         }
  106.         else System.out.println("No");
  107.     }
  108. }
');