我試圖解決clique problem。 我使用的是Bron Kerbosch Clique algorithm,很好用java編寫了一個巧妙的實現,可以找到here。但是,由於團聚硬度,它可能會非常緩慢,我想要做的是使用我知道他們連接的一組初始頂點組合。然後調用該方法。對於我的生活,我不確定我在做什麼錯誤,結果不是派系。Clique使用遞歸方法
注意:評論代碼來自原始代碼(上面鏈接)。
public class BronKerboschCliqueFinder<V, E> {
//~ Instance fields --------------------------------------------------------
private final UndirectedGraph<V, E> graph;
private Collection<Set<V>> cliques;
// public Collection<Set<V>> getAllMaximalCliques()
public Collection<Set<V>> getAllMaximalCliqes(Set<String> initials){
{
// TODO: assert that graph is simple
cliques = new ArrayList<Set<V>>();
List<V> potential_clique = new ArrayList<V>();
List<V> candidates = new ArrayList<V>();
List<V> already_found = new ArrayList<V>();
// candidates.addAll(graph.getVertices()); instead I do this:
for(V v : graph.getVertices()){
if(initial.contains(v)){
potential_clique.add(v);
}else{
candidates.add(v);
}
}
findCliques(potential_clique, candidates, already_found);
return cliques;
}
private void findCliques(
List<V> potential_clique,
List<V> candidates,
List<V> already_found)
{
List<V> candidates_array = new ArrayList<V>(candidates);
if (!end(candidates, already_found)) {
// for each candidate_node in candidates do
for (V candidate : candidates_array) {
List<V> new_candidates = new ArrayList<V>();
List<V> new_already_found = new ArrayList<V>();
// move candidate node to potential_clique
potential_clique.add(candidate);
candidates.remove(candidate);
// create new_candidates by removing nodes in candidates not
// connected to candidate node
for (V new_candidate : candidates) {
if (graph.isNeighbor(candidate, new_candidate))
{
new_candidates.add(new_candidate);
} // of if
} // of for
// create new_already_found by removing nodes in already_found
// not connected to candidate node
for (V new_found : already_found) {
if (graph.isNeighbor(candidate, new_found)) {
new_already_found.add(new_found);
} // of if
} // of for
// if new_candidates and new_already_found are empty
if (new_candidates.isEmpty() && new_already_found.isEmpty()) {
// potential_clique is maximal_clique
cliques.add(new HashSet<V>(potential_clique));
return;
} // of if
else {
// recursive call
findCliques(
potential_clique,
new_candidates,
new_already_found);
} // of else
// move candidate_node from potential_clique to already_found;
already_found.add(candidate);
potential_clique.remove(candidate);
} // of for
} // of if
}
private boolean end(List<V> candidates, List<V> already_found)
{
// if a node in already_found is connected to all nodes in candidates
boolean end = false;
int edgecounter;
for (V found : already_found) {
edgecounter = 0;
for (V candidate : candidates) {
if (graph.isNeighbor(found, candidate)) {
edgecounter++;
} // of if
} // of for
if (edgecounter == candidates.size()) {
end = true;
}
} // of for
return end;
}
}
因此,在短期我唯一的變化是getAllMaximalCliques
方法。 我不太確定遞歸調用方法在這裏的工作原理。
我會感激,如果任何幫助或方向可以給。
你能澄清這個問題嗎?也許我讀得太快了,但是一個句子中的問題是什麼......明確表示。 –
@AlexK好的問題是解決集合問題,給定存在於每個最大集團中的初始頂點集合。 – nafas