This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathhamiltonion.cpp
More file actions
138 lines (127 loc) · 2.76 KB
/
Copy pathhamiltonion.cpp
File metadata and controls
138 lines (127 loc) · 2.76 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<iostream>
#include<vector>
#include<string>
#include<climits>
using namespace std;
class Edge {
public:
int nbr;
int wt;
};
vector<vector<Edge>> graph;
void addEdge(int v1, int v2, int wt)
{
Edge e1;
e1.nbr = v2;
e1.wt = wt;
graph[v1].push_back(e1);
Edge e2;
e2.nbr = v1;
e2.wt = wt;
graph[v2].push_back(e2);
}
void display()
{
for(int v = 0; v < graph.size(); v++)
{
cout << v << " -> ";
for(int n = 0; n < graph[v].size(); n++)
{
Edge ne = graph[v][n];
cout << "[" << ne.nbr << "-" << ne.wt << "], ";
}
cout << "." << endl;
}
}
bool haspath(int s, int d, vector<bool>& visited)
{
if(s == d)
{
return true;
}
visited[s] = true;
for(int n = 0; n < graph[s].size(); n++)
{
Edge ne = graph[s][n];
if(visited[ne.nbr] == false)
{
bool hpfntod = haspath(ne.nbr, d, visited);
if(hpfntod == true)
{
return true;
}
}
}
return false;
}
void printallpaths(int s, int d, vector<bool>& visited,
string psf, int dsf)
{
if(s == d)
{
cout << psf << d << "@" << dsf << endl;
return;
}
visited[s] = true;
for(int n = 0; n < graph[s].size(); n++)
{
Edge ne = graph[s][n];
if(visited[ne.nbr] == false)
{
printallpaths(ne.nbr, d, visited,
psf + to_string(s),
dsf + ne.wt);
}
}
visited[s] = false;
}
void hamiltonian(int s, vector<bool>& visited, string asf, int os)
{
if(csf == graph.size()-1)
cout<<asf<<s;
for(int n = 0; n < graph[s].size(); n++)
{
Edge ne = graph[s][n];
if(visited[ne.nbr] == false)
{
hamiltonian()
}
}
}
{
psf += to_string(s);
cout<<psf;
for(int n = 0; n < graph[s].size(); n++)
{
Edge ne = graph[s][n];
if(ne.nbr == os)
{
cout<<"*"<<endl;
return;
}
}
cout<<"."<<endl;
return;
}
int main(int argc, char** argv)
{
graph.push_back(vector<Edge>()); // 0
graph.push_back(vector<Edge>()); // 1
graph.push_back(vector<Edge>()); // 2
graph.push_back(vector<Edge>()); // 3
graph.push_back(vector<Edge>()); // 4
graph.push_back(vector<Edge>()); // 5
graph.push_back(vector<Edge>()); // 6
addEdge(0, 1, 10);
addEdge(1, 2, 10);
addEdge(2, 3, 10);
addEdge(0, 3, 40);
addEdge(3, 4, 2);
addEdge(4, 5, 3);
addEdge(5, 6, 3);
addEdge(4, 6, 8);
display();
vector<bool> visited (7, false);
// cout << haspath(0, 6, visited) << endl;
printallpaths(0, 6, visited, "", 0);
}