protected Integer triangleCount()

in computer-algorithm/src/main/java/org/apache/hugegraph/computer/algorithm/community/trianglecount/TriangleCount.java [67:103]


    protected Integer triangleCount(ComputationContext context, Vertex vertex,
                                    Iterator<IdList> messages, IdSet neighbors) {
        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.value()) {
                context.sendMessage(targetId, neighbors);
            }
        } else if (context.superstep() == 2) {
            int count = 0;

            while (messages.hasNext()) {
                IdList twoDegreeNeighbors = messages.next();
                for (Id twoDegreeNeighbor : twoDegreeNeighbors.values()) {
                    if (neighbors.contains(twoDegreeNeighbor)) {
                        count++;
                    }
                }
            }

            return count >> 1;
        }
        return null;
    }