Search in sources :

Example 26 with Id

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();
    }
}
Also used : Id(com.baidu.hugegraph.computer.core.graph.id.Id)

Example 27 with Id

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();
}
Also used : Id(com.baidu.hugegraph.computer.core.graph.id.Id)

Example 28 with Id

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);
}
Also used : HashMap(java.util.HashMap) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.computer.core.graph.id.Id) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with Id

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);
}
Also used : Id(com.baidu.hugegraph.computer.core.graph.id.Id)

Example 30 with Id

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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Id(com.baidu.hugegraph.computer.core.graph.id.Id) IdList(com.baidu.hugegraph.computer.core.graph.value.IdList)

Aggregations

Id (com.baidu.hugegraph.computer.core.graph.id.Id)74 BytesId (com.baidu.hugegraph.computer.core.graph.id.BytesId)51 Test (org.junit.Test)29 IdList (com.baidu.hugegraph.computer.core.graph.value.IdList)18 Edge (com.baidu.hugegraph.computer.core.graph.edge.Edge)12 DoubleValue (com.baidu.hugegraph.computer.core.graph.value.DoubleValue)12 LongValue (com.baidu.hugegraph.computer.core.graph.value.LongValue)12 Vertex (com.baidu.hugegraph.computer.core.graph.vertex.Vertex)12 ComputerContext (com.baidu.hugegraph.computer.core.common.ComputerContext)11 GraphFactory (com.baidu.hugegraph.computer.core.graph.GraphFactory)10 KvEntry (com.baidu.hugegraph.computer.core.store.entry.KvEntry)9 ConnectionId (com.baidu.hugegraph.computer.core.network.ConnectionId)8 IdListList (com.baidu.hugegraph.computer.core.graph.value.IdListList)7 Properties (com.baidu.hugegraph.computer.core.graph.properties.Properties)6 File (java.io.File)6 Value (com.baidu.hugegraph.computer.core.graph.value.Value)5 BytesInput (com.baidu.hugegraph.computer.core.io.BytesInput)5 Config (com.baidu.hugegraph.computer.core.config.Config)4 BooleanValue (com.baidu.hugegraph.computer.core.graph.value.BooleanValue)4 FloatValue (com.baidu.hugegraph.computer.core.graph.value.FloatValue)4