-
Notifications
You must be signed in to change notification settings - Fork 0
/
bron-kerbosch.go
79 lines (64 loc) · 1.96 KB
/
bron-kerbosch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
type Node struct {
name string
neighbors map[string]Node
}
func (n Node) ToString() string {
return n.name
}
func newNode(n string) Node {
return Node{
name: n,
neighbors: make(map[string]Node, 0),
}
}
// https://goplay.tools/snippet/6UHTIwo-K7z
func findCliques(potentialClique map[string]Node, remainingNodes map[string]Node, skipNodes map[string]Node, depth int, maxClique *int) int {
if len(remainingNodes) == 0 && len(skipNodes) == 0 {
// log.Println("Find clique :", potentialClique)
// log.Println("Find clique")
// for k := range potentialClique {
// log.Println(k)
// }
if len(potentialClique) > *maxClique {
*maxClique = len(potentialClique)
}
return 1
}
// No need to look any further : we search bigger clique only
if len(remainingNodes)+len(potentialClique) < *maxClique {
return 0
}
// found_cliques = 0
foundCliques := 0
// for node in remaining_nodes:
for knode, vnode := range remainingNodes {
// new_potential_clique = potential_clique + [node]
newPotentialClique := make(map[string]Node, 0)
for k, v := range potentialClique {
newPotentialClique[k] = v
}
newPotentialClique[knode] = vnode
// new_remaining_nodes = [n for n in remaining_nodes if n in node.neighbors]
newRemainingNodes := make(map[string]Node, 0)
for k, v := range remainingNodes {
if _, b := vnode.neighbors[k]; b {
newRemainingNodes[k] = v
}
}
// new_skip_list = [n for n in skip_nodes if n in node.neighbors]
newSkipNodes := make(map[string]Node, 0)
for k, v := range skipNodes {
if _, b := vnode.neighbors[k]; b {
newSkipNodes[k] = v
}
}
// found_cliques += find_cliques(new_potential_clique, new_remaining_nodes, new_skip_list, depth + 1)
foundCliques += findCliques(newPotentialClique, newRemainingNodes, newSkipNodes, depth+1, maxClique)
// remaining_nodes.remove(node)
delete(remainingNodes, knode)
// skip_nodes.append(node)
skipNodes[knode] = vnode
}
return foundCliques
}