Skip to content

Commit

Permalink
Use dedicated PageRank variant configs
Browse files Browse the repository at this point in the history
  • Loading branch information
vnickolov committed Sep 17, 2024
1 parent 939c441 commit c68167f
Show file tree
Hide file tree
Showing 49 changed files with 1,186 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package org.neo4j.gds.algorithms.centrality;

import org.neo4j.gds.core.concurrency.DefaultPool;
import org.neo4j.gds.pagerank.PageRankConfig;
import org.neo4j.gds.pagerank.PageRankResult;
import org.neo4j.gds.pagerank.RankConfig;
import org.neo4j.gds.result.CentralityStatistics;
import org.neo4j.gds.scaling.LogScaler;

Expand All @@ -37,7 +37,7 @@ private PageRankDistributionComputer() {}

public static PageRankDistribution computeDistribution(
PageRankResult result,
PageRankConfig configuration,
RankConfig configuration,
boolean shouldComputeCentralityDistribution
) {
Map<String, Object> centralitySummary = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.pagerank;

import com.carrotsearch.hppc.LongScatterSet;
import org.neo4j.gds.GraphAlgorithmFactory;
import org.neo4j.gds.api.Graph;
import org.neo4j.gds.beta.pregel.Pregel;
import org.neo4j.gds.core.concurrency.DefaultPool;
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
import org.neo4j.gds.core.utils.progress.tasks.Task;
import org.neo4j.gds.mem.MemoryEstimation;
import org.neo4j.gds.termination.TerminationFlag;

import static org.neo4j.gds.pagerank.PageRankVariant.ARTICLE_RANK;

public class ArticleRankAlgorithmFactory<C extends ArticleRankConfig> extends GraphAlgorithmFactory<PageRankAlgorithm<C>, C> {

@Override
public String taskName() {
return ARTICLE_RANK.taskName();
}

@Override
public PageRankAlgorithm<C> build(
Graph graph,
C configuration,
ProgressTracker progressTracker
) {

var degreeFunction = DegreeFunctions.pageRankDegreeFunction(
graph,
configuration.hasRelationshipWeightProperty(), configuration.concurrency()
);

var mappedSourceNodes = new LongScatterSet(configuration.sourceNodes().size());
configuration.sourceNodes().stream()
.mapToLong(graph::toMappedNodeId)
.forEach(mappedSourceNodes::add);

double avgDegree = DegreeFunctions.averageDegree(graph, configuration.concurrency());
var computation = new ArticleRankComputation<>(configuration, mappedSourceNodes, degreeFunction, avgDegree);

return new PageRankAlgorithm<>(
graph,
configuration,
computation,
ARTICLE_RANK,
DefaultPool.INSTANCE,
progressTracker,
TerminationFlag.RUNNING_TRUE
);
}

@Override
public Task progressTask(Graph graph, C config) {
return Pregel.progressTask(graph, config, taskName());
}

@Override
public MemoryEstimation memoryEstimation(C configuration) {
return new PageRankMemoryEstimateDefinition().memoryEstimation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Optional;
import java.util.function.LongToDoubleFunction;

public final class ArticleRankComputation implements PregelComputation<PageRankConfig> {
public final class ArticleRankComputation<C extends ArticleRankConfig> implements PregelComputation<C> {

private static final String PAGE_RANK = "pagerank";

Expand All @@ -46,7 +46,7 @@ public final class ArticleRankComputation implements PregelComputation<PageRankC
private final double averageDegree;

public ArticleRankComputation(
PageRankConfig config,
C config,
LongSet sourceNodes,
LongToDoubleFunction degreeFunction,
double averageDegree
Expand All @@ -61,24 +61,24 @@ public ArticleRankComputation(
}

@Override
public PregelSchema schema(PageRankConfig config) {
public PregelSchema schema(C config) {
return new PregelSchema.Builder().add(PAGE_RANK, ValueType.DOUBLE).build();
}

@Override
public void init(InitContext<PageRankConfig> context) {
public void init(InitContext<C> context) {
context.setNodeValue(PAGE_RANK, initialValue(context));
}

private double initialValue(InitContext<PageRankConfig> context) {
private double initialValue(InitContext<C> context) {
if (!hasSourceNodes || sourceNodes.contains(context.nodeId())) {
return alpha;
}
return 0;
}

@Override
public void compute(ComputeContext<PageRankConfig> context, Messages messages) {
public void compute(ComputeContext<C> context, Messages messages) {
double rank = context.doubleNodeValue(PAGE_RANK);
double delta = rank;

Expand Down
31 changes: 31 additions & 0 deletions algo/src/main/java/org/neo4j/gds/pagerank/ArticleRankConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.pagerank;

import org.neo4j.gds.annotation.Configuration;

@Configuration("ArticleRankConfigImpl")
public interface ArticleRankConfig extends RankConfig
{
@Configuration.DoubleRange(min = 0, max = 1, maxInclusive = false)
default double dampingFactor() {
return 0.85;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.pagerank;

import org.neo4j.gds.annotation.Configuration;
import org.neo4j.gds.config.MutateNodePropertyConfig;
import org.neo4j.gds.core.CypherMapWrapper;

@Configuration
public interface ArticleRankMutateConfig extends ArticleRankConfig, MutateNodePropertyConfig {

static ArticleRankMutateConfig of(CypherMapWrapper userInput) {
return new ArticleRankMutateConfigImpl(userInput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.pagerank;

import org.neo4j.gds.annotation.Configuration;
import org.neo4j.gds.core.CypherMapWrapper;

@Configuration
public interface ArticleRankStatsConfig extends ArticleRankConfig {

public static ArticleRankStatsConfig of(CypherMapWrapper userInput) {
return new ArticleRankStatsConfigImpl(userInput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.pagerank;

import org.neo4j.gds.annotation.Configuration;
import org.neo4j.gds.core.CypherMapWrapper;

@Configuration
public interface ArticleRankStreamConfig extends ArticleRankConfig {

public static ArticleRankStreamConfig of(CypherMapWrapper userInput) {
return new ArticleRankStreamConfigImpl(userInput);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gds.pagerank;

import org.neo4j.gds.annotation.Configuration;
import org.neo4j.gds.config.WritePropertyConfig;
import org.neo4j.gds.core.CypherMapWrapper;

@Configuration
public interface ArticleRankWriteConfig extends ArticleRankConfig, WritePropertyConfig {

public static ArticleRankWriteConfig of(CypherMapWrapper userInput) {
return new ArticleRankWriteConfigImpl(userInput);
}
}
Loading

0 comments on commit c68167f

Please sign in to comment.