public Object degreeCentrality()

in hugegraph-core/src/main/java/org/apache/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java [63:122]


        public Object degreeCentrality(Directions direction,
                                       String label,
                                       long topN) {
            if (direction == null || direction == Directions.BOTH) {
                return this.degreeCentralityForBothDir(label, topN);
            }
            assert direction == Directions.OUT || direction == Directions.IN;
            assert topN >= 0L || topN == NO_LIMIT;

            Iterator<Edge> edges = this.edges(direction);

            JsonMap degrees = new JsonMap();
            TopMap<Id> tops = new TopMap<>(topN);
            Id vertex = null;
            Id labelId = this.getEdgeLabelId(label);
            long degree = 0L;
            long totalEdges = 0L;

            degrees.startObject();
            while (edges.hasNext()) {
                HugeEdge edge = (HugeEdge) edges.next();
                this.updateProgress(++totalEdges);

                Id schemaLabel = edge.schemaLabel().id();
                if (labelId != null && !labelId.equals(schemaLabel)) {
                    continue;
                }

                Id source = edge.ownerVertex().id();
                if (source.equals(vertex)) {
                    // edges belong to same source vertex
                    degree++;
                    continue;
                }

                if (vertex != null) {
                    // next vertex found
                    if (topN <= 0L && topN != NO_LIMIT) {
                        degrees.append(vertex, degree);
                    } else {
                        tops.put(vertex, degree);
                    }
                }
                vertex = source;
                degree = 1L;
            }

            if (vertex != null) {
                if (topN <= 0L && topN != NO_LIMIT) {
                    degrees.append(vertex, degree);
                } else {
                    tops.put(vertex, degree);
                    degrees.append(tops.entrySet());
                }
            }

            degrees.endObject();

            return degrees.asJson();
        }