
    h                         S r SSKJr  SSKrSSKJrJrJr  SSK	J
r
  S/r\
" S5      \R                  " SS	9S
 5       5       rg)z2Function for computing a junction tree of a graph.    )combinationsN)chordal_graph_cliquescomplete_to_chordal_graphmoral)not_implemented_forjunction_tree
multigraphT)returns_graphc                 l   [         R                  " 5       nU R                  5       (       a  [        R                  " U 5      n [        U 5      u  p#[        U5       Vs/ s H  n[        [        U5      5      PM     nnUR                  USS9  [        US5       H{  n[        US   5      n[        US   5      nUR                  U5      (       a  M7  [        [        UR                  U5      5      5      n	UR                  US   US   [        U	5      U	S9  M}     [         R                   " U5      n
[#        U
R%                  SS95       Hg  nU
R'                  US   S	   S	S9  U
R                  US   US   S	   5        U
R                  US   US   S	   5        U
R)                  US   US   5        Mi     U
$ s  snf )
u  Returns a junction tree of a given graph.

A junction tree (or clique tree) is constructed from a (un)directed graph G.
The tree is constructed based on a moralized and triangulated version of G.
The tree's nodes consist of maximal cliques and sepsets of the revised graph.
The sepset of two cliques is the intersection of the nodes of these cliques,
e.g. the sepset of (A,B,C) and (A,C,E,F) is (A,C). These nodes are often called
"variables" in this literature. The tree is bipartite with each sepset
connected to its two cliques.

Junction Trees are not unique as the order of clique consideration determines
which sepsets are included.

The junction tree algorithm consists of five steps [1]_:

1. Moralize the graph
2. Triangulate the graph
3. Find maximal cliques
4. Build the tree from cliques, connecting cliques with shared
   nodes, set edge-weight to number of shared variables
5. Find maximum spanning tree


Parameters
----------
G : networkx.Graph
    Directed or undirected graph.

Returns
-------
junction_tree : networkx.Graph
    The corresponding junction tree of `G`.

Raises
------
NetworkXNotImplemented
    Raised if `G` is an instance of `MultiGraph` or `MultiDiGraph`.

References
----------
.. [1] Junction tree algorithm:
   https://en.wikipedia.org/wiki/Junction_tree_algorithm

.. [2] Finn V. Jensen and Frank Jensen. 1994. Optimal
   junction trees. In Proceedings of the Tenth international
   conference on Uncertainty in artificial intelligence (UAI’94).
   Morgan Kaufmann Publishers Inc., San Francisco, CA, USA, 360–366.
clique)type   r      )weightsepsetT)datar   )nxGraphis_directedr   moral_graphr   r   tuplesortedadd_nodes_fromr   set
isdisjointintersectionadd_edgelenmaximum_spanning_treelistedgesadd_noderemove_edge)Gclique_graphchordal_graph_icliquesedge
set_edge_0
set_edge_1r   r   s              X/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/tree/decomposition.pyr   r      s   h 88:L}}a 03M)>})MN)MAuVAY)MGNh7Wa(a\
a\
$$Z006*"9"9*"EFGF!!$q'473v;v!V ) ,,\:M]((d(34tAwx0x@tAwQ(9:tAwQ(9:!!$q'473	 5 % Os   F1)__doc__	itertoolsr   networkxr   networkx.algorithmsr   r   r   networkx.utilsr   __all___dispatchabler        r-   <module>r7      sM    9 "  W W .
 \"%J & #Jr6   