Advertisement
Guest User

Untitled

a guest
May 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. // Number of vertices in the graph
  6. #define V 5
  7.  
  8. void printSolution(int path[]);
  9.  
  10. /* A utility function to check if the vertex v can be added at
  11. index 'pos' in the Hamiltonian Cycle constructed so far (stored
  12. in 'path[]') */
  13. bool isSafe(int v, bool graph[V][V], int path[], int pos)
  14. {
  15.     /* Check if this vertex is an adjacent vertex of the previously
  16.     added vertex. */
  17.     if (graph[path[pos - 1]][v] == 0)
  18.         return false;
  19.  
  20.     /* Check if the vertex has already been included.
  21.     This step can be optimized by creating an array of size V */
  22.     for (int i = 0; i < pos; i++)
  23.         if (path[i] == v)
  24.             return false;
  25.  
  26.     return true;
  27. }
  28.  
  29. /* A recursive utility function to solve hamiltonian cycle problem */
  30. bool hamCycleUtil(bool graph[V][V], int path[], int pos)
  31. {
  32.     /* base case: If all vertices are included in Hamiltonian Cycle */
  33.     if (pos == V)
  34.     {
  35.         // And if there is an edge from the last included vertex to the
  36.         // first vertex
  37.         if (graph[path[pos - 1]][path[0]] == 1)
  38.             return true;
  39.         else
  40.             return false;
  41.     }
  42.  
  43.     // Try different vertices as a next candidate in Hamiltonian Cycle.
  44.     // We don't try for 0 as we included 0 as starting point in in hamCycle()
  45.     for (int v = 1; v < V; v++)
  46.     {
  47.         /* Check if this vertex can be added to Hamiltonian Cycle */
  48.         if (isSafe(v, graph, path, pos))
  49.         {
  50.             path[pos] = v;
  51.  
  52.             /* recur to construct rest of the path */
  53.             if (hamCycleUtil(graph, path, pos + 1) == true)
  54.                 return true;
  55.  
  56.             /* If adding vertex v doesn't lead to a solution,
  57.             then remove it */
  58.             path[pos] = -1;
  59.         }
  60.     }
  61.  
  62.     /* If no vertex can be added to Hamiltonian Cycle constructed so far,
  63.     then return false */
  64.     return false;
  65. }
  66.  
  67. /* This function solves the Hamiltonian Cycle problem using Backtracking.
  68. It mainly uses hamCycleUtil() to solve the problem. It returns false
  69. if there is no Hamiltonian Cycle possible, otherwise return true and
  70. prints the path. Please note that there may be more than one solutions,
  71. this function prints one of the feasible solutions. */
  72. bool hamCycle(bool graph[V][V])
  73. {
  74.     int *path = new int[V];
  75.     for (int i = 0; i < V; i++)
  76.         path[i] = -1;
  77.  
  78.     /* Let us put vertex 0 as the first vertex in the path. If there is
  79.     a Hamiltonian Cycle, then the path can be started from any point
  80.     of the cycle as the graph is undirected */
  81.     path[0] = 0;
  82.     if (hamCycleUtil(graph, path, 1) == false)
  83.     {
  84.         printf("\nSolution does not exist");
  85.         return false;
  86.     }
  87.  
  88.     printSolution(path);
  89.     return true;
  90. }
  91.  
  92. /* A utility function to print solution */
  93. void printSolution(int path[])
  94. {
  95.     printf("Solution Exists:"
  96.         " Following is one Hamiltonian Cycle \n");
  97.     for (int i = 0; i < V; i++)
  98.         printf(" %d ", path[i]);
  99.  
  100.     // Let us print the first vertex again to show the complete cycle
  101.     printf(" %d ", path[0]);
  102.     printf("\n");
  103. }
  104.  
  105. // driver program to test above function
  106. int main()
  107. {
  108.     /* Let us create the following graph
  109.     (0)--(1)--(2)
  110.     |   / \   |
  111.     |  /   \  |
  112.     | /     \ |
  113.     (3)-------(4)    */
  114.     ifstream fin;
  115.     fin.open("C:\\Users\\kalin\\Desktop");
  116.     int m;
  117.     bool graph1[V][V];
  118.     fin >> m;
  119.     for (int i = 0; i < m; i++) {
  120.         int v1, v2;
  121.         fin >> v1 >> v2;
  122.         graph1[v1][v2] = 1;
  123.         graph1[v2][v1] = 1;
  124.         printf("%d %d\n", v1, v2);
  125.     }
  126.     // Print the solution
  127.     hamCycle(graph1);
  128.  
  129.     /* Let us create the following graph
  130.     (0)--(1)--(2)
  131.     |   / \   |
  132.     |  /   \  |
  133.     | /     \ |
  134.     (3)       (4)    */
  135.     bool graph2[V][V];
  136.     fin >> m;
  137.     for (int i = 0; i < m; i++) {
  138.         int v1, v2;
  139.         fin >> v1 >> v2;
  140.         graph2[v1][v2] = 1;
  141.         graph2[v2][v1] = 1;
  142.         printf("%d %d\n", v1, v2);
  143.     }
  144.     // Print the solution
  145.     hamCycle(graph2);
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement