afiqakraam

graph

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