decomposition.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. r"""Function for computing a junction tree of a graph."""
  2. from itertools import combinations
  3. import networkx as nx
  4. from networkx.algorithms import chordal_graph_cliques, complete_to_chordal_graph, moral
  5. from networkx.utils import not_implemented_for
  6. __all__ = ["junction_tree"]
  7. @not_implemented_for("multigraph")
  8. def junction_tree(G):
  9. r"""Returns a junction tree of a given graph.
  10. A junction tree (or clique tree) is constructed from a (un)directed graph G.
  11. The tree is constructed based on a moralized and triangulated version of G.
  12. The tree's nodes consist of maximal cliques and sepsets of the revised graph.
  13. The sepset of two cliques is the intersection of the nodes of these cliques,
  14. e.g. the sepset of (A,B,C) and (A,C,E,F) is (A,C). These nodes are often called
  15. "variables" in this literature. The tree is bipartitie with each sepset
  16. connected to its two cliques.
  17. Junction Trees are not unique as the order of clique consideration determines
  18. which sepsets are included.
  19. The junction tree algorithm consists of five steps [1]_:
  20. 1. Moralize the graph
  21. 2. Triangulate the graph
  22. 3. Find maximal cliques
  23. 4. Build the tree from cliques, connecting cliques with shared
  24. nodes, set edge-weight to number of shared variables
  25. 5. Find maximum spanning tree
  26. Parameters
  27. ----------
  28. G : networkx.Graph
  29. Directed or undirected graph.
  30. Returns
  31. -------
  32. junction_tree : networkx.Graph
  33. The corresponding junction tree of `G`.
  34. Raises
  35. ------
  36. NetworkXNotImplemented
  37. Raised if `G` is an instance of `MultiGraph` or `MultiDiGraph`.
  38. References
  39. ----------
  40. .. [1] Junction tree algorithm:
  41. https://en.wikipedia.org/wiki/Junction_tree_algorithm
  42. .. [2] Finn V. Jensen and Frank Jensen. 1994. Optimal
  43. junction trees. In Proceedings of the Tenth international
  44. conference on Uncertainty in artificial intelligence (UAI’94).
  45. Morgan Kaufmann Publishers Inc., San Francisco, CA, USA, 360–366.
  46. """
  47. clique_graph = nx.Graph()
  48. if G.is_directed():
  49. G = moral.moral_graph(G)
  50. chordal_graph, _ = complete_to_chordal_graph(G)
  51. cliques = [tuple(sorted(i)) for i in chordal_graph_cliques(chordal_graph)]
  52. clique_graph.add_nodes_from(cliques, type="clique")
  53. for edge in combinations(cliques, 2):
  54. set_edge_0 = set(edge[0])
  55. set_edge_1 = set(edge[1])
  56. if not set_edge_0.isdisjoint(set_edge_1):
  57. sepset = tuple(sorted(set_edge_0.intersection(set_edge_1)))
  58. clique_graph.add_edge(edge[0], edge[1], weight=len(sepset), sepset=sepset)
  59. junction_tree = nx.maximum_spanning_tree(clique_graph)
  60. for edge in list(junction_tree.edges(data=True)):
  61. junction_tree.add_node(edge[2]["sepset"], type="sepset")
  62. junction_tree.add_edge(edge[0], edge[2]["sepset"])
  63. junction_tree.add_edge(edge[1], edge[2]["sepset"])
  64. junction_tree.remove_edge(edge[0], edge[1])
  65. return junction_tree