diff --git a/computer-algorithm/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/Lpa.java b/computer-algorithm/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/Lpa.java new file mode 100644 index 000000000..063e9bf18 --- /dev/null +++ b/computer-algorithm/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/Lpa.java @@ -0,0 +1,101 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.computer.algorithm.community.lpa; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Random; + +import org.apache.commons.lang3.mutable.MutableInt; + +import com.baidu.hugegraph.computer.core.graph.id.Id; +import com.baidu.hugegraph.computer.core.graph.vertex.Vertex; +import com.baidu.hugegraph.computer.core.worker.Computation; +import com.baidu.hugegraph.computer.core.worker.ComputationContext; + +public class Lpa implements Computation { + + private final Random random = new Random(); + + @Override + public String name() { + return "lpa"; + } + + @Override + public String category() { + return "community"; + } + + @Override + public void compute0(ComputationContext context, Vertex vertex) { + Id value = vertex.id(); + vertex.value(value); + vertex.inactivate(); + context.sendMessageToAllEdges(vertex, value); + } + + @Override + public void compute(ComputationContext context, Vertex vertex, + Iterator messages) { + Id label = this.voteLabel(messages); + Id value = vertex.value(); + if (!value.equals(label)) { + vertex.value(label); + context.sendMessageToAllEdges(vertex, label); + } + vertex.inactivate(); + } + + private Id voteLabel(Iterator messages) { + // Calculate label frequency + Map labels = new HashMap<>(); + 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 maxLabels = new ArrayList<>(); + int maxFreq = 1; + for (Map.Entry 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); + } +} diff --git a/computer-algorithm/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/LpaParams.java b/computer-algorithm/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/LpaParams.java new file mode 100644 index 000000000..93de697e2 --- /dev/null +++ b/computer-algorithm/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/LpaParams.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.computer.algorithm.community.lpa; + +import java.util.Map; + +import com.baidu.hugegraph.computer.algorithm.AlgorithmParams; +import com.baidu.hugegraph.computer.core.config.ComputerOptions; +import com.baidu.hugegraph.computer.core.graph.id.BytesId; +import com.baidu.hugegraph.computer.core.output.LimitedLogOutput; + +public class LpaParams implements AlgorithmParams { + + @Override + public void setAlgorithmParameters(Map params) { + this.setIfAbsent(params, ComputerOptions.WORKER_COMPUTATION_CLASS, + Lpa.class.getName()); + this.setIfAbsent(params, ComputerOptions.ALGORITHM_RESULT_CLASS, + BytesId.class.getName()); + this.setIfAbsent(params, ComputerOptions.ALGORITHM_MESSAGE_CLASS, + BytesId.class.getName()); + this.setIfAbsent(params, ComputerOptions.OUTPUT_CLASS, + LimitedLogOutput.class.getName()); + } +} diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/AlgorithmTestSuite.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/AlgorithmTestSuite.java index c82700304..38bc20296 100644 --- a/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/AlgorithmTestSuite.java +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/AlgorithmTestSuite.java @@ -24,11 +24,12 @@ import org.junit.runners.Suite; import com.baidu.hugegraph.computer.algorithm.centrality.degree.DegreeCentralityTest; +import com.baidu.hugegraph.computer.algorithm.centrality.pagerank.PageRankTest; +import com.baidu.hugegraph.computer.algorithm.community.lpa.LpaTest; import com.baidu.hugegraph.computer.algorithm.community.trianglecount.TriangleCountTest; +import com.baidu.hugegraph.computer.algorithm.community.wcc.WccTest; import com.baidu.hugegraph.computer.algorithm.path.rings.RingsDetectionTest; import com.baidu.hugegraph.computer.algorithm.path.rings.RingsDetectionWithFilterTest; -import com.baidu.hugegraph.computer.algorithm.centrality.pagerank.PageRankTest; -import com.baidu.hugegraph.computer.algorithm.community.wcc.WccTest; import com.baidu.hugegraph.config.OptionSpace; @RunWith(Suite.class) @@ -36,6 +37,7 @@ PageRankTest.class, DegreeCentralityTest.class, WccTest.class, + LpaTest.class, TriangleCountTest.class, RingsDetectionTest.class, RingsDetectionWithFilterTest.class diff --git a/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/LpaTest.java b/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/LpaTest.java new file mode 100644 index 000000000..a5263d460 --- /dev/null +++ b/computer-test/src/main/java/com/baidu/hugegraph/computer/algorithm/community/lpa/LpaTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.computer.algorithm.community.lpa; + +import org.junit.Test; + +import com.baidu.hugegraph.computer.algorithm.AlgorithmTestBase; + +public class LpaTest extends AlgorithmTestBase { + + @Test + public void testRunAlgorithm() throws InterruptedException { + runAlgorithm(LpaParams.class.getName()); + } +}