This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclique.py
214 lines (194 loc) · 8.01 KB
/
clique.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import graph_tool.all as gt
def find_cliques(G):
"""Returns all maximal cliques in an undirected graph.
For each node *v*, a *maximal clique for v* is a largest complete
subgraph containing *v*. The largest maximal clique is sometimes
called the *maximum clique*.
This function returns an iterator over cliques, each of which is a
list of nodes. It is an iterative implementation, so should not
suffer from recursion depth issues.
Parameters
----------
G : graph-tool graph
An undirected graph.
Returns
-------
iterator
An iterator over maximal cliques, each of which is a list of
nodes in `G`. The order of cliques is arbitrary.
See Also
--------
find_cliques_recursive
A recursive version of the same algorithm.
Notes
-----
Taken from NetworkX.
https://github.com/networkx/networkx/blob/master/networkx/algorithms/clique.py
To obtain a list of all maximal cliques, use
`list(find_cliques(G))`. However, be aware that in the worst-case,
the length of this list can be exponential in the number of nodes in
the graph (for example, when the graph is the complete graph). This
function avoids storing all cliques in memory by only keeping
current candidate node lists in memory during its search.
This implementation is based on the algorithm published by Bron and
Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi
(2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. It
essentially unrolls the recursion used in the references to avoid
issues of recursion stack depth (for a recursive implementation, see
:func:`find_cliques_recursive`).
This algorithm ignores self-loops and parallel edges, since cliques
are not conventionally defined with such edges.
References
----------
.. [1] Bron, C. and Kerbosch, J.
"Algorithm 457: finding all cliques of an undirected graph".
*Communications of the ACM* 16, 9 (Sep. 1973), 575--577.
<http://portal.acm.org/citation.cfm?doid=362342.362367>
.. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi,
"The worst-case time complexity for generating all maximal
cliques and computational experiments",
*Theoretical Computer Science*, Volume 363, Issue 1,
Computing and Combinatorics,
10th Annual International Conference on
Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42
<https://doi.org/10.1016/j.tcs.2006.06.015>
.. [3] F. Cazals, C. Karande,
"A note on the problem of reporting maximal cliques",
*Theoretical Computer Science*,
Volume 407, Issues 1--3, 6 November 2008, Pages 564--568,
<https://doi.org/10.1016/j.tcs.2008.05.010>
"""
if len(G.get_vertices()) == 0:
return
adj = {u: {v for v in G.get_out_neighbors(u)} for u in G.vertices()}
Q = [None]
subg = set(G.get_vertices())
cand = set(G.get_vertices())
u = max(subg, key=lambda u: len(cand & adj[u]))
ext_u = cand - adj[u]
stack = []
try:
while True:
if ext_u:
q = ext_u.pop()
cand.remove(q)
Q[-1] = q
adj_q = adj[q]
subg_q = subg & adj_q
if not subg_q:
yield Q[:]
else:
cand_q = cand & adj_q
if cand_q:
stack.append((subg, cand, ext_u))
Q.append(None)
subg = subg_q
cand = cand_q
u = max(subg, key=lambda u: len(cand & adj[u]))
ext_u = cand - adj[u]
else:
Q.pop()
subg, cand, ext_u = stack.pop()
except IndexError:
pass
def find_cliques_recursive(G):
"""Returns all maximal cliques in a graph.
For each node *v*, a *maximal clique for v* is a largest complete
subgraph containing *v*. The largest maximal clique is sometimes
called the *maximum clique*.
This function returns an iterator over cliques, each of which is a
list of nodes. It is a recursive implementation, so may suffer from
recursion depth issues.
Parameters
----------
G : graph-tool graph
Returns
-------
iterator
An iterator over maximal cliques, each of which is a list of
nodes in `G`. The order of cliques is arbitrary.
See Also
--------
find_cliques
An iterative version of the same algorithm.
Notes
-----
Taken from NetworkX.
https://github.com/networkx/networkx/blob/master/networkx/algorithms/clique.py
To obtain a list of all maximal cliques, use
`list(find_cliques_recursive(G))`. However, be aware that in the
worst-case, the length of this list can be exponential in the number
of nodes in the graph (for example, when the graph is the complete
graph). This function avoids storing all cliques in memory by only
keeping current candidate node lists in memory during its search.
This implementation is based on the algorithm published by Bron and
Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi
(2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. For a
non-recursive implementation, see :func:`find_cliques`.
This algorithm ignores self-loops and parallel edges, since cliques
are not conventionally defined with such edges.
References
----------
.. [1] Bron, C. and Kerbosch, J.
"Algorithm 457: finding all cliques of an undirected graph".
*Communications of the ACM* 16, 9 (Sep. 1973), 575--577.
<http://portal.acm.org/citation.cfm?doid=362342.362367>
.. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi,
"The worst-case time complexity for generating all maximal
cliques and computational experiments",
*Theoretical Computer Science*, Volume 363, Issue 1,
Computing and Combinatorics,
10th Annual International Conference on
Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42
<https://doi.org/10.1016/j.tcs.2006.06.015>
.. [3] F. Cazals, C. Karande,
"A note on the problem of reporting maximal cliques",
*Theoretical Computer Science*,
Volume 407, Issues 1--3, 6 November 2008, Pages 564--568,
<https://doi.org/10.1016/j.tcs.2008.05.010>
"""
if len(G.get_vertices()) == 0:
return iter([])
adj = {u: {v for v in G.get_out_neighbors(u)} for u in G.vertices()}
Q = []
def expand(subg, cand):
u = max(subg, key=lambda u: len(cand & adj[u]))
for q in cand - adj[u]:
cand.remove(q)
Q.append(q)
adj_q = adj[q]
subg_q = subg & adj_q
if not subg_q:
yield Q[:]
else:
cand_q = cand & adj_q
if cand_q:
for clique in expand(subg_q, cand_q):
yield clique
Q.pop()
return expand(set(G.get_vertices()), set(G.get_vertices()))
def cliques_containing_node(G, nodes=None, cliques=None):
"""Returns a list of cliques containing the given node.
Returns a single list or list of lists depending on input nodes.
Optional list of cliques can be input if already computed.
Taken from NetworkX.
https://github.com/networkx/networkx/blob/master/networkx/algorithms/clique.py
"""
if cliques is None:
cliques = list(find_cliques(G))
if nodes is None:
nodes = list(G.get_vertices()) # none, get entire graph
if not isinstance(nodes, list): # check for a list
v = nodes
# assume it is a single value
vcliques = [c for c in cliques if v in c]
else:
vcliques = {}
for v in nodes:
vcliques[v] = [c for c in cliques if v in c]
return vcliques
if __name__ == "__main__":
g = gt.collection.data["karate"]
cliques = list(find_cliques(g))
print(cliques)
print(cliques_containing_node(g, 1))