use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class Kcore method compute.
@Override
public void compute(ComputationContext context, Vertex vertex, Iterator<Id> messages) {
KcoreValue value = vertex.value();
if (!value.active()) {
// Ignore messages of deleted vertex
vertex.inactivate();
return;
}
int superstep = context.superstep();
if (superstep <= 2) {
int deleted = Iterators.size(messages);
assert value.active();
if (value.decreaseDegree(deleted) < this.k) {
// From active to inactive, delete self vertex
value.degree(0);
if (superstep == 1) {
context.sendMessageToAllEdges(vertex, vertex.id());
}
vertex.inactivate();
} else {
// From active to active, do wcc from superstep 2
if (superstep == 2) {
// Start wcc
context.sendMessageToAllEdgesIf(vertex, vertex.id(), (source, target) -> {
return source.compareTo(target) < 0;
});
vertex.inactivate();
} else {
// Keep active at superstep 1 to continue superstep 2
assert superstep == 1;
assert vertex.active();
}
}
} else {
// Do wcc
assert superstep > 2;
Id message = Combiner.combineAll(context.combiner(), messages);
if (value.core().compareTo(message) > 0) {
value.core(message);
context.sendMessageToAllEdges(vertex, message);
}
vertex.inactivate();
}
}
use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class Lpa method compute.
@Override
public void compute(ComputationContext context, Vertex vertex, Iterator<Id> messages) {
Id label = this.voteLabel(messages);
Id value = vertex.value();
if (!value.equals(label)) {
vertex.value(label);
context.sendMessageToAllEdges(vertex, label);
}
vertex.inactivate();
}
use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class Lpa method voteLabel.
private Id voteLabel(Iterator<Id> messages) {
// Calculate label frequency
Map<Id, MutableInt> labels = new HashMap<>();
assert messages.hasNext();
while (messages.hasNext()) {
Id label = messages.next();
MutableInt labelCount = labels.get(label);
if (labelCount != null) {
labelCount.increment();
} else {
labels.put(label, new MutableInt(1));
}
}
// Calculate the labels with maximum frequency
List<Id> maxLabels = new ArrayList<>();
int maxFreq = 1;
for (Map.Entry<Id, MutableInt> e : labels.entrySet()) {
int value = e.getValue().intValue();
if (value > maxFreq) {
maxFreq = value;
maxLabels.clear();
}
if (value == maxFreq) {
maxLabels.add(e.getKey());
}
}
// Random choice
int selected = this.random.nextInt(maxLabels.size());
return maxLabels.get(selected);
}
use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class Lpa method compute0.
@Override
public void compute0(ComputationContext context, Vertex vertex) {
Id value = vertex.id();
vertex.value(value);
vertex.inactivate();
context.sendMessageToAllEdges(vertex, value);
}
use of com.baidu.hugegraph.computer.core.graph.id.Id in project hugegraph-computer by hugegraph.
the class TriangleCount method triangleCount.
private Integer triangleCount(ComputationContext context, Vertex vertex, Iterator<IdList> messages) {
IdList neighbors = ((TriangleCountValue) vertex.value()).idList();
if (context.superstep() == 1) {
// Collect outgoing neighbors
Set<Id> outNeighbors = getOutNeighbors(vertex);
neighbors.addAll(outNeighbors);
// Collect incoming neighbors
while (messages.hasNext()) {
IdList idList = messages.next();
assert idList.size() == 1;
Id inId = idList.get(0);
if (!outNeighbors.contains(inId)) {
neighbors.add(inId);
}
}
// Send all neighbors to neighbors
for (Id targetId : neighbors.values()) {
context.sendMessage(targetId, neighbors);
}
} else if (context.superstep() == 2) {
int count = 0;
Set<Id> allNeighbors = new HashSet<>(neighbors.values());
while (messages.hasNext()) {
IdList twoDegreeNeighbors = messages.next();
for (Id twoDegreeNeighbor : twoDegreeNeighbors.values()) {
if (allNeighbors.contains(twoDegreeNeighbor)) {
count++;
}
}
}
return count >> 1;
}
return null;
}
Aggregations