All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.gds.closeness.ClosenessCentrality Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) "Neo4j"
 * Neo4j Sweden AB [http://neo4j.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.gds.closeness;

import org.neo4j.gds.Algorithm;
import org.neo4j.gds.api.Graph;
import org.neo4j.gds.collections.ha.HugeDoubleArray;
import org.neo4j.gds.collections.haa.HugeAtomicIntArray;
import org.neo4j.gds.core.concurrency.Concurrency;
import org.neo4j.gds.core.concurrency.ParallelUtil;
import org.neo4j.gds.core.utils.paged.ParallelIntPageCreator;
import org.neo4j.gds.core.utils.partition.PartitionUtils;
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
import org.neo4j.gds.msbfs.BfsConsumer;
import org.neo4j.gds.msbfs.MultiSourceBFSAccessMethods;
import org.neo4j.gds.termination.TerminationFlag;

import java.util.Optional;
import java.util.concurrent.ExecutorService;

/**
 * Normalized Closeness Centrality
 *
 * Utilizes the MSBFS for counting the farness between nodes.
 * See MSBFS documentation.
 */
public final class ClosenessCentrality extends Algorithm {

    private final Graph graph;
    private final long nodeCount;
    private final Concurrency concurrency;
    private final ExecutorService executorService;
    private final HugeAtomicIntArray farness;
    private final HugeAtomicIntArray component;
    private final CentralityComputer centralityComputer;

    public ClosenessCentrality(
        Graph graph,
        Concurrency concurrency,
        CentralityComputer centralityComputer,
        ExecutorService executorService,
        ProgressTracker progressTracker,
        TerminationFlag terminationFlag
    ) {
        super(progressTracker);
        this.graph = graph;
        this.nodeCount = graph.nodeCount();
        this.concurrency = concurrency;
        this.executorService = executorService;
        this.centralityComputer = centralityComputer;
        this.farness = HugeAtomicIntArray.of(nodeCount, ParallelIntPageCreator.of(concurrency));
        this.component = HugeAtomicIntArray.of(nodeCount, ParallelIntPageCreator.of(concurrency));

        this.terminationFlag = terminationFlag;
    }

    @Override
    public ClosenessCentralityResult compute() {
        progressTracker.beginSubTask();
        computeFarness();
        var centralities = computeCloseness();
        progressTracker.endSubTask();

        return new ClosenessCentralityResult(centralities);
    }

    private void computeFarness() {
        progressTracker.beginSubTask();
        final BfsConsumer consumer = (nodeId, depth, sourceNodeIds) -> {
            int len = sourceNodeIds.size();
            farness.getAndAdd(nodeId, len * depth);
            component.getAndAdd(nodeId, len);
            progressTracker.logProgress(len);
        };
        MultiSourceBFSAccessMethods
            .aggregatedNeighborProcessing(
                nodeCount,
                graph,
                consumer,
                Optional.empty(),
                terminationFlag
            )
            .run(concurrency, executorService);
        progressTracker.endSubTask();
    }

    private HugeDoubleArray computeCloseness() {
        progressTracker.beginSubTask();

        var closeness = HugeDoubleArray.newArray(nodeCount);

        var tasks = PartitionUtils.rangePartition(
            concurrency,
            nodeCount,
            partition -> (Runnable) () -> {
                partition.consume(nodeId -> closeness.set(nodeId, centralityComputer.centrality(
                    farness.get(nodeId),
                    component.get(nodeId)
                )));
                progressTracker.logProgress(partition.nodeCount());
            },
            Optional.empty()
        );

        ParallelUtil.run(tasks, executorService);

        progressTracker.endSubTask();

        return closeness;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy