diff --git a/graph/hamiltonianGraph.cpp b/graph/hamiltonianGraph.cpp new file mode 100644 index 0000000..9d7f8bb --- /dev/null +++ b/graph/hamiltonianGraph.cpp @@ -0,0 +1,71 @@ +/* + To determine that a given graph contains hamiltonian cycle or not + Hamiltonian Path :- A path in undirected graph which visits every vertex exactly once + + +*/ + +#include +using namespace std; +bool isSafe(int vertex, vector> &graph, vector &path, int index) +{ + // check if this vertex is adjacent to previously added vertex + if (graph[path[index - 1]][vertex] == 0) + return false; + // check if it's already included or not + for (int i = 0; i <= index - 1; i++) + { + if (path[i] == vertex) + return false; + } + return true; +} +bool hashamCycle(vector> &graph, vector &path, int index, int n) +{ + // If all vertices have been included + if (index == n) + { + // we need to check if there is a path + // from last added vertex to first added vertex + if (graph[path[index - 1]][path[0]] != 1) + return false; + else + return true; + } + // try out possible candidates for next index + for (int v = 1; v < n; v++) + { + if (isSafe(v, graph, path, index)) + { + path[index] = v; + if (hashamCycle(graph, path, index + 1, n)) + return true; + // backtrack + path[index] = -1; + } + } + return false; +} +int main() +{ + int n = 5; + vector> graph{{0, 1, 0, 1, 0}, + {1, 0, 1, 1, 1}, + {0, 1, 0, 0, 1}, + {1, 1, 0, 0, 1}, + {0, 1, 1, 1, 0}}; + vector path(n, -1); + path[0] = 0; + if (hashamCycle(graph, path, 1, 5)) + { + cout << "Hamiltonian Path\n"; + for (auto x : path) + cout << x << " "; + cout << endl; + } + else + { + cout << "No hamiltonian path exist\n"; + } + return 0; +} \ No newline at end of file