-
Notifications
You must be signed in to change notification settings - Fork 517
/
SingleSourceShortestPathTraverser.java
349 lines (308 loc) · 13.1 KB
/
SingleSourceShortestPathTraverser.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hugegraph.traversal.algorithm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.backend.id.Id;
import org.apache.hugegraph.backend.query.QueryResults;
import org.apache.hugegraph.structure.HugeEdge;
import org.apache.hugegraph.type.define.Directions;
import org.apache.hugegraph.util.CollectionUtil;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.InsertionOrderUtil;
import org.apache.hugegraph.util.NumericUtil;
import org.apache.tinkerpop.gremlin.structure.Edge;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
public class SingleSourceShortestPathTraverser extends HugeTraverser {
public SingleSourceShortestPathTraverser(HugeGraph graph) {
super(graph);
}
public WeightedPaths singleSourceShortestPaths(Id sourceV, Directions dir,
String label, String weight,
long degree, long skipDegree,
long capacity, long limit) {
E.checkNotNull(sourceV, "source vertex id");
this.checkVertexExist(sourceV, "source vertex");
E.checkNotNull(dir, "direction");
checkDegree(degree);
checkCapacity(capacity);
checkSkipDegree(skipDegree, degree, capacity);
checkLimit(limit);
Id labelId = this.getEdgeLabelIdOrNull(label);
Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
degree, skipDegree, capacity, limit);
while (true) {
// Found, reach max depth or reach capacity, stop searching
traverser.forward();
if (traverser.done()) {
this.vertexIterCounter.addAndGet(traverser.vertexCount);
this.edgeIterCounter.addAndGet(traverser.edgeCount);
WeightedPaths paths = traverser.shortestPaths();
List<List<Id>> pathList = paths.pathList();
Set<Edge> edges = new HashSet<>();
for (List<Id> path : pathList) {
edges.addAll(traverser.edgeRecord.getEdges(path.iterator()));
}
paths.setEdges(edges);
return paths;
}
checkCapacity(traverser.capacity, traverser.size, "shortest path");
}
}
public NodeWithWeight weightedShortestPath(Id sourceV, Id targetV,
Directions dir, String label,
String weight, long degree,
long skipDegree, long capacity) {
E.checkNotNull(sourceV, "source vertex id");
E.checkNotNull(targetV, "target vertex id");
this.checkVertexExist(sourceV, "source vertex");
this.checkVertexExist(targetV, "target vertex");
E.checkNotNull(dir, "direction");
E.checkNotNull(weight, "weight property");
checkDegree(degree);
checkCapacity(capacity);
checkSkipDegree(skipDegree, degree, capacity);
Id labelId = this.getEdgeLabelIdOrNull(label);
Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
degree, skipDegree, capacity,
NO_LIMIT);
while (true) {
traverser.forward();
Map<Id, NodeWithWeight> results = traverser.shortestPaths();
if (results.containsKey(targetV) || traverser.done()) {
this.vertexIterCounter.addAndGet(traverser.vertexCount);
this.edgeIterCounter.addAndGet(traverser.edgeCount);
NodeWithWeight nodeWithWeight = results.get(targetV);
if (nodeWithWeight != null) {
Iterator<Id> vertexIter = nodeWithWeight.node.path().iterator();
Set<Edge> edges = traverser.edgeRecord.getEdges(vertexIter);
nodeWithWeight.setEdges(edges);
}
return nodeWithWeight;
}
checkCapacity(traverser.capacity, traverser.size, "shortest path");
}
}
public static class NodeWithWeight implements Comparable<NodeWithWeight> {
private final double weight;
private final Node node;
private Set<Edge> edges = Collections.emptySet();
public NodeWithWeight(double weight, Node node) {
this.weight = weight;
this.node = node;
}
public NodeWithWeight(double weight, Id id, NodeWithWeight prio) {
this(weight, new Node(id, prio.node()));
}
public Set<Edge> getEdges() {
return edges;
}
public void setEdges(Set<Edge> edges) {
this.edges = edges;
}
public double weight() {
return weight;
}
public Node node() {
return this.node;
}
public Map<String, Object> toMap() {
return ImmutableMap.of("weight", this.weight,
"vertices", this.node().path());
}
@Override
public int compareTo(NodeWithWeight other) {
return Double.compare(this.weight, other.weight);
}
}
public static class WeightedPaths extends LinkedHashMap<Id, NodeWithWeight> {
private static final long serialVersionUID = -313873642177730993L;
private Set<Edge> edges = Collections.emptySet();
public Set<Edge> getEdges() {
return edges;
}
public void setEdges(Set<Edge> edges) {
this.edges = edges;
}
public Set<Id> vertices() {
Set<Id> vertices = newIdSet();
vertices.addAll(this.keySet());
for (NodeWithWeight nw : this.values()) {
vertices.addAll(nw.node().path());
}
return vertices;
}
public List<List<Id>> pathList() {
List<List<Id>> pathList = new ArrayList<>();
for (NodeWithWeight nw : this.values()) {
pathList.add(nw.node.path());
}
return pathList;
}
public Map<Id, Map<String, Object>> toMap() {
Map<Id, Map<String, Object>> results = newMap();
for (Map.Entry<Id, NodeWithWeight> entry : this.entrySet()) {
Id source = entry.getKey();
NodeWithWeight nw = entry.getValue();
Map<String, Object> result = nw.toMap();
results.put(source, result);
}
return results;
}
}
private class Traverser {
private final Directions direction;
private final Id label;
private final String weight;
private final long degree;
private final long skipDegree;
private final long capacity;
private final long limit;
private final WeightedPaths findingNodes = new WeightedPaths();
private final WeightedPaths foundNodes = new WeightedPaths();
private final EdgeRecord edgeRecord;
private final Id source;
private final long size;
private Set<NodeWithWeight> sources;
private long vertexCount;
private long edgeCount;
private boolean done = false;
public Traverser(Id sourceV, Directions dir, Id label, String weight,
long degree, long skipDegree, long capacity, long limit) {
this.source = sourceV;
this.sources = ImmutableSet.of(new NodeWithWeight(
0D, new Node(sourceV, null)));
this.direction = dir;
this.label = label;
this.weight = weight;
this.degree = degree;
this.skipDegree = skipDegree;
this.capacity = capacity;
this.limit = limit;
this.size = 0L;
this.vertexCount = 0L;
this.edgeCount = 0L;
this.edgeRecord = new EdgeRecord(false);
}
/**
* Search forward from source
*/
public void forward() {
long degree = this.skipDegree > 0L ? this.skipDegree : this.degree;
for (NodeWithWeight node : this.sources) {
Iterator<Edge> edges = edgesOfVertex(node.node().id(),
this.direction,
this.label, degree);
edges = this.skipSuperNodeIfNeeded(edges);
while (edges.hasNext()) {
HugeEdge edge = (HugeEdge) edges.next();
Id target = edge.id().otherVertexId();
this.edgeCount += 1L;
if (this.foundNodes.containsKey(target) ||
this.source.equals(target)) {
// Already find shortest path for target, skip
continue;
}
this.edgeRecord.addEdge(node.node().id(), target, edge);
double currentWeight = this.edgeWeight(edge);
double weight = currentWeight + node.weight();
NodeWithWeight nw = new NodeWithWeight(weight, target, node);
NodeWithWeight exist = this.findingNodes.get(target);
if (exist == null || weight < exist.weight()) {
/*
* There are 2 scenarios to update finding nodes:
* 1. The 'target' found first time, add current path
* 2. Already exist path for 'target' and current
* path is shorter, update path for 'target'
*/
this.findingNodes.put(target, nw);
}
}
}
this.vertexCount += sources.size();
Map<Id, NodeWithWeight> sorted = CollectionUtil.sortByValue(
this.findingNodes, true);
double minWeight = 0;
Set<NodeWithWeight> newSources = InsertionOrderUtil.newSet();
for (Map.Entry<Id, NodeWithWeight> entry : sorted.entrySet()) {
Id id = entry.getKey();
NodeWithWeight wn = entry.getValue();
if (minWeight == 0) {
minWeight = wn.weight();
} else if (wn.weight() > minWeight) {
break;
}
// Move shortest paths from 'findingNodes' to 'foundNodes'
this.foundNodes.put(id, wn);
if (this.limit != NO_LIMIT &&
this.foundNodes.size() >= this.limit) {
this.done = true;
return;
}
this.findingNodes.remove(id);
// Re-init 'sources'
newSources.add(wn);
}
this.sources = newSources;
if (this.sources.isEmpty()) {
this.done = true;
}
}
public boolean done() {
return this.done;
}
public WeightedPaths shortestPaths() {
return this.foundNodes;
}
private double edgeWeight(HugeEdge edge) {
double edgeWeight;
if (this.weight == null ||
!edge.property(this.weight).isPresent()) {
edgeWeight = 1.0;
} else {
edgeWeight = NumericUtil.convertToNumber(
edge.value(this.weight)).doubleValue();
}
return edgeWeight;
}
private Iterator<Edge> skipSuperNodeIfNeeded(Iterator<Edge> edges) {
if (this.skipDegree <= 0L) {
return edges;
}
List<Edge> edgeList = newList();
int count = 0;
while (edges.hasNext()) {
if (count < this.degree) {
edgeList.add(edges.next());
}
if (count >= this.skipDegree) {
return QueryResults.emptyIterator();
}
count++;
}
return edgeList.iterator();
}
}
}