Advertisement
Samuel_Berkat_Hulu

graphhh

Jun 15th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Graph here.
  4.  *
  5.  * Samuel Berkat Hulu
  6.  * @version 5.0
  7.  */
  8. class Graph
  9. {
  10.     private final int numNodes;
  11.     private final boolean direct;
  12.     private final boolean weight;
  13.     private final float[][] matrix;
  14.     private final boolean[][] isMatrix;
  15.  
  16.     Graph(int num, boolean direct, boolean weight)
  17.     {
  18.  
  19.         this.direct = direct;
  20.         this.weight = weight;
  21.         this.numNodes = num;
  22.         matrix = new float[num][num];
  23.         isMatrix = new boolean[num][num];
  24.     }
  25.  
  26.     public void addEdge(int start, int end)
  27.     {
  28.         int addValue = 1;
  29.  
  30.         if (weight)
  31.             addValue = 0;
  32.  
  33.         matrix[start][end] = addValue;
  34.         isMatrix[start][end] = true;
  35.  
  36.         if (!direct)
  37.         {
  38.             matrix[end][start] = addValue;
  39.             isMatrix[end][start] = true;
  40.         }
  41.     }
  42.  
  43.     public void addEdge(int start, int end, float the_weight)
  44.     {
  45.         float addValue = the_weight;
  46.  
  47.         if (!weight)
  48.             addValue = 1;
  49.  
  50.         matrix[start][end] = addValue;
  51.         isMatrix[start][end] = true;
  52.  
  53.         if (!direct)
  54.         {
  55.             matrix[end][start] = addValue;
  56.             isMatrix[end][start] = true;
  57.         }
  58.     }
  59.  
  60.     public void printMatrix()
  61.     {
  62.         for (int i = 0; i < numNodes; i++)
  63.         {
  64.             for (int j = 0; j < numNodes; j++)
  65.             {
  66.                 if (isMatrix[i][j])
  67.                     System.out.format("%8s", matrix[i][j]);
  68.                 else
  69.                     System.out.format("%8s", "/  ");
  70.             }
  71.             System.out.println();
  72.         }
  73.     }
  74.  
  75.     public void printEdges()
  76.     {
  77.         for (int i = 0; i < numNodes; i++)
  78.         {
  79.             System.out.print("Node " + i + " is connected to: ");
  80.             for (int j = 0; j < numNodes; j++)
  81.             {
  82.                 if (isMatrix[i][j])
  83.                     System.out.print(j + " ");
  84.             }
  85.             System.out.println();
  86.         }
  87.     }
  88.  
  89.     public boolean hasEdge(int start, int end)
  90.     {
  91.         return isMatrix[start][end];
  92.     }
  93. }
  94.  
  95. public class Main
  96. {
  97.     public static void main(String[] args)
  98.     {
  99.         Graph graph = new Graph(6, false, true);
  100.  
  101.         graph.addEdge(0, 1, 19);
  102.         graph.addEdge(0, 2, 10);
  103.         graph.addEdge(0, 3, 7);
  104.         graph.addEdge(0, 4, 2);
  105.         graph.addEdge(0, 5, 11);
  106.         graph.addEdge(1, 2, 3);
  107.         graph.addEdge(1, 3);
  108.         graph.addEdge(1, 4);
  109.         graph.addEdge(2, 3);
  110.         graph.addEdge(3, 4);
  111.         graph.addEdge(4, 5);
  112.  
  113.         graph.printMatrix();
  114.  
  115.         System.out.println();
  116.         System.out.println();
  117.  
  118.         graph.printEdges();
  119.  
  120.         System.out.println();
  121.         System.out.println("Does an edge from 1 to 5 exist?");
  122.  
  123.         if (graph.hasEdge(0,1))
  124.             System.out.println("Yes");
  125.         else
  126.             System.out.println("No");
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement