Skip to content

Commit

Permalink
add skipi_isolated param for louvain (#37)
Browse files Browse the repository at this point in the history
Change-Id: Ib24ede9c20bb2c23a3f06fe72c53be2342295fd4
  • Loading branch information
javeme committed Oct 19, 2022
1 parent 32bacdd commit 8e52966
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public abstract class AbstractAlgorithm implements Algorithm {
public static final String KEY_PRECISION = "precision";
public static final String KEY_SHOW_MOD= "show_modularity";
public static final String KEY_SHOW_COMM = "show_community";
public static final String KEY_SKIP_ISOLATED = "skip_isolated";
public static final String KEY_CLEAR = "clear";
public static final String KEY_CAPACITY = "capacity";
public static final String KEY_LIMIT = "limit";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void checkParameters(Map<String, Object> parameters) {
sourceCLabel(parameters);
showModularity(parameters);
showCommunity(parameters);
skipIsolated(parameters);
clearPass(parameters);
workers(parameters);
}
Expand All @@ -53,6 +54,7 @@ public Object call(UserJob<Object> job, Map<String, Object> parameters) {
String label = sourceLabel(parameters);
String clabel = sourceCLabel(parameters);
long degree = degree(parameters);
boolean skipIsolated = skipIsolated(parameters);
int workers = workers(parameters);

Long clearPass = clearPass(parameters);
Expand All @@ -61,7 +63,7 @@ public Object call(UserJob<Object> job, Map<String, Object> parameters) {

try (LouvainTraverser traverser = new LouvainTraverser(
job, workers, degree,
label, clabel)) {
label, clabel, skipIsolated)) {
if (clearPass != null) {
return traverser.clearPass(clearPass.intValue());
} else if (modPass != null) {
Expand Down Expand Up @@ -96,4 +98,11 @@ protected static Long showModularity(Map<String, Object> parameters) {
HugeTraverser.checkNonNegative(pass, KEY_SHOW_MOD);
return pass;
}

protected static boolean skipIsolated(Map<String, Object> parameters) {
if (!parameters.containsKey(KEY_SKIP_ISOLATED)) {
return true;
}
return ParameterUtil.parameterBoolean(parameters, KEY_SKIP_ISOLATED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,22 @@ public class LouvainTraverser extends AlgoTraverser {
private final String sourceLabel;
private final String sourceCLabel;
private final long degree;
private final boolean skipIsolated;

private final Cache cache;

private long m;
private String passLabel;

public LouvainTraverser(UserJob<Object> job, int workers, long degree,
String sourceLabel, String sourceCLabel) {
String sourceLabel, String sourceCLabel,
boolean skipIsolated) {
super(job, LouvainAlgorithm.ALGO_NAME, workers);
this.g = this.graph().traversal();
this.sourceLabel = sourceLabel;
this.sourceCLabel = sourceCLabel;
this.degree = degree;
this.skipIsolated = skipIsolated;
this.m = 1L;
this.passLabel = "";

Expand Down Expand Up @@ -355,6 +359,9 @@ private void doMoveCommunity(Vertex v, List<Edge> nbs, Community newC) {
private boolean moveCommunity(Vertex v, int pass) {
// move vertex to neighbor community if needed
List<Edge> nbs = neighbors((Id) v.id());
if (this.skipIsolated && pass == 0 && nbs.isEmpty()) {
return false;
}
Community c = communityOfVertex(v, nbs);
double ki = kinOfVertex(v) + weightOfVertex(v, nbs);
// update community of v if △Q changed
Expand Down Expand Up @@ -448,7 +455,7 @@ private void mergeCommunities(int pass) {
LOG.info("Merge community for pass {}", pass);
// merge each community as a vertex
Collection<Pair<Community, Set<Id>>> comms = this.cache.communities();
assert this.allMembersExist(comms, pass - 1);
assert this.skipIsolated || this.allMembersExist(comms, pass - 1);
this.cache.resetVertexWeight();

Consumers<Pair<Community, Set<Id>>> consumers = new Consumers<>(
Expand Down Expand Up @@ -479,7 +486,7 @@ private void mergeCommunities(int pass) {
}

this.graph().tx().commit();
assert this.allMembersExist(pass);
assert this.skipIsolated || this.allMembersExist(pass);

// reset communities
this.cache.reset();
Expand Down

0 comments on commit 8e52966

Please sign in to comment.