Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Id> {

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<Id> 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<Id> messages) {
// Calculate label frequency
Map<Id, MutableInt> 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<Id> maxLabels = new ArrayList<>();
int maxFreq = 1;
for (Map.Entry<Id, MutableInt> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,20 @@
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)
@Suite.SuiteClasses({
PageRankTest.class,
DegreeCentralityTest.class,
WccTest.class,
LpaTest.class,
TriangleCountTest.class,
RingsDetectionTest.class,
RingsDetectionWithFilterTest.class
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}