-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath.cpp
More file actions
139 lines (125 loc) · 2.85 KB
/
Copy pathpath.cpp
File metadata and controls
139 lines (125 loc) · 2.85 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
139
#include <vector>
#include <stack>
/* Vertex Degrees
*
* The degree of a vertex is the number of edges incident on it.
*
*/
template <class Graph> class Degree {
const Graph &G;
std::vector <int> degree;
public:
Degree(const Graph &G) : G(G), degree(G.V()), 0) {
for (int v = 0; v < G.V(); v++) {
typename Graph::it it(G, v);
for (int w = it.begin(); !it.end(); w = it.next()) {
degree[v]++;
}
}
}
int operator[] (int v) const { return degree[v]; }
};
/* Simple Path Search
*
* A simple path is a path in a graph which has distinct edges and vertices.
*
* Uses a recursive depth-first search to find a simple path connecting two
* given vertices in a graph
*
*/
template <class Graph> class SimplePath {
const Graph &G;
std::vector <bool> visited;
bool found;
bool searchR(int v, int w) {
if (v == w) return true;
visited[v] = true;
typename Graph::it it(G, v);
for (int t = it.begin(); !it.end(); t = it.next()) {
if (!visited[t]) {
if (searchR(t, w)) return true;
}
}
return false;
}
public:
SimplePath(const Graph &G, int v, int w) : G(G), visited(G.V(), false) {
found = searchR(v, w);
}
bool exists() const { return found; }
};
/* Hamilton Path
*
* Given two vertices, is there a simple path connecting them that visits
* every vertex in the graph exactly once?
*
*/
template <class Graph> class HamiltonPath {
const Graph &G;
std::vector <bool> visited;
bool found;
bool searchR(int v, int w, int d) {
if (v == w) return (d == 0);
visited[v] = true;
typename Graph::it it(G, v);
for (int t = it.begin(); !it.end(); t = it.next()) {
if (!visited[t]) {
if (searchR(t, w, d - 1)) return true;
}
}
visited[v] = false;
return false;
}
public:
HamiltonPath(const Graph &G, int v, int w) : G(G), visited(G.V(), false) {
found = searchR(v, w);
}
bool exists() const { return found; }
};
/* Euler Path Existence
*
* Is there a path connecting two given vertices that uses each edge in the
* graph exactly once?
*
*/
template <class Graph> class EulerPath {
Graph G;
int v, w;
bool found;
std::stack <int> S;
int tour(int v);
public:
EulerPath(const Graph &G, int v, int w) : G(G), v(v), w(w) {
Degree<Graph> deg(G);
int t = deg[v] + deg[w];
if ((t % 2) != 0) { found = false; return; }
for (t = 0; t < G.V(); t++) {
if ((t != v) && (t != w)) {
if ((deg[t] % 2) != 0) {
found = false; return;
}
}
}
found = true;
}
bool exists() const { return found; }
int tour(int v) {
while (true) {
typename Graph::it it(G, v);
int w = it.begin();
if (it.end()) break;
S.push(v);
G.remove(Edge(v, w));
v = w;
}
return v;
}
void show() {
if (!found) return;
while (tour(v) == v && !S.empty()) {
v = S.pop();
std::cout << "-" << v;
}
std::cout << std::endl;
}
}