-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhamiltonian_path.cpp
61 lines (51 loc) · 1.28 KB
/
hamiltonian_path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
A hamiltonian path is a path in an undirected or directed graph in which each vertex is visited only once.
A hamiltonian cycle is a hamiltonian path which is a cycle
*/
#include <bits/stdc++.h>
using namespace std;
bool dfs(int u, int count, int n, vector<int> adj[], bool visited[]){
//if count of nodes in current path is equal to total number of vertex
if(count == n) return true;
//mark current node visited
visited[u] = true;
//recur for each adjacent vertex
for(auto v: adj[u]){
if(!visited[v]){
if(dfs(v, count+1, n, adj, visited))
return true;
}
}
//we do backtracking since we need to check each path
visited[u] = false;
return false;
}
bool hasHamiltonianPath(vector<int> adj[], int n){
bool visited[n];
memset(visited, false, sizeof(visited));
//the hamiltonian path can start from any vertex
//so we check for every vertex
for(int i = 0; i < n; i++){
if(dfs(i, 1, n, adj, visited)){
return true;
}
}
//if no hamiltonian path found
return false;
}
int main(){
int n, m;
cin >> n >> m;
vector<int> adj[n];
for(int i =0; i < m; i++){
int f,s;
cin >> f >> s;
adj[f].push_back(s);
adj[s].push_back(f);
}
if(hasHamiltonianPath(adj, n)){
cout << "Hamiltonian path exists\n";
}
else cout << "Hamiltonian path doesn't exists\n";
return 0;
}