clique.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. """Functions for finding and manipulating cliques.
  2. Finding the largest clique in a graph is NP-complete problem, so most of
  3. these algorithms have an exponential running time; for more information,
  4. see the Wikipedia article on the clique problem [1]_.
  5. .. [1] clique problem:: https://en.wikipedia.org/wiki/Clique_problem
  6. """
  7. from collections import defaultdict, deque
  8. from itertools import chain, combinations, islice
  9. import networkx as nx
  10. from networkx.utils import not_implemented_for
  11. __all__ = [
  12. "find_cliques",
  13. "find_cliques_recursive",
  14. "make_max_clique_graph",
  15. "make_clique_bipartite",
  16. "graph_clique_number",
  17. "graph_number_of_cliques",
  18. "node_clique_number",
  19. "number_of_cliques",
  20. "cliques_containing_node",
  21. "enumerate_all_cliques",
  22. "max_weight_clique",
  23. ]
  24. @not_implemented_for("directed")
  25. def enumerate_all_cliques(G):
  26. """Returns all cliques in an undirected graph.
  27. This function returns an iterator over cliques, each of which is a
  28. list of nodes. The iteration is ordered by cardinality of the
  29. cliques: first all cliques of size one, then all cliques of size
  30. two, etc.
  31. Parameters
  32. ----------
  33. G : NetworkX graph
  34. An undirected graph.
  35. Returns
  36. -------
  37. iterator
  38. An iterator over cliques, each of which is a list of nodes in
  39. `G`. The cliques are ordered according to size.
  40. Notes
  41. -----
  42. To obtain a list of all cliques, use
  43. `list(enumerate_all_cliques(G))`. However, be aware that in the
  44. worst-case, the length of this list can be exponential in the number
  45. of nodes in the graph (for example, when the graph is the complete
  46. graph). This function avoids storing all cliques in memory by only
  47. keeping current candidate node lists in memory during its search.
  48. The implementation is adapted from the algorithm by Zhang, et
  49. al. (2005) [1]_ to output all cliques discovered.
  50. This algorithm ignores self-loops and parallel edges, since cliques
  51. are not conventionally defined with such edges.
  52. References
  53. ----------
  54. .. [1] Yun Zhang, Abu-Khzam, F.N., Baldwin, N.E., Chesler, E.J.,
  55. Langston, M.A., Samatova, N.F.,
  56. "Genome-Scale Computational Approaches to Memory-Intensive
  57. Applications in Systems Biology".
  58. *Supercomputing*, 2005. Proceedings of the ACM/IEEE SC 2005
  59. Conference, pp. 12, 12--18 Nov. 2005.
  60. <https://doi.org/10.1109/SC.2005.29>.
  61. """
  62. index = {}
  63. nbrs = {}
  64. for u in G:
  65. index[u] = len(index)
  66. # Neighbors of u that appear after u in the iteration order of G.
  67. nbrs[u] = {v for v in G[u] if v not in index}
  68. queue = deque(([u], sorted(nbrs[u], key=index.__getitem__)) for u in G)
  69. # Loop invariants:
  70. # 1. len(base) is nondecreasing.
  71. # 2. (base + cnbrs) is sorted with respect to the iteration order of G.
  72. # 3. cnbrs is a set of common neighbors of nodes in base.
  73. while queue:
  74. base, cnbrs = map(list, queue.popleft())
  75. yield base
  76. for i, u in enumerate(cnbrs):
  77. # Use generators to reduce memory consumption.
  78. queue.append(
  79. (
  80. chain(base, [u]),
  81. filter(nbrs[u].__contains__, islice(cnbrs, i + 1, None)),
  82. )
  83. )
  84. @not_implemented_for("directed")
  85. def find_cliques(G, nodes=None):
  86. """Returns all maximal cliques in an undirected graph.
  87. For each node *n*, a *maximal clique for n* is a largest complete
  88. subgraph containing *n*. The largest maximal clique is sometimes
  89. called the *maximum clique*.
  90. This function returns an iterator over cliques, each of which is a
  91. list of nodes. It is an iterative implementation, so should not
  92. suffer from recursion depth issues.
  93. This function accepts a list of `nodes` and only the maximal cliques
  94. containing all of these `nodes` are returned. It can considerably speed up
  95. the running time if some specific cliques are desired.
  96. Parameters
  97. ----------
  98. G : NetworkX graph
  99. An undirected graph.
  100. nodes : list, optional (default=None)
  101. If provided, only yield *maximal cliques* containing all nodes in `nodes`.
  102. If `nodes` isn't a clique itself, a ValueError is raised.
  103. Returns
  104. -------
  105. iterator
  106. An iterator over maximal cliques, each of which is a list of
  107. nodes in `G`. If `nodes` is provided, only the maximal cliques
  108. containing all the nodes in `nodes` are returned. The order of
  109. cliques is arbitrary.
  110. Raises
  111. ------
  112. ValueError
  113. If `nodes` is not a clique.
  114. Examples
  115. --------
  116. >>> from pprint import pprint # For nice dict formatting
  117. >>> G = nx.karate_club_graph()
  118. >>> sum(1 for c in nx.find_cliques(G)) # The number of maximal cliques in G
  119. 36
  120. >>> max(nx.find_cliques(G), key=len) # The largest maximal clique in G
  121. [0, 1, 2, 3, 13]
  122. The size of the largest maximal clique is known as the *clique number* of
  123. the graph, which can be found directly with:
  124. >>> max(len(c) for c in nx.find_cliques(G))
  125. 5
  126. One can also compute the number of maximal cliques in `G` that contain a given
  127. node. The following produces a dictionary keyed by node whose
  128. values are the number of maximal cliques in `G` that contain the node:
  129. >>> pprint({n: sum(1 for c in nx.find_cliques(G) if n in c) for n in G})
  130. {0: 13,
  131. 1: 6,
  132. 2: 7,
  133. 3: 3,
  134. 4: 2,
  135. 5: 3,
  136. 6: 3,
  137. 7: 1,
  138. 8: 3,
  139. 9: 2,
  140. 10: 2,
  141. 11: 1,
  142. 12: 1,
  143. 13: 2,
  144. 14: 1,
  145. 15: 1,
  146. 16: 1,
  147. 17: 1,
  148. 18: 1,
  149. 19: 2,
  150. 20: 1,
  151. 21: 1,
  152. 22: 1,
  153. 23: 3,
  154. 24: 2,
  155. 25: 2,
  156. 26: 1,
  157. 27: 3,
  158. 28: 2,
  159. 29: 2,
  160. 30: 2,
  161. 31: 4,
  162. 32: 9,
  163. 33: 14}
  164. Or, similarly, the maximal cliques in `G` that contain a given node.
  165. For example, the 4 maximal cliques that contain node 31:
  166. >>> [c for c in nx.find_cliques(G) if 31 in c]
  167. [[0, 31], [33, 32, 31], [33, 28, 31], [24, 25, 31]]
  168. See Also
  169. --------
  170. find_cliques_recursive
  171. A recursive version of the same algorithm.
  172. Notes
  173. -----
  174. To obtain a list of all maximal cliques, use
  175. `list(find_cliques(G))`. However, be aware that in the worst-case,
  176. the length of this list can be exponential in the number of nodes in
  177. the graph. This function avoids storing all cliques in memory by
  178. only keeping current candidate node lists in memory during its search.
  179. This implementation is based on the algorithm published by Bron and
  180. Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi
  181. (2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. It
  182. essentially unrolls the recursion used in the references to avoid
  183. issues of recursion stack depth (for a recursive implementation, see
  184. :func:`find_cliques_recursive`).
  185. This algorithm ignores self-loops and parallel edges, since cliques
  186. are not conventionally defined with such edges.
  187. References
  188. ----------
  189. .. [1] Bron, C. and Kerbosch, J.
  190. "Algorithm 457: finding all cliques of an undirected graph".
  191. *Communications of the ACM* 16, 9 (Sep. 1973), 575--577.
  192. <http://portal.acm.org/citation.cfm?doid=362342.362367>
  193. .. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi,
  194. "The worst-case time complexity for generating all maximal
  195. cliques and computational experiments",
  196. *Theoretical Computer Science*, Volume 363, Issue 1,
  197. Computing and Combinatorics,
  198. 10th Annual International Conference on
  199. Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42
  200. <https://doi.org/10.1016/j.tcs.2006.06.015>
  201. .. [3] F. Cazals, C. Karande,
  202. "A note on the problem of reporting maximal cliques",
  203. *Theoretical Computer Science*,
  204. Volume 407, Issues 1--3, 6 November 2008, Pages 564--568,
  205. <https://doi.org/10.1016/j.tcs.2008.05.010>
  206. """
  207. if len(G) == 0:
  208. return
  209. adj = {u: {v for v in G[u] if v != u} for u in G}
  210. # Initialize Q with the given nodes and subg, cand with their nbrs
  211. Q = nodes[:] if nodes is not None else []
  212. cand = set(G)
  213. for node in Q:
  214. if node not in cand:
  215. raise ValueError(f"The given `nodes` {nodes} do not form a clique")
  216. cand &= adj[node]
  217. if not cand:
  218. yield Q[:]
  219. return
  220. subg = cand.copy()
  221. stack = []
  222. Q.append(None)
  223. u = max(subg, key=lambda u: len(cand & adj[u]))
  224. ext_u = cand - adj[u]
  225. try:
  226. while True:
  227. if ext_u:
  228. q = ext_u.pop()
  229. cand.remove(q)
  230. Q[-1] = q
  231. adj_q = adj[q]
  232. subg_q = subg & adj_q
  233. if not subg_q:
  234. yield Q[:]
  235. else:
  236. cand_q = cand & adj_q
  237. if cand_q:
  238. stack.append((subg, cand, ext_u))
  239. Q.append(None)
  240. subg = subg_q
  241. cand = cand_q
  242. u = max(subg, key=lambda u: len(cand & adj[u]))
  243. ext_u = cand - adj[u]
  244. else:
  245. Q.pop()
  246. subg, cand, ext_u = stack.pop()
  247. except IndexError:
  248. pass
  249. # TODO Should this also be not implemented for directed graphs?
  250. def find_cliques_recursive(G, nodes=None):
  251. """Returns all maximal cliques in a graph.
  252. For each node *v*, a *maximal clique for v* is a largest complete
  253. subgraph containing *v*. The largest maximal clique is sometimes
  254. called the *maximum clique*.
  255. This function returns an iterator over cliques, each of which is a
  256. list of nodes. It is a recursive implementation, so may suffer from
  257. recursion depth issues, but is included for pedagogical reasons.
  258. For a non-recursive implementation, see :func:`find_cliques`.
  259. This function accepts a list of `nodes` and only the maximal cliques
  260. containing all of these `nodes` are returned. It can considerably speed up
  261. the running time if some specific cliques are desired.
  262. Parameters
  263. ----------
  264. G : NetworkX graph
  265. nodes : list, optional (default=None)
  266. If provided, only yield *maximal cliques* containing all nodes in `nodes`.
  267. If `nodes` isn't a clique itself, a ValueError is raised.
  268. Returns
  269. -------
  270. iterator
  271. An iterator over maximal cliques, each of which is a list of
  272. nodes in `G`. If `nodes` is provided, only the maximal cliques
  273. containing all the nodes in `nodes` are yielded. The order of
  274. cliques is arbitrary.
  275. Raises
  276. ------
  277. ValueError
  278. If `nodes` is not a clique.
  279. See Also
  280. --------
  281. find_cliques
  282. An iterative version of the same algorithm. See docstring for examples.
  283. Notes
  284. -----
  285. To obtain a list of all maximal cliques, use
  286. `list(find_cliques_recursive(G))`. However, be aware that in the
  287. worst-case, the length of this list can be exponential in the number
  288. of nodes in the graph. This function avoids storing all cliques in memory
  289. by only keeping current candidate node lists in memory during its search.
  290. This implementation is based on the algorithm published by Bron and
  291. Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi
  292. (2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. For a
  293. non-recursive implementation, see :func:`find_cliques`.
  294. This algorithm ignores self-loops and parallel edges, since cliques
  295. are not conventionally defined with such edges.
  296. References
  297. ----------
  298. .. [1] Bron, C. and Kerbosch, J.
  299. "Algorithm 457: finding all cliques of an undirected graph".
  300. *Communications of the ACM* 16, 9 (Sep. 1973), 575--577.
  301. <http://portal.acm.org/citation.cfm?doid=362342.362367>
  302. .. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi,
  303. "The worst-case time complexity for generating all maximal
  304. cliques and computational experiments",
  305. *Theoretical Computer Science*, Volume 363, Issue 1,
  306. Computing and Combinatorics,
  307. 10th Annual International Conference on
  308. Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42
  309. <https://doi.org/10.1016/j.tcs.2006.06.015>
  310. .. [3] F. Cazals, C. Karande,
  311. "A note on the problem of reporting maximal cliques",
  312. *Theoretical Computer Science*,
  313. Volume 407, Issues 1--3, 6 November 2008, Pages 564--568,
  314. <https://doi.org/10.1016/j.tcs.2008.05.010>
  315. """
  316. if len(G) == 0:
  317. return iter([])
  318. adj = {u: {v for v in G[u] if v != u} for u in G}
  319. # Initialize Q with the given nodes and subg, cand with their nbrs
  320. Q = nodes[:] if nodes is not None else []
  321. cand_init = set(G)
  322. for node in Q:
  323. if node not in cand_init:
  324. raise ValueError(f"The given `nodes` {nodes} do not form a clique")
  325. cand_init &= adj[node]
  326. if not cand_init:
  327. return iter([Q])
  328. subg_init = cand_init.copy()
  329. def expand(subg, cand):
  330. u = max(subg, key=lambda u: len(cand & adj[u]))
  331. for q in cand - adj[u]:
  332. cand.remove(q)
  333. Q.append(q)
  334. adj_q = adj[q]
  335. subg_q = subg & adj_q
  336. if not subg_q:
  337. yield Q[:]
  338. else:
  339. cand_q = cand & adj_q
  340. if cand_q:
  341. yield from expand(subg_q, cand_q)
  342. Q.pop()
  343. return expand(subg_init, cand_init)
  344. def make_max_clique_graph(G, create_using=None):
  345. """Returns the maximal clique graph of the given graph.
  346. The nodes of the maximal clique graph of `G` are the cliques of
  347. `G` and an edge joins two cliques if the cliques are not disjoint.
  348. Parameters
  349. ----------
  350. G : NetworkX graph
  351. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  352. Graph type to create. If graph instance, then cleared before populated.
  353. Returns
  354. -------
  355. NetworkX graph
  356. A graph whose nodes are the cliques of `G` and whose edges
  357. join two cliques if they are not disjoint.
  358. Notes
  359. -----
  360. This function behaves like the following code::
  361. import networkx as nx
  362. G = nx.make_clique_bipartite(G)
  363. cliques = [v for v in G.nodes() if G.nodes[v]['bipartite'] == 0]
  364. G = nx.bipartite.projected_graph(G, cliques)
  365. G = nx.relabel_nodes(G, {-v: v - 1 for v in G})
  366. It should be faster, though, since it skips all the intermediate
  367. steps.
  368. """
  369. if create_using is None:
  370. B = G.__class__()
  371. else:
  372. B = nx.empty_graph(0, create_using)
  373. cliques = list(enumerate(set(c) for c in find_cliques(G)))
  374. # Add a numbered node for each clique.
  375. B.add_nodes_from(i for i, c in cliques)
  376. # Join cliques by an edge if they share a node.
  377. clique_pairs = combinations(cliques, 2)
  378. B.add_edges_from((i, j) for (i, c1), (j, c2) in clique_pairs if c1 & c2)
  379. return B
  380. def make_clique_bipartite(G, fpos=None, create_using=None, name=None):
  381. """Returns the bipartite clique graph corresponding to `G`.
  382. In the returned bipartite graph, the "bottom" nodes are the nodes of
  383. `G` and the "top" nodes represent the maximal cliques of `G`.
  384. There is an edge from node *v* to clique *C* in the returned graph
  385. if and only if *v* is an element of *C*.
  386. Parameters
  387. ----------
  388. G : NetworkX graph
  389. An undirected graph.
  390. fpos : bool
  391. If True or not None, the returned graph will have an
  392. additional attribute, `pos`, a dictionary mapping node to
  393. position in the Euclidean plane.
  394. create_using : NetworkX graph constructor, optional (default=nx.Graph)
  395. Graph type to create. If graph instance, then cleared before populated.
  396. Returns
  397. -------
  398. NetworkX graph
  399. A bipartite graph whose "bottom" set is the nodes of the graph
  400. `G`, whose "top" set is the cliques of `G`, and whose edges
  401. join nodes of `G` to the cliques that contain them.
  402. The nodes of the graph `G` have the node attribute
  403. 'bipartite' set to 1 and the nodes representing cliques
  404. have the node attribute 'bipartite' set to 0, as is the
  405. convention for bipartite graphs in NetworkX.
  406. """
  407. B = nx.empty_graph(0, create_using)
  408. B.clear()
  409. # The "bottom" nodes in the bipartite graph are the nodes of the
  410. # original graph, G.
  411. B.add_nodes_from(G, bipartite=1)
  412. for i, cl in enumerate(find_cliques(G)):
  413. # The "top" nodes in the bipartite graph are the cliques. These
  414. # nodes get negative numbers as labels.
  415. name = -i - 1
  416. B.add_node(name, bipartite=0)
  417. B.add_edges_from((v, name) for v in cl)
  418. return B
  419. def graph_clique_number(G, cliques=None):
  420. """Returns the clique number of the graph.
  421. The *clique number* of a graph is the size of the largest clique in
  422. the graph.
  423. .. deprecated:: 3.0
  424. graph_clique_number is deprecated in NetworkX 3.0 and will be removed
  425. in v3.2. The graph clique number can be computed directly with::
  426. max(len(c) for c in nx.find_cliques(G))
  427. Parameters
  428. ----------
  429. G : NetworkX graph
  430. An undirected graph.
  431. cliques : list
  432. A list of cliques, each of which is itself a list of nodes. If
  433. not specified, the list of all cliques will be computed, as by
  434. :func:`find_cliques`.
  435. Returns
  436. -------
  437. int
  438. The size of the largest clique in `G`.
  439. Notes
  440. -----
  441. You should provide `cliques` if you have already computed the list
  442. of maximal cliques, in order to avoid an exponential time search for
  443. maximal cliques.
  444. """
  445. import warnings
  446. warnings.warn(
  447. (
  448. "\n\ngraph_clique_number is deprecated and will be removed.\n"
  449. "Use: ``max(len(c) for c in nx.find_cliques(G))`` instead."
  450. ),
  451. DeprecationWarning,
  452. stacklevel=2,
  453. )
  454. if len(G.nodes) < 1:
  455. return 0
  456. if cliques is None:
  457. cliques = find_cliques(G)
  458. return max([len(c) for c in cliques] or [1])
  459. def graph_number_of_cliques(G, cliques=None):
  460. """Returns the number of maximal cliques in the graph.
  461. .. deprecated:: 3.0
  462. graph_number_of_cliques is deprecated and will be removed in v3.2.
  463. The number of maximal cliques can be computed directly with::
  464. sum(1 for _ in nx.find_cliques(G))
  465. Parameters
  466. ----------
  467. G : NetworkX graph
  468. An undirected graph.
  469. cliques : list
  470. A list of cliques, each of which is itself a list of nodes. If
  471. not specified, the list of all cliques will be computed, as by
  472. :func:`find_cliques`.
  473. Returns
  474. -------
  475. int
  476. The number of maximal cliques in `G`.
  477. Notes
  478. -----
  479. You should provide `cliques` if you have already computed the list
  480. of maximal cliques, in order to avoid an exponential time search for
  481. maximal cliques.
  482. """
  483. import warnings
  484. warnings.warn(
  485. (
  486. "\n\ngraph_number_of_cliques is deprecated and will be removed.\n"
  487. "Use: ``sum(1 for _ in nx.find_cliques(G))`` instead."
  488. ),
  489. DeprecationWarning,
  490. stacklevel=2,
  491. )
  492. if cliques is None:
  493. cliques = list(find_cliques(G))
  494. return len(cliques)
  495. def node_clique_number(G, nodes=None, cliques=None, separate_nodes=False):
  496. """Returns the size of the largest maximal clique containing each given node.
  497. Returns a single or list depending on input nodes.
  498. An optional list of cliques can be input if already computed.
  499. Parameters
  500. ----------
  501. G : NetworkX graph
  502. An undirected graph.
  503. cliques : list, optional (default=None)
  504. A list of cliques, each of which is itself a list of nodes.
  505. If not specified, the list of all cliques will be computed
  506. using :func:`find_cliques`.
  507. Returns
  508. -------
  509. int or dict
  510. If `nodes` is a single node, returns the size of the
  511. largest maximal clique in `G` containing that node.
  512. Otherwise return a dict keyed by node to the size
  513. of the largest maximal clique containing that node.
  514. See Also
  515. --------
  516. find_cliques
  517. find_cliques yields the maximal cliques of G.
  518. It accepts a `nodes` argument which restricts consideration to
  519. maximal cliques containing all the given `nodes`.
  520. The search for the cliques is optimized for `nodes`.
  521. """
  522. if cliques is None:
  523. if nodes is not None:
  524. # Use ego_graph to decrease size of graph
  525. # check for single node
  526. if nodes in G:
  527. return max(len(c) for c in find_cliques(nx.ego_graph(G, nodes)))
  528. # handle multiple nodes
  529. return {
  530. n: max(len(c) for c in find_cliques(nx.ego_graph(G, n))) for n in nodes
  531. }
  532. # nodes is None--find all cliques
  533. cliques = list(find_cliques(G))
  534. # single node requested
  535. if nodes in G:
  536. return max(len(c) for c in cliques if nodes in c)
  537. # multiple nodes requested
  538. # preprocess all nodes (faster than one at a time for even 2 nodes)
  539. size_for_n = defaultdict(int)
  540. for c in cliques:
  541. size_of_c = len(c)
  542. for n in c:
  543. if size_for_n[n] < size_of_c:
  544. size_for_n[n] = size_of_c
  545. if nodes is None:
  546. return size_for_n
  547. return {n: size_for_n[n] for n in nodes}
  548. def number_of_cliques(G, nodes=None, cliques=None):
  549. """Returns the number of maximal cliques for each node.
  550. .. deprecated:: 3.0
  551. number_of_cliques is deprecated and will be removed in v3.2.
  552. Use the result of `find_cliques` directly to compute the number of
  553. cliques containing each node::
  554. {n: sum(1 for c in nx.find_cliques(G) if n in c) for n in G}
  555. Returns a single or list depending on input nodes.
  556. Optional list of cliques can be input if already computed.
  557. """
  558. import warnings
  559. warnings.warn(
  560. (
  561. "\n\nnumber_of_cliques is deprecated and will be removed.\n"
  562. "Use the result of find_cliques directly to compute the number\n"
  563. "of cliques containing each node:\n\n"
  564. " {n: sum(1 for c in nx.find_cliques(G) if n in c) for n in G}\n\n"
  565. ),
  566. DeprecationWarning,
  567. stacklevel=2,
  568. )
  569. if cliques is None:
  570. cliques = list(find_cliques(G))
  571. if nodes is None:
  572. nodes = list(G.nodes()) # none, get entire graph
  573. if not isinstance(nodes, list): # check for a list
  574. v = nodes
  575. # assume it is a single value
  576. numcliq = len([1 for c in cliques if v in c])
  577. else:
  578. numcliq = {}
  579. for v in nodes:
  580. numcliq[v] = len([1 for c in cliques if v in c])
  581. return numcliq
  582. def cliques_containing_node(G, nodes=None, cliques=None):
  583. """Returns a list of cliques containing the given node.
  584. .. deprecated:: 3.0
  585. cliques_containing_node is deprecated and will be removed in 3.2.
  586. Use the result of `find_cliques` directly to compute the cliques that
  587. contain each node::
  588. {n: [c for c in nx.find_cliques(G) if n in c] for n in G}
  589. Returns a single list or list of lists depending on input nodes.
  590. Optional list of cliques can be input if already computed.
  591. """
  592. import warnings
  593. warnings.warn(
  594. (
  595. "\n\ncliques_containing_node is deprecated and will be removed.\n"
  596. "Use the result of find_cliques directly to compute maximal cliques\n"
  597. "containing each node:\n\n"
  598. " {n: [c for c in nx.find_cliques(G) if n in c] for n in G}\n\n"
  599. ),
  600. DeprecationWarning,
  601. stacklevel=2,
  602. )
  603. if cliques is None:
  604. cliques = list(find_cliques(G))
  605. if nodes is None:
  606. nodes = list(G.nodes()) # none, get entire graph
  607. if not isinstance(nodes, list): # check for a list
  608. v = nodes
  609. # assume it is a single value
  610. vcliques = [c for c in cliques if v in c]
  611. else:
  612. vcliques = {}
  613. for v in nodes:
  614. vcliques[v] = [c for c in cliques if v in c]
  615. return vcliques
  616. class MaxWeightClique:
  617. """A class for the maximum weight clique algorithm.
  618. This class is a helper for the `max_weight_clique` function. The class
  619. should not normally be used directly.
  620. Parameters
  621. ----------
  622. G : NetworkX graph
  623. The undirected graph for which a maximum weight clique is sought
  624. weight : string or None, optional (default='weight')
  625. The node attribute that holds the integer value used as a weight.
  626. If None, then each node has weight 1.
  627. Attributes
  628. ----------
  629. G : NetworkX graph
  630. The undirected graph for which a maximum weight clique is sought
  631. node_weights: dict
  632. The weight of each node
  633. incumbent_nodes : list
  634. The nodes of the incumbent clique (the best clique found so far)
  635. incumbent_weight: int
  636. The weight of the incumbent clique
  637. """
  638. def __init__(self, G, weight):
  639. self.G = G
  640. self.incumbent_nodes = []
  641. self.incumbent_weight = 0
  642. if weight is None:
  643. self.node_weights = {v: 1 for v in G.nodes()}
  644. else:
  645. for v in G.nodes():
  646. if weight not in G.nodes[v]:
  647. errmsg = f"Node {v!r} does not have the requested weight field."
  648. raise KeyError(errmsg)
  649. if not isinstance(G.nodes[v][weight], int):
  650. errmsg = f"The {weight!r} field of node {v!r} is not an integer."
  651. raise ValueError(errmsg)
  652. self.node_weights = {v: G.nodes[v][weight] for v in G.nodes()}
  653. def update_incumbent_if_improved(self, C, C_weight):
  654. """Update the incumbent if the node set C has greater weight.
  655. C is assumed to be a clique.
  656. """
  657. if C_weight > self.incumbent_weight:
  658. self.incumbent_nodes = C[:]
  659. self.incumbent_weight = C_weight
  660. def greedily_find_independent_set(self, P):
  661. """Greedily find an independent set of nodes from a set of
  662. nodes P."""
  663. independent_set = []
  664. P = P[:]
  665. while P:
  666. v = P[0]
  667. independent_set.append(v)
  668. P = [w for w in P if v != w and not self.G.has_edge(v, w)]
  669. return independent_set
  670. def find_branching_nodes(self, P, target):
  671. """Find a set of nodes to branch on."""
  672. residual_wt = {v: self.node_weights[v] for v in P}
  673. total_wt = 0
  674. P = P[:]
  675. while P:
  676. independent_set = self.greedily_find_independent_set(P)
  677. min_wt_in_class = min(residual_wt[v] for v in independent_set)
  678. total_wt += min_wt_in_class
  679. if total_wt > target:
  680. break
  681. for v in independent_set:
  682. residual_wt[v] -= min_wt_in_class
  683. P = [v for v in P if residual_wt[v] != 0]
  684. return P
  685. def expand(self, C, C_weight, P):
  686. """Look for the best clique that contains all the nodes in C and zero or
  687. more of the nodes in P, backtracking if it can be shown that no such
  688. clique has greater weight than the incumbent.
  689. """
  690. self.update_incumbent_if_improved(C, C_weight)
  691. branching_nodes = self.find_branching_nodes(P, self.incumbent_weight - C_weight)
  692. while branching_nodes:
  693. v = branching_nodes.pop()
  694. P.remove(v)
  695. new_C = C + [v]
  696. new_C_weight = C_weight + self.node_weights[v]
  697. new_P = [w for w in P if self.G.has_edge(v, w)]
  698. self.expand(new_C, new_C_weight, new_P)
  699. def find_max_weight_clique(self):
  700. """Find a maximum weight clique."""
  701. # Sort nodes in reverse order of degree for speed
  702. nodes = sorted(self.G.nodes(), key=lambda v: self.G.degree(v), reverse=True)
  703. nodes = [v for v in nodes if self.node_weights[v] > 0]
  704. self.expand([], 0, nodes)
  705. @not_implemented_for("directed")
  706. def max_weight_clique(G, weight="weight"):
  707. """Find a maximum weight clique in G.
  708. A *clique* in a graph is a set of nodes such that every two distinct nodes
  709. are adjacent. The *weight* of a clique is the sum of the weights of its
  710. nodes. A *maximum weight clique* of graph G is a clique C in G such that
  711. no clique in G has weight greater than the weight of C.
  712. Parameters
  713. ----------
  714. G : NetworkX graph
  715. Undirected graph
  716. weight : string or None, optional (default='weight')
  717. The node attribute that holds the integer value used as a weight.
  718. If None, then each node has weight 1.
  719. Returns
  720. -------
  721. clique : list
  722. the nodes of a maximum weight clique
  723. weight : int
  724. the weight of a maximum weight clique
  725. Notes
  726. -----
  727. The implementation is recursive, and therefore it may run into recursion
  728. depth issues if G contains a clique whose number of nodes is close to the
  729. recursion depth limit.
  730. At each search node, the algorithm greedily constructs a weighted
  731. independent set cover of part of the graph in order to find a small set of
  732. nodes on which to branch. The algorithm is very similar to the algorithm
  733. of Tavares et al. [1]_, other than the fact that the NetworkX version does
  734. not use bitsets. This style of algorithm for maximum weight clique (and
  735. maximum weight independent set, which is the same problem but on the
  736. complement graph) has a decades-long history. See Algorithm B of Warren
  737. and Hicks [2]_ and the references in that paper.
  738. References
  739. ----------
  740. .. [1] Tavares, W.A., Neto, M.B.C., Rodrigues, C.D., Michelon, P.: Um
  741. algoritmo de branch and bound para o problema da clique máxima
  742. ponderada. Proceedings of XLVII SBPO 1 (2015).
  743. .. [2] Warrent, Jeffrey S, Hicks, Illya V.: Combinatorial Branch-and-Bound
  744. for the Maximum Weight Independent Set Problem. Technical Report,
  745. Texas A&M University (2016).
  746. """
  747. mwc = MaxWeightClique(G, weight)
  748. mwc.find_max_weight_clique()
  749. return mwc.incumbent_nodes, mwc.incumbent_weight