-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fusiform_similarity,rings_detect and kcore ap algorithm (#5)
* improve * move c_label to lower layer and add appendRow(value) * add community limit 100w for louvain * improve louvain log * fix louvain bug Change-Id: I886ac3e7a3f0dfd49e66fdf544f97f6f7db615df
- Loading branch information
Showing
8 changed files
with
623 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
286 changes: 286 additions & 0 deletions
286
hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/KCoreAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
/* | ||
* 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.job.algorithm.comm; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.collections.CollectionUtils; | ||
import org.apache.commons.lang3.mutable.MutableInt; | ||
import org.apache.tinkerpop.gremlin.structure.Vertex; | ||
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils; | ||
|
||
import com.baidu.hugegraph.HugeGraph; | ||
import com.baidu.hugegraph.backend.id.Id; | ||
import com.baidu.hugegraph.backend.query.Query; | ||
import com.baidu.hugegraph.job.Job; | ||
import com.baidu.hugegraph.schema.EdgeLabel; | ||
import com.baidu.hugegraph.traversal.algorithm.FusiformSimilarityTraverser; | ||
import com.baidu.hugegraph.type.define.Directions; | ||
import com.baidu.hugegraph.util.CollectionUtil; | ||
import com.baidu.hugegraph.util.E; | ||
import com.baidu.hugegraph.util.JsonUtil; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
public class KCoreAlgorithm extends AbstractCommAlgorithm { | ||
|
||
public static final String KEY_K = "k"; | ||
public static final String KEY_MERGED = "merged"; | ||
|
||
public static final int DEFAULT_K = 3; | ||
|
||
@Override | ||
public String name() { | ||
return "k_core"; | ||
} | ||
|
||
@Override | ||
public void checkParameters(Map<String, Object> parameters) { | ||
k(parameters); | ||
alpha(parameters); | ||
merged(parameters); | ||
degree(parameters); | ||
sourceLabel(parameters); | ||
sourceCLabel(parameters); | ||
direction(parameters); | ||
edgeLabel(parameters); | ||
} | ||
|
||
@Override | ||
public Object call(Job<Object> job, Map<String, Object> parameters) { | ||
Traverser traverser = new Traverser(job); | ||
return traverser.kcore(sourceLabel(parameters), | ||
sourceCLabel(parameters), | ||
direction(parameters), edgeLabel(parameters), | ||
k(parameters), alpha(parameters), | ||
degree(parameters), merged(parameters)); | ||
} | ||
|
||
protected static int k(Map<String, Object> parameters) { | ||
if (!parameters.containsKey(KEY_K)) { | ||
return DEFAULT_K; | ||
} | ||
int k = parameterInt(parameters, KEY_K); | ||
E.checkArgument(k > 1, "The k of kcore must be > 1, but got %s", k); | ||
return k; | ||
} | ||
|
||
protected static boolean merged(Map<String, Object> parameters) { | ||
if (!parameters.containsKey(KEY_MERGED)) { | ||
return false; | ||
} | ||
return parameterBoolean(parameters, KEY_MERGED); | ||
} | ||
|
||
public static class Traverser extends AlgoTraverser { | ||
|
||
public Traverser(Job<Object> job) { | ||
super(job); | ||
} | ||
|
||
public Object kcore(String sourceLabel, String sourceCLabel, | ||
Directions dir, String label, int k, double alpha, | ||
long degree, boolean merged) { | ||
HugeGraph graph = this.graph(); | ||
Iterator<Vertex> vertices = this.vertices(sourceLabel, sourceCLabel, | ||
Query.NO_LIMIT); | ||
EdgeLabel edgeLabel = label == null ? null : graph.edgeLabel(label); | ||
|
||
KcoreTraverser traverser = new KcoreTraverser(graph); | ||
JsonMap kcoresJson = new JsonMap(); | ||
kcoresJson.startObject(); | ||
kcoresJson.appendKey("kcores"); | ||
kcoresJson.startList(); | ||
Set<Set<Id>> kcoreSet = new HashSet<>(); | ||
while(vertices.hasNext()) { | ||
this.updateProgress(++this.progress); | ||
Vertex vertex = vertices.next(); | ||
Set<Id> kcore = traverser.kcore(IteratorUtils.of(vertex), | ||
dir, edgeLabel, k, alpha, | ||
degree); | ||
if (kcore.isEmpty()) { | ||
continue; | ||
} | ||
if (merged) { | ||
mergeKcores(kcoreSet, kcore); | ||
} else { | ||
kcoresJson.appendRaw(JsonUtil.toJson(kcore)); | ||
} | ||
} | ||
if (merged) { | ||
for (Set<Id> kcore : kcoreSet) { | ||
kcoresJson.appendRaw(JsonUtil.toJson(kcore)); | ||
} | ||
} | ||
kcoresJson.endList(); | ||
kcoresJson.endObject(); | ||
|
||
return kcoresJson.asJson(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static void mergeKcores(Set<Set<Id>> kcores, Set<Id> kcore) { | ||
boolean merged = false; | ||
/* | ||
* Iterate to collect merging kcores firstly, because merging | ||
* kcores will be removed from all kcores. | ||
* Besides one new kcore may connect to multiple existing kcores. | ||
*/ | ||
Set<Set<Id>> mergingKcores = new HashSet<>(); | ||
for (Set<Id> existedKcore : kcores) { | ||
if (CollectionUtil.hasIntersection(existedKcore, kcore)) { | ||
mergingKcores.add(existedKcore); | ||
merged = true; | ||
} | ||
} | ||
if (merged) { | ||
for (Set<Id> mergingKcore : mergingKcores) { | ||
kcores.remove(mergingKcore); | ||
kcore.addAll(mergingKcore); | ||
} | ||
} | ||
kcores.add(kcore); | ||
} | ||
} | ||
|
||
public static class KcoreTraverser extends FusiformSimilarityTraverser { | ||
|
||
public KcoreTraverser(HugeGraph graph) { | ||
super(graph); | ||
} | ||
|
||
public Set<Id> kcore(Iterator<Vertex> vertices, Directions direction, | ||
EdgeLabel label, int k, double alpha, | ||
long degree) { | ||
int minNeighbors = (int) Math.floor(1 / alpha * k); | ||
SimilarsMap map = fusiformSimilarity(vertices, direction, label, | ||
minNeighbors, alpha, k - 1, | ||
0, null, 1, degree, | ||
NO_LIMIT, NO_LIMIT, true); | ||
if (map.isEmpty()) { | ||
return ImmutableSet.of(); | ||
} | ||
return extractKcore(map, k); | ||
} | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
private static Set<Id> extractKcore(SimilarsMap similarsMap, int k) { | ||
assert similarsMap.size() == 1; | ||
Map.Entry<Id, Set<Similar>> entry = similarsMap.entrySet() | ||
.iterator().next(); | ||
Id source = entry.getKey(); | ||
Set<KcoreSimilar> similars = new HashSet<>(); | ||
for (Similar similar: entry.getValue()) { | ||
similars.add(new KcoreSimilar(similar)); | ||
} | ||
|
||
boolean stop; | ||
do { | ||
stop = true; | ||
// Do statistics | ||
Map<Id, MutableInt> counts = new HashMap<>(); | ||
for (KcoreSimilar similar : similars) { | ||
for (Id id : similar.ids()) { | ||
MutableInt count = counts.get(id); | ||
if (count == null) { | ||
count = new MutableInt(0); | ||
counts.put(id, count); | ||
} | ||
count.increment(); | ||
} | ||
} | ||
/* | ||
* Iterate similars to: | ||
* 1. delete failed similar | ||
* 2. delete failed intermediaries in survive similar | ||
* 3. update statistics | ||
*/ | ||
Set<KcoreSimilar> failedSimilars = new HashSet<>(); | ||
for (KcoreSimilar similar : similars) { | ||
Set<Id> failedIds = new HashSet<>(); | ||
for (Id id : similar.ids()) { | ||
MutableInt count = counts.get(id); | ||
if (count.getValue() < k - 1) { | ||
count.decrement(); | ||
failedIds.add(id); | ||
stop = false; | ||
} | ||
} | ||
|
||
Set<Id> survivedIds = new HashSet<>(CollectionUtils | ||
.subtract(similar.ids(), failedIds)); | ||
if (survivedIds.size() < k) { | ||
for (Id id : survivedIds) { | ||
counts.get(id).decrement(); | ||
} | ||
failedSimilars.add(similar); | ||
} else { | ||
similar.ids(survivedIds); | ||
} | ||
} | ||
similars = new HashSet<>(CollectionUtils.subtract( | ||
similars, failedSimilars)); | ||
} while (!stop); | ||
|
||
if (similars.isEmpty()) { | ||
return ImmutableSet.of(); | ||
} | ||
Set<Id> kcores = new HashSet<>(); | ||
kcores.add(source); | ||
for (KcoreSimilar similar : similars) { | ||
kcores.add(similar.id()); | ||
kcores.addAll(similar.ids()); | ||
} | ||
return kcores; | ||
} | ||
} | ||
|
||
private static class KcoreSimilar extends | ||
FusiformSimilarityTraverser.Similar { | ||
|
||
private Set<Id> ids; | ||
|
||
public KcoreSimilar(Id id, double score, List<Id> intermediaries) { | ||
super(id, score, intermediaries); | ||
this.ids = null; | ||
} | ||
|
||
public KcoreSimilar(FusiformSimilarityTraverser.Similar similar) { | ||
super(similar.id(), similar.score(), similar.intermediaries()); | ||
this.ids = new HashSet<>(this.intermediaries()); | ||
} | ||
|
||
public Set<Id> ids() { | ||
if (this.ids == null) { | ||
this.ids = new HashSet<>(this.intermediaries()); | ||
} | ||
return this.ids; | ||
} | ||
|
||
public void ids(Set<Id> ids) { | ||
this.ids = ids; | ||
} | ||
} | ||
} |
Oops, something went wrong.