-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAcyclicLP.h
118 lines (105 loc) · 4.21 KB
/
AcyclicLP.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
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
#ifndef CH4_ACYCLICLP_H
#define CH4_ACYCLICLP_H
#include "../head/EdgeWeightedDigraph.h"
#include "../head/Topological.h"
#include <numeric>
using std::numeric_limits;
/**
* The {@code AcyclicLP} class represents a data type for solving the
* single-source longest paths problem in edge-weighted directed
* acyclic graphs (DAGs). The edge weights can be positive, negative, or zero.
* <p>
* This implementation uses a topological-sort based algorithm.
* 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 {@code distTo(int)} and {@code hasPathTo(int)} takes constant time;
* each call to {@code pathTo(int)} takes time proportional to the number of
* edges in the shortest path returned.
* <p>
* For additional documentation,
* see <a href="https://algs4.cs.princeton.edu/44sp">Section 4.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
class AcyclicLP {
public:
/**
* Computes a longest paths tree from {@code s} to every other vertex in
* the directed acyclic graph {@code G}.
* @param G the acyclic digraph
* @param s the source vertex
* @throws IllegalArgumentException if the digraph is not acyclic
* @throws IllegalArgumentException unless {@code 0 <= s < V}
*/
AcyclicLP(EdgeWeightedDigraph &G, int s) : distTo(G.V_(), numeric_limits<double>::min()), edgeTo(G.V_()) {
validateVertex(s);
distTo[s] = 0.0;
// relax vertices in topological order
Topological topological(G);
if (!topological.hasOrder())
throw runtime_error("Digraph is not acyclic.");
for (int v : topological.getorder()) {
for (DirectedEdge e : G.adj_(v))
relax(e);
}
}
/**
* Returns the length of a longest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return the length of a longest path from the source vertex {@code s} to vertex {@code v};
* {@code Double.NEGATIVE_INFINITY} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
double distTo_(int v) {
validateVertex(v);
return distTo[v];
}
/**
* Is there a path from the source vertex {@code s} to vertex {@code v}?
* @param v the destination vertex
* @return {@code true} if there is a path from the source vertex
* {@code s} to vertex {@code v}, and {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
bool hasPathTo(int v) {
validateVertex(v);
return distTo[v] > numeric_limits<double>::min();
}
/**
* Returns a longest path from the source vertex {@code s} to vertex {@code v}.
* @param v the destination vertex
* @return a longest path from the source vertex {@code s} to vertex {@code v}
* as an iterable of edges, and {@code null} if no such path
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
forward_list<DirectedEdge> pathTo(int v) {
validateVertex(v);
if (!hasPathTo(v)) throw runtime_error("no path to this vertex");
forward_list<DirectedEdge> path;
for (DirectedEdge e = edgeTo[v]; e.to() != -1; e = edgeTo[e.from()]) {
path.push_front(e);
}
return path;
}
private:
// relax edge e, but update if you find a *longer* path
void relax(DirectedEdge e) {
int v = e.from(), w = e.to();
if (distTo[w] < distTo[v] + e.weight_()) {
distTo[w] = distTo[v] + e.weight_();
edgeTo[w] = e;
}
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
void validateVertex(int v) {
int V = distTo.size();
if (v < 0 || v >= V)
throw runtime_error("vertex " + to_string(v) + " is not between 0 and " + to_string(V - 1));
}
private:
vector<double> distTo; // distTo[v] = distance of longest s->v path
vector<DirectedEdge> edgeTo; // edgeTo[v] = last edge on longest s->v path
};
#endif //CH4_ACYCLICLP_H