-
Notifications
You must be signed in to change notification settings - Fork 17
/
SAP.java
204 lines (188 loc) · 7.01 KB
/
SAP.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.Stack;
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.Digraph;
public class SAP {
private int length; // length of the shortest path between V and W
private int ancestor; // the nearest ancestor of V and W
private Digraph copyG; // save the copy of associated digraph
private int[] distTo1; // distTo1[v] = length of shortest V->v path
private int[] distTo2; // distTo2[v] = length of shortest W->v path
private boolean[] marked1; // marked1[v] = is there an V->v path?
private boolean[] marked2; // marked2[v] = is there an W->v path?
private Stack<Integer> stack1; // store changed auxiliary array1 entries
private Stack<Integer> stack2; // store changed auxiliary array1 entries
// constructor takes a digraph (not necessarily a DAG)
public SAP(Digraph G) {
if (G == null) {
throw new IllegalArgumentException("argument to SAP() is null");
}
copyG = new Digraph(G);
distTo1 = new int[G.V()];
distTo2 = new int[G.V()];
marked1 = new boolean[G.V()];
marked2 = new boolean[G.V()];
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
// length of shortest ancestral path between v and w; -1 if no such path
public int length(int v, int w) {
validateVertex(v);
validateVertex(w);
compute(v, w);
return length;
}
// a common ancestor of v and w that participates in a shortest ancestral path;
// -1 if no such path
public int ancestor(int v, int w) {
validateVertex(v);
validateVertex(w);
compute(v, w);
return ancestor;
}
// length of shortest ancestral path between any vertex in v and any vertex in
// w; -1 if no such path
public int length(Iterable<Integer> v, Iterable<Integer> w) {
validateVertices(v);
validateVertices(w);
compute(v, w);
return length;
}
// a common ancestor that participates in shortest ancestral path; -1 if no such
// path
public int ancestor(Iterable<Integer> v, Iterable<Integer> w) {
validateVertices(v);
validateVertices(w);
compute(v, w);
return ancestor;
}
// using two bfs lockstep from v and w to compute sap
private void compute(int v, int w) {
length = -1;
ancestor = -1;
distTo1[v] = 0;
distTo2[w] = 0;
marked1[v] = true;
marked2[w] = true;
stack1.push(v);
stack2.push(w);
Queue<Integer> q1 = new Queue<Integer>();
Queue<Integer> q2 = new Queue<Integer>();
q1.enqueue(v);
q2.enqueue(w);
bfs(q1, q2);
}
// using two bfs lockstep from sources v and sources w to compute sap
private void compute(Iterable<Integer> v, Iterable<Integer> w) {
length = -1;
ancestor = -1;
Queue<Integer> q1 = new Queue<Integer>();
Queue<Integer> q2 = new Queue<Integer>();
for (int v1 : v) {
marked1[v1] = true;
stack1.push(v1);
distTo1[v1] = 0;
q1.enqueue(v1);
}
for (int w1 : w) {
marked2[w1] = true;
stack2.push(w1);
distTo2[w1] = 0;
q2.enqueue(w1);
}
bfs(q1, q2);
}
// run two bfs alternating back and forth bewteen q1 and q2
private void bfs(Queue<Integer> q1, Queue<Integer> q2) {
while (!q1.isEmpty() || !q2.isEmpty()) {
if (!q1.isEmpty()) {
int v = q1.dequeue();
if (marked2[v]) {
if (distTo1[v] + distTo2[v] < length || length == -1) {
ancestor = v;
length = distTo1[v] + distTo2[v];
}
}
// stop adding new vertex to queue if the distance exceeds the length
if (distTo1[v] < length || length == -1) {
for (int w : copyG.adj(v)) {
if (!marked1[w]) {
distTo1[w] = distTo1[v] + 1;
marked1[w] = true;
stack1.push(w);
q1.enqueue(w);
// StdOut.println("push " + w + " into q1");
}
}
}
}
if (!q2.isEmpty()) {
int v = q2.dequeue();
if (marked1[v]) {
if (distTo1[v] + distTo2[v] < length || length == -1) {
ancestor = v;
length = distTo1[v] + distTo2[v];
}
}
// stop adding new vertex to queue if the distance exceeds the length
if (distTo2[v] < length || length == -1) {
for (int w : copyG.adj(v)) {
if (!marked2[w]) {
distTo2[w] = distTo2[v] + 1;
marked2[w] = true;
stack2.push(w);
q2.enqueue(w);
// StdOut.println("push " + w + " into q2");
}
}
}
}
}
init(); // reinitialize auxiliary array for next bfs
}
// init auxiliary array for bfs
private void init() {
while (!stack1.isEmpty()) {
int v = stack1.pop();
marked1[v] = false;
}
while (!stack2.isEmpty()) {
int v = stack2.pop();
marked2[v] = false;
}
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertex(int v) {
int V = marked1.length;
if (v < 0 || v >= V)
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V - 1));
}
// throw an IllegalArgumentException unless {@code 0 <= v < V}
private void validateVertices(Iterable<Integer> vertices) {
if (vertices == null) {
throw new IllegalArgumentException("argument is null");
}
int V = marked1.length;
for (int v : vertices) {
if (v < 0 || v >= V) {
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V - 1));
}
}
}
// do unit testing of this class
public static void main(String[] args) {
In in = new In(args[0]);
Digraph G = new Digraph(in);
SAP sap = new SAP(G);
while (!StdIn.isEmpty()) {
int v = StdIn.readInt();
int w = StdIn.readInt();
int length = sap.length(v, w);
int ancestor = sap.ancestor(v, w);
StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);
}
}
}