SHARE
TWEET

Untitled

uopspop Sep 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. public class GraphAdjacentMatrix {
  4.  
  5.     private boolean adjMatrix[][];
  6.     private int numVertices;
  7.  
  8.     public GraphAdjacentMatrix(int numVertices) {
  9.         this.numVertices = numVertices;
  10.         adjMatrix = new boolean[numVertices][numVertices];
  11.     }
  12.  
  13.     public void setEdge(int i, int j) {
  14.         adjMatrix[i][j] = true;
  15.         adjMatrix[j][i] = true;
  16.     }
  17.  
  18.     public void delEdge(int i, int j) {
  19.         adjMatrix[i][j] = false;
  20.         adjMatrix[j][i] = false;
  21.     }
  22.  
  23.     public boolean isEdge(int i, int j) {
  24.         return adjMatrix[i][j];
  25.     }
  26.  
  27.     public int first(int v1){
  28.         boolean[] ary = this.adjMatrix[v1];
  29.         int firstNeighborIndex = this.numVertices; // return n if none
  30.         for (int i = 0; i < ary.length; i++) {
  31.             if (ary[i] != false){
  32.                 firstNeighborIndex = i;
  33.                 break;
  34.             }
  35.         }
  36.         return firstNeighborIndex;
  37.     }
  38.  
  39.     public int next(int v1, int v2){
  40.         boolean[] ary = this.adjMatrix[v1];
  41.         int nextNeighborIndex = this.numVertices; // return n if none
  42.         for (int i = v2 + 1; i < ary.length; i++) {
  43.             if (ary[i] != false){
  44.                 nextNeighborIndex = i;
  45.                 break;
  46.             }
  47.         }
  48.         return nextNeighborIndex;
  49.     }
  50.  
  51.     public String toString() {
  52.         StringBuilder s = new StringBuilder();
  53.         for (int i = 0; i < numVertices; i++) {
  54.             s.append(i + ": ");
  55.             for (boolean j : adjMatrix[i]) {
  56.                 s.append((j?1:0) + " ");
  57.             }
  58.             s.append("\n");
  59.         }
  60.         return s.toString();
  61.     }
  62.  
  63.     public static void main(String[] args) {
  64.         GraphAdjacentMatrix gam = new GraphAdjacentMatrix(5);
  65.         boolean isEdge;
  66.         gam.setEdge(0, 1);
  67.         isEdge = gam.isEdge(0, 1);
  68.         gam.delEdge(0, 1);
  69.         isEdge = gam.isEdge(0, 1);
  70.  
  71.         gam.setEdge(0, 1);
  72.         gam.setEdge(0, 2);
  73.         gam.setEdge(0, 4);
  74.  
  75.         int nextNeighbor;
  76.         nextNeighbor = gam.first(0);
  77.         nextNeighbor = gam.next(0, nextNeighbor);
  78.         nextNeighbor = gam.next(0, nextNeighbor);
  79.         nextNeighbor = gam.next(0, nextNeighbor);
  80.  
  81.         System.out.print(gam.toString());
  82.         System.out.println();
  83.     }
  84.  
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top