Advertisement
Guest User

TestGraph Hamiltonian Circuit Algorithm

a guest
Dec 23rd, 2017
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package circuits;
  2.  
  3. public class TestGraph {
  4.  
  5.     public static void main(String[] args) {
  6.         Graph g = new Graph(5);
  7.        
  8.         System.out.println("Graph:");
  9.         // add Edges
  10.         g.addEdge(0, 1);
  11.         g.addEdge(0, 3);
  12.         g.addEdge(1, 0);
  13.         g.addEdge(1, 2);
  14.         g.addEdge(1, 3);
  15.         g.addEdge(1, 4);
  16.         g.addEdge(2, 1);
  17.         g.addEdge(2, 4);
  18.         g.addEdge(3, 0);
  19.         g.addEdge(3, 1);
  20.         g.addEdge(3, 4);
  21.         g.addEdge(4, 1);
  22.         g.addEdge(4, 2);
  23.         g.addEdge(4, 3);
  24.  
  25.         // print Graph
  26.         g.printGraph();
  27.  
  28.         // Hamiltonian Circuit Algorithm
  29.         System.out.println("Backtracking Hamiltonian Circuit Algorithm:");
  30.         hamCircuit(g);
  31.     }
  32.    
  33.     static void hamCircuit(Graph g){
  34.         // vertex count
  35.         int V = g.getvCount();
  36.        
  37.         // path matrix
  38.         int path[] = new int[V];
  39.         // initialize to -1 except starting Vertex
  40.         for(int i=0; i<V; i++){
  41.             path[i] = -1;
  42.         }
  43.         path[0] = 0;
  44.        
  45.         if(solveHamCircuit(V, g, path, 1) == false){
  46.             System.out.println("Solution does not exist!");
  47.         }
  48.         else{
  49.             System.out.println("Solution exists! The following is one of the solutions:");
  50.             for(int i=0; i<V; i++){
  51.                 System.out.print(" " + path[i] + " ");
  52.             }
  53.         }
  54.        
  55.     }
  56.    
  57.     static boolean canBeAdded(int v, Graph g, int path[], int pos){
  58.         // if vertex is already adjacent of previously added vertex
  59.         if(g.getAdj()[pos - 1][v] == 0){
  60.             return false;
  61.         }
  62.        
  63.         // check if vertex already included
  64.         for (int i = 0; i < pos; i++){
  65.             if (path[i] == v) return false;
  66.         }
  67.        
  68.         // in any other case return true
  69.         return true;
  70.     }
  71.    
  72.     static boolean solveHamCircuit(int V, Graph g, int path[], int pos){
  73.         // check if all are included and if it makes a circle
  74.         if(pos == V){
  75.             if( g.getAdj()[path[pos - 1]][path[0]] == 1){
  76.                 return true;
  77.             }
  78.             else{
  79.                 return false;
  80.             }
  81.         }
  82.        
  83.         // try adding different vertices
  84.         for (int v = 1; v < V; v++)
  85.         {
  86.             // check if it can be added
  87.             if (canBeAdded(v, g, path, pos))
  88.             {
  89.                 path[pos] = v;
  90.  
  91.                 // call function again recursively to build the path
  92.                 if (solveHamCircuit(V, g, path, pos + 1) == true){
  93.                     return true;
  94.                 }
  95.                
  96.                 // backtracking part
  97.                 // we go back if adding v doesn't give us a solution
  98.                 path[pos] = -1;
  99.             }
  100.         }
  101.        
  102.         // if no vertex can be added | circle can't be created
  103.         return false;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement