-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDepthFirstPaths.h
93 lines (84 loc) · 3.03 KB
/
DepthFirstPaths.h
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
#ifndef CH4_DEPTHFIRSTPATHS_H
#define CH4_DEPTHFIRSTPATHS_H
#include "../head/Graph.h"
/**
* The {@code DepthFirstPaths} class represents a data type for finding
* paths from a source vertex <em>s</em> to every other vertex
* in an undirected graph.
* <p>
* This implementation uses depth-first search.
* The constructor takes time proportional to <em>V</em> + <em>E</em>,
* where <em>V</em> is the number of vertices and <em>E</em> is the number of edges.
* Each call to {@link #hasPathTo(int)} takes constant time;
* each call to {@link #pathTo(int)} takes time proportional to the length
* of the path.
* It uses extra space (not including the graph) proportional to <em>V</em>.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/41graph">Section 4.1</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
class DepthFirstPaths {
public:
/**
* Computes a path between {@code s} and every other vertex in graph {@code G}.
* @param G the graph
* @param s the source vertex
* @throws IllegalArgumentException unless {@code 0 <= s < V}
*/
DepthFirstPaths(Graph &G, int s) : s(s), edgeTo(G.getV()), marked(G.getV()) {
validateVertex(s);
dfs(G, s);
}
/**
* Is there a path between the source vertex {@code s} and vertex {@code v}?
* @param v the vertex
* @return {@code true} if there is a path, {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
bool hasPathTo(int v) {
validateVertex(v);
return marked[v];
}
/**
* Returns a path between the source vertex {@code s} and vertex {@code v}, or
* {@code null} if no such path.
* @param v the vertex
* @return the sequence of vertices on a path between the source vertex
* {@code s} and vertex {@code v}, as an Iterable
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
decltype(auto) pathTo(int v) {
validateVertex(v);
if (!hasPathTo(v)) throw runtime_error("No path to this vertex");
forward_list<int> path;
for (int x = v; x != s; x = edgeTo[x])
path.push_front(x);
path.push_front(s);
return path;
}
private:
// depth first search from v
void dfs(Graph &G, int v) {
marked[v] = true;
for (int w : G.getadj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
dfs(G, w);
}
}
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
void validateVertex(int v) {
int V = marked.size();
if (v < 0 || v >= V)
throw runtime_error("vertex " + to_string(v) + " is not between 0 and " + to_string(V - 1));
}
private:
vector<bool> marked; // marked[v] = is there an s-v path?
vector<int> edgeTo; // edgeTo[v] = last edge on s-v path
int s; // source vertex
};
#endif //CH4_DEPTHFIRSTPATHS_H