-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAP.java
165 lines (151 loc) · 5.26 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
//package wordNet;
//import edu.princeton.cs.algs4.BreadthFirstDirectedPaths;
//import edu.princeton.cs.algs4.Digraph;
//import edu.princeton.cs.introcs.In;
//import edu.princeton.cs.introcs.StdIn;
//import edu.princeton.cs.introcs.StdOut;
public class SAP {
private Digraph g;
private int ancestor;
private boolean ancestorFound;
private int ancestor_group;
private boolean ancestor_groupFound;
// constructor takes a digraph (not necessarily a DAG)
public SAP(Digraph G) {
this.g = G;
this.ancestor = -1;
this.ancestorFound = false;
this.ancestor_group= -1;
this.ancestor_groupFound = false;
}
// length of shortest ancestral path between v and w; -1 if no such path
public int length(int v, int w)
{
if (v<0 | w<0) {
throw new java.lang.IndexOutOfBoundsException();
}
this.ancestor = -1;
this.ancestorFound = false;
BreadthFirstDirectedPaths bfs_v = new BreadthFirstDirectedPaths(this.g, v);
BreadthFirstDirectedPaths bfs_w = new BreadthFirstDirectedPaths(this.g, w);
int tmpDist = Integer.MAX_VALUE;
int[] dist_vw = new int[this.g.V()]; //sum of v->i and w->i
for (int i=0;i<g.V();i++) {
if (bfs_v.hasPathTo(i) & bfs_w.hasPathTo(i)) {
this.ancestorFound = true;
dist_vw[i] = bfs_v.distTo(i) + bfs_w.distTo(i);
if (dist_vw[i] < tmpDist) {
tmpDist = dist_vw[i];
this.ancestor = i;
}
}
}
if (this.ancestorFound) {
return tmpDist;
} else {
return -1;
}
}
// 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)
{
if (v<0 | w<0) {
throw new java.lang.IndexOutOfBoundsException();
}
this.ancestor = -1;
this.ancestorFound = false;
BreadthFirstDirectedPaths bfs_v = new BreadthFirstDirectedPaths(this.g, v);
BreadthFirstDirectedPaths bfs_w = new BreadthFirstDirectedPaths(this.g, w);
int tmpDist = Integer.MAX_VALUE;
int[] dist_vw = new int[this.g.V()]; //sum of v->i and w->i
for (int i=0;i<g.V();i++) {
if (bfs_v.hasPathTo(i) & bfs_w.hasPathTo(i)) {
this.ancestorFound = true;
dist_vw[i] = bfs_v.distTo(i) + bfs_w.distTo(i);
if (dist_vw[i] < tmpDist) {
tmpDist = dist_vw[i];
this.ancestor = i;
}
}
}
if (this.ancestorFound) {
return this.ancestor;
} else {
return -1;
}
}
// 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)
{
if (v==null | w==null) {
throw new java.lang.NullPointerException();
}
this.ancestor_group= -1;
this.ancestor_groupFound = false;
BreadthFirstDirectedPaths bfs_group_v = new BreadthFirstDirectedPaths(this.g, v);
BreadthFirstDirectedPaths bfs_group_w = new BreadthFirstDirectedPaths(this.g, w);
int tmpDist = Integer.MAX_VALUE;
int[] dist_groupvw = new int[this.g.V()];
for (int i=0;i<g.V();i++) {
if (bfs_group_v.hasPathTo(i) & bfs_group_w.hasPathTo(i)) {
dist_groupvw[i] = bfs_group_v.distTo(i) + bfs_group_w.distTo(i);
this.ancestor_groupFound = true;
if (dist_groupvw[i] < tmpDist)
{
tmpDist = dist_groupvw[i];
this.ancestor_group = i;
}
}
}
if (this.ancestor_groupFound) {
return tmpDist;
} else {
return -1;
}
}
// a common ancestor that participates in shortest ancestral path; -1 if no such path
public int ancestor(Iterable<Integer> v, Iterable<Integer> w)
{
if (v==null | w==null) {
throw new java.lang.NullPointerException();
}
this.ancestor_group= -1;
this.ancestor_groupFound = false;
BreadthFirstDirectedPaths bfs_group_v = new BreadthFirstDirectedPaths(this.g, v);
BreadthFirstDirectedPaths bfs_group_w = new BreadthFirstDirectedPaths(this.g, w);
int tmpDist = Integer.MAX_VALUE;
int[] dist_groupvw = new int[this.g.V()];
for (int i=0;i<g.V();i++) {
if (bfs_group_v.hasPathTo(i) & bfs_group_w.hasPathTo(i)) {
dist_groupvw[i] = bfs_group_v.distTo(i) + bfs_group_w.distTo(i);
this.ancestor_groupFound = true;
if (dist_groupvw[i] < tmpDist)
{
tmpDist = dist_groupvw[i];
this.ancestor_group = i;
}
}
}
if (this.ancestor_groupFound) {
return this.ancestor_group;
} else {
return -1;
}
}
// do unit testing of this class
public static void main(String[] args) {
In in = new In("digraph-wordnet.txt");
// In in = new In("digraph3.txt");
// In in = new In("hypernyms15Path.txt");
// 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);
}
}
}