Skip to content

Commit

Permalink
add with_boundary parameter for betweeness (#42)
Browse files Browse the repository at this point in the history
* improve betweeness by remove boundary vertex of path

Change-Id: I76924daf8d9da113ab7a1aeac536c6080eccb296

* add with_boundary parameter for betweeness

also fix lpa count comms with limit

Change-Id: Iaf675cd87a8dc0b5ef75476144bc8141f2dd4385
  • Loading branch information
javeme committed Oct 19, 2022
1 parent 199fb16 commit 7c665f3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -454,6 +455,16 @@ protected <V> V execute(GraphTraversal<?, ?> traversal,
}
}

protected <V extends Number> Number tryNext(GraphTraversal<?, V> iter) {
return this.execute(iter, () -> {
try {
return iter.next();
} catch (NoSuchElementException e) {
return 0;
}
});
}

protected void commitIfNeeded() {
// commit if needed
Transaction tx = this.graph().tx();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package com.baidu.hugegraph.job.algorithm.cent;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand All @@ -32,15 +34,21 @@
import org.apache.tinkerpop.gremlin.structure.Column;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.iterator.MapperIterator;
import com.baidu.hugegraph.job.UserJob;
import com.baidu.hugegraph.job.algorithm.AbstractAlgorithm;
import com.baidu.hugegraph.structure.HugeElement;
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.Log;

public abstract class AbstractCentAlgorithm extends AbstractAlgorithm {

private static final Logger LOG = Log.logger(AbstractCentAlgorithm.class);

@Override
public String category() {
return CATEGORY_CENT;
Expand Down Expand Up @@ -161,6 +169,33 @@ protected <V> GraphTraversal<V, V> filterNonShortestPath(
});
}

protected GraphTraversal<Vertex, Id> substractPath(
GraphTraversal<Vertex, Vertex> t,
boolean withBoundary) {
// t.select(Pop.all, "v").unfold().id()
return t.select(Pop.all, "v").flatMap(it -> {
List<?> path = (List<?>) it.get();
if (withBoundary) {
@SuppressWarnings("unchecked")
Iterator<HugeVertex> items = (Iterator<HugeVertex>)
path.iterator();
return new MapperIterator<>(items, v -> v.id());
}
int len = path.size();
if (len < 3) {
return Collections.emptyIterator();
}

LOG.debug("CentAlgorithm substract path: {}", path);
path.remove(path.size() -1);
path.remove(0);
@SuppressWarnings("unchecked")
Iterator<HugeVertex> items = (Iterator<HugeVertex>)
path.iterator();
return new MapperIterator<>(items, v -> v.id());
});
}

protected GraphTraversal<Vertex, ?> topN(GraphTraversal<Vertex, ?> t,
long topN) {
if (topN > 0L || topN == NO_LIMIT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@
import java.util.Map;

import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Pop;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import com.baidu.hugegraph.job.UserJob;
import com.baidu.hugegraph.type.define.Directions;
import com.baidu.hugegraph.util.ParameterUtil;

public class BetweenessCentralityAlgorithm extends AbstractCentAlgorithm {

public static final String KEY_WITH_BOUNDARY = "with_boundary";

@Override
public String name() {
return "betweeness_centrality";
}

@Override
public void checkParameters(Map<String, Object> parameters) {
super.checkParameters(parameters);
withBoundary(parameters);
}

@Override
public Object call(UserJob<Object> job, Map<String, Object> parameters) {
try (Traverser traverser = new Traverser(job)) {
Expand All @@ -45,13 +53,21 @@ public Object call(UserJob<Object> job, Map<String, Object> parameters) {
depth(parameters),
degree(parameters),
sample(parameters),
withBoundary(parameters),
sourceLabel(parameters),
sourceSample(parameters),
sourceCLabel(parameters),
top(parameters));
}
}

protected static boolean withBoundary(Map<String, Object> parameters) {
if (!parameters.containsKey(KEY_WITH_BOUNDARY)) {
return false;
}
return ParameterUtil.parameterBoolean(parameters, KEY_WITH_BOUNDARY);
}

private static class Traverser extends AbstractCentAlgorithm.Traverser {

public Traverser(UserJob<Object> job) {
Expand All @@ -63,6 +79,7 @@ public Object betweenessCentrality(Directions direction,
int depth,
long degree,
long sample,
boolean withBoundary,
String sourceLabel,
long sourceSample,
String sourceCLabel,
Expand All @@ -79,8 +96,8 @@ public Object betweenessCentrality(Directions direction,
t = t.emit().until(__.loops().is(P.gte(depth)));
t = filterNonShortestPath(t, false);

GraphTraversal<Vertex, ?> tg = t.select(Pop.all, "v")
.unfold().id().groupCount();
GraphTraversal<Vertex, ?> tg = this.substractPath(t, withBoundary)
.groupCount();
GraphTraversal<Vertex, ?> tLimit = topN(tg, topN);

return this.execute(tLimit, () -> tLimit.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -649,16 +648,6 @@ private double modularity(String label) {
return q;
}

private <V extends Number> Number tryNext(GraphTraversal<?, V> iter) {
return this.execute(iter, () -> {
try {
return iter.next();
} catch (NoSuchElementException e) {
return 0;
}
});
}

public Collection<Object> showCommunity(String community) {
final String C_PASS0 = labelOfPassN(0);
Collection<Object> comms = Arrays.asList(community);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public Object lpa(String sourceLabel, String edgeLabel,
}
}

long communities = this.graph().traversal().V().limit(100000L)
.groupCount().by(C_LABEL)
.count(Scope.local).next();
Number communities = tryNext(this.graph().traversal().V()
.groupCount().by(C_LABEL)
.count(Scope.local));
return ImmutableMap.of("iteration_times", times,
"last_precision", changedPercent,
"times", maxTimes,
Expand Down

0 comments on commit 7c665f3

Please sign in to comment.