use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class JaccardSimilarTraverser method jaccardSimilarity.
public double jaccardSimilarity(Id vertex, Id other, Directions dir, String label, long degree) {
E.checkNotNull(vertex, "vertex id");
E.checkNotNull(other, "the other vertex id");
this.checkVertexExist(vertex, "vertex");
this.checkVertexExist(other, "other vertex");
E.checkNotNull(dir, "direction");
checkDegree(degree);
Id labelId = this.getEdgeLabelId(label);
Set<Id> sourceNeighbors = IteratorUtils.set(this.adjacentVertices(vertex, dir, labelId, degree));
Set<Id> targetNeighbors = IteratorUtils.set(this.adjacentVertices(other, dir, labelId, degree));
return jaccardSimilarity(sourceNeighbors, targetNeighbors);
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class JaccardSimilarTraverser method jaccardSimilarsConcurrent.
public Map<Id, Double> jaccardSimilarsConcurrent(Id source, EdgeStep step, long capacity) {
AtomicLong count = new AtomicLong(0L);
Set<Id> accessed = ConcurrentHashMap.newKeySet();
accessed.add(source);
reachCapacity(count.incrementAndGet(), capacity);
// Query neighbors
Set<Id> layer1s = this.adjacentVertices(source, step);
reachCapacity(count.get() + layer1s.size(), capacity);
count.addAndGet(layer1s.size());
if (layer1s.isEmpty()) {
return ImmutableMap.of();
}
Map<Id, Double> results = new ConcurrentHashMap<>();
Set<Id> layer2All = ConcurrentHashMap.newKeySet();
this.traverseIds(layer1s.iterator(), id -> {
// Skip if accessed already
if (accessed.contains(id)) {
return;
}
Set<Id> layer2s = this.adjacentVertices(id, step);
if (layer2s.isEmpty()) {
results.put(id, 0.0D);
}
layer2All.addAll(layer2s);
reachCapacity(count.get() + layer2All.size(), capacity);
double jaccardSimilarity = this.jaccardSimilarity(layer1s, layer2s);
results.put(id, jaccardSimilarity);
accessed.add(id);
});
count.addAndGet(layer2All.size());
this.traverseIds(layer2All.iterator(), id -> {
// Skip if accessed already
if (accessed.contains(id)) {
return;
}
Set<Id> layer3s = this.adjacentVertices(id, step);
reachCapacity(count.get() + layer3s.size(), capacity);
if (layer3s.isEmpty()) {
results.put(id, 0.0D);
}
double jaccardSimilarity = this.jaccardSimilarity(layer1s, layer3s);
results.put(id, jaccardSimilarity);
accessed.add(id);
});
return results;
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class KoutTraverser method kout.
public Set<Id> kout(Id sourceV, Directions dir, String label, int depth, boolean nearest, long degree, long capacity, long limit) {
E.checkNotNull(sourceV, "source vertex id");
this.checkVertexExist(sourceV, "source vertex");
E.checkNotNull(dir, "direction");
checkPositive(depth, "k-out max_depth");
checkDegree(degree);
checkCapacity(capacity);
checkLimit(limit);
if (capacity != NO_LIMIT) {
// Capacity must > limit because sourceV is counted in capacity
E.checkArgument(capacity >= limit && limit != NO_LIMIT, "Capacity can't be less than limit, " + "but got capacity '%s' and limit '%s'", capacity, limit);
}
Id labelId = this.getEdgeLabelId(label);
Set<Id> latest = newIdSet();
latest.add(sourceV);
Set<Id> all = newIdSet();
all.add(sourceV);
long remaining = capacity == NO_LIMIT ? NO_LIMIT : capacity - latest.size();
while (depth-- > 0) {
// Just get limit nodes in last layer if limit < remaining capacity
if (depth == 0 && limit != NO_LIMIT && (limit < remaining || remaining == NO_LIMIT)) {
remaining = limit;
}
if (nearest) {
latest = this.adjacentVertices(sourceV, latest, dir, labelId, all, degree, remaining);
all.addAll(latest);
} else {
latest = this.adjacentVertices(sourceV, latest, dir, labelId, null, degree, remaining);
}
if (capacity != NO_LIMIT) {
// Update 'remaining' value to record remaining capacity
remaining -= latest.size();
if (remaining <= 0 && depth > 0) {
throw new HugeException("Reach capacity '%s' while remaining depth '%s'", capacity, depth);
}
}
}
return latest;
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class KoutTraverser method customizedKout.
public KoutRecords customizedKout(Id source, EdgeStep step, int maxDepth, boolean nearest, long capacity, long limit) {
E.checkNotNull(source, "source vertex id");
this.checkVertexExist(source, "source vertex");
checkPositive(maxDepth, "k-out max_depth");
checkCapacity(capacity);
checkLimit(limit);
long[] depth = new long[1];
depth[0] = maxDepth;
boolean concurrent = maxDepth >= this.concurrentDepth();
KoutRecords records = new KoutRecords(concurrent, source, nearest);
Consumer<Id> consumer = v -> {
if (this.reachLimit(limit, depth[0], records.size())) {
return;
}
Iterator<Edge> edges = edgesOfVertex(v, step);
while (!this.reachLimit(limit, depth[0], records.size()) && edges.hasNext()) {
Id target = ((HugeEdge) edges.next()).id().otherVertexId();
records.addPath(v, target);
this.checkCapacity(capacity, records.accessed(), depth[0]);
}
};
while (depth[0]-- > 0) {
records.startOneLayer(true);
this.traverseIds(records.keys(), consumer, concurrent);
records.finishOneLayer();
}
return records;
}
use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.
the class NeighborRankTraverser method contributePrevLayers.
private void contributePrevLayers(List<Ranks> ranks, double incr, Map<Integer, Set<Id>> prevLayerNodesV) {
for (Map.Entry<Integer, Set<Id>> e : prevLayerNodesV.entrySet()) {
Ranks prevLayerRanks = ranks.get(e.getKey());
for (Id node : e.getValue()) {
double oldRank = prevLayerRanks.get(node);
prevLayerRanks.put(node, oldRank + incr);
}
}
}
Aggregations