summaryrefslogtreecommitdiffstats
path: root/Algorithms/Part-II/1-WordNet/SAP.java
blob: 39c6df30c13e739aacccad7a619f25d5c209e1d1 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* vim: set expandtab tabstop=4 shiftwidth=4 : */

import java.util.Iterator;

public class SAP
{

    private final Digraph g;
    private final BFS vBFS;
    private final BFS wBFS;

    private class BFS implements Iterable<Integer>
    {
        private static final int MAXINT = Integer.MAX_VALUE;

        private boolean[] marked;
        private int[] distTo;
        private Queue<Integer> visited;
        private Queue<Integer> q;

        public BFS(int size)
        {
            marked = new boolean[size];
            distTo = new int[size];
            visited = new Queue<Integer>();
            q = new Queue<Integer>();
            for (int v = 0; v < size; v++) {
                // distTo[v] = -1;
                marked[v] = false;
            }
        }

        public void bfs(int s)
        {
            clear();

            marked[s] = true;
            distTo[s] = 0;

            q.enqueue(s);
            visited.enqueue(s);
            bfs();
        }

        public void bfs(Iterable<Integer> ss)
        {
            clear();

            for (int s : ss) {
                marked[s] = true;
                distTo[s] = 0;
                q.enqueue(s);
                visited.enqueue(s);
            }

            bfs();
        }

        public int distTo(int v)
        {
            return distTo[v];
        }

        public boolean hasPathTo(int v)
        {
            return marked[v];
        }

        public Iterator<Integer> iterator() {
            return visited.iterator();
        }

        public int visitedCount()
        {
            return visited.size();
        }

        private void clear()
        {
            while (!visited.isEmpty()) {
                marked[visited.dequeue()] = false;
            }
        }

        private void bfs()
        {
            while (!q.isEmpty()) {
                int v = q.dequeue();
                for (int w : g.adj(v)) {
                    if (!marked[w]) {
                        distTo[w] = distTo[v] + 1;
                        marked[w] = true;
                        q.enqueue(w);
                        visited.enqueue(w);
                    }
                }
            }
        }
    }

    // data type use space proportional to E + V
    // all methods take time at most proportional to E + V in the worst case

    // constructor takes a digraph (not necessarily a DAG)
    public SAP(Digraph G)
    {
        g = new Digraph(G);
        vBFS = new BFS(G.V());
        wBFS = new BFS(G.V());
    }

    // length of shortest ancestral path between v and w
    // -1 if no such path
    public int length(int v, int w)
    {
        check(v);
        check(w);

        vBFS.bfs(v);
        wBFS.bfs(w);

        return distance();
    }

    // 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)
    {
        check(v);
        check(w);

        vBFS.bfs(v);
        wBFS.bfs(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)
    {
        check(v);
        check(w);

        vBFS.bfs(v);
        wBFS.bfs(w);

        return distance();
    }

    // a common ancestor that participates in shortest ancestral path
    // -1 if no such path
    public int ancestor(Iterable<Integer> v, Iterable<Integer> w)
    {
        check(v);
        check(w);

        vBFS.bfs(v);
        wBFS.bfs(w);

        return ancestor();
    }

    private int distance()
    {
        BFS aBFS, bBFS;
        if (vBFS.visitedCount() < wBFS.visitedCount()) {
            aBFS = vBFS;
            bBFS = wBFS;
        } else {
            aBFS = wBFS;
            bBFS = vBFS;
        }

        int len = Integer.MAX_VALUE;

        for (int x : aBFS) {
            if (bBFS.hasPathTo(x)) {
                int l = aBFS.distTo(x) + bBFS.distTo(x);
                if (l < len) {
                    len = l;
                }
            }
        }

        if (len == Integer.MAX_VALUE)
            return -1;
        return len;
    }

    private int ancestor()
    {
        BFS aBFS, bBFS;
        if (vBFS.visitedCount() < wBFS.visitedCount()) {
            aBFS = vBFS;
            bBFS = wBFS;
        } else {
            aBFS = wBFS;
            bBFS = vBFS;
        }

        int ancestor = -1;
        int len = Integer.MAX_VALUE;

        for (int x : aBFS) {
            if (bBFS.hasPathTo(x)) {
                int l = aBFS.distTo(x) + bBFS.distTo(x);
                if (l < len) {
                    len = l;
                    ancestor = x;
                }
            }
        }

        return ancestor;
    }

    private void check(int v)
    {
        if (v < 0 || v >= g.V())
            throw new IndexOutOfBoundsException("out of index");
    }

    private void check(Iterable<Integer> vs)
    {
        for (int v : vs) {
            check(v);
        }
    }

    // for unit testing of this class (such as the one below)
    public static void main(String[] args)
    {
        int length, ancestor;
        In in;
        Digraph G;
        SAP sap;

        in = new In("./data/digraph1.txt");
        G = new Digraph(in);
        sap = new SAP(G);

        length = sap.length(3, 11);
        ancestor = sap.ancestor(3, 11);
        StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);

        length = sap.length(9, 12);
        ancestor = sap.ancestor(9, 12);
        StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);

        length = sap.length(7, 2);
        ancestor = sap.ancestor(7, 2);
        StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);

        length = sap.length(1, 6);
        ancestor = sap.ancestor(1, 6);
        StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);

        Stopwatch w1 = new Stopwatch();
        for (int i = 1; i < 7; i++) {
            String g = "./data/digraph"+i+".txt";
            in = new In(g);
            G = new Digraph(in);
            sap = new SAP(G);
            for (int v = 0; v < G.V(); v++) {
                for (int w = 0; w < G.V(); w++) {
                    int a = sap.ancestor(v, w);
                    int l = sap.length(v, w);
                    // StdOut.printf("%s %d->%d : a:%d l:%d\n", g, v, w, a, l);
                }
            }
        }
        double a1 = w1.elapsedTime();
        StdOut.printf("all solved in : %g \n", a1);
    }
}