clique.py 27 KB

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