a
HlX`:@sdZddlZddlZddlZddlmZddlmZm Z gdZ
GdddejZdd Z
ejfd
dZdd
ZddZddZddZddZdejfddZddZe dddZdS)z
Algorithms for chordal graphs.
A graph is chordal if every cycle of length at least 4 has a chord
(an edge joining two nodes not adjacent in the cycle).
https://en.wikipedia.org/wiki/Chordal_graph
N)connected_components)arbitrary_elementnot_implemented_for)
is_chordalfind_induced_nodeschordal_graph_cliqueschordal_graph_treewidthNetworkXTreewidthBoundExceededcomplete_to_chordal_graphc@seZdZdZdS)r zVException raised when a treewidth bound has been provided and it has
been exceededN)__name__
__module____qualname____doc__rr/Users/cmdms/OneDriveUOB/Development/Projects/2021/samconsensusv3/env/lib/python3.9/sitepackages/networkx/algorithms/chordal.pyr sr cCs@rtdr$tdttdkr8dSdSdS)ucChecks whether G is a chordal graph.
A graph is chordal if every cycle of length at least 4 has a chord
(an edge joining two nodes not adjacent in the cycle).
Parameters

G : graph
A NetworkX graph.
Returns

chordal : bool
True if G is a chordal graph and False otherwise.
Raises

NetworkXError
The algorithm does not support DiGraph, MultiGraph and MultiDiGraph.
If the input graph is an instance of one of these classes, a
:exc:`NetworkXError` is raised.
Examples

>>> e = [
... (1, 2),
... (1, 3),
... (2, 3),
... (2, 4),
... (3, 4),
... (3, 5),
... (3, 6),
... (4, 5),
... (4, 6),
... (5, 6),
... ]
>>> G = nx.Graph(e)
>>> nx.is_chordal(G)
True
Notes

The routine tries to go through every node following maximum cardinality
search. It returns False when it finds that the separator for any node
is not a clique. Based on the algorithms in [1]_.
References

.. [1] R. E. Tarjan and M. Yannakakis, Simple lineartime algorithms
to test chordality of graphs, test acyclicity of hypergraphs, and
selectively reduce acyclic hypergraphs, SIAM J. Comput., 13 (1984),
pp. 566–579.
zDirected graphs not supportedz(Multiply connected graphs not supported.rTFN)Zis_directednx
NetworkXErrorZ
is_multigraphlen_find_chordality_breakerGrrrrs6
rcCststdt}t}t}r~\}}} D]}

krV
qVt}q:rD]*}t t@dkrqqS)aReturns the set of induced nodes in the path from s to t.
Parameters

G : graph
A chordal NetworkX graph
s : node
Source node to look for induced nodes
t : node
Destination node to look for induced nodes
treewith_bound: float
Maximum treewidth acceptable for the graph H. The search
for induced nodes will end as soon as the treewidth_bound is exceeded.
Returns

Induced_nodes : Set of nodes
The set of induced nodes in the path from s to t in G
Raises

NetworkXError
The algorithm does not support DiGraph, MultiGraph and MultiDiGraph.
If the input graph is an instance of one of these classes, a
:exc:`NetworkXError` is raised.
The algorithm can only be applied to chordal graphs. If the input
graph is found to be nonchordal, a :exc:`NetworkXError` is raised.
Examples

>>> G = nx.Graph()
>>> G = nx.generators.classic.path_graph(10)
>>> induced_nodes = nx.find_induced_nodes(G, 1, 9, 2)
>>> sorted(induced_nodes)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Notes

G must be a chordal graph and (s,t) an edge that is not in G.
If a treewidth_bound is provided, the search for induced nodes will end
as soon as the treewidth_bound is exceeded.
The algorithm is inspired by Algorithm 4 in [1]_.
A formal definition of induced node can also be found on that reference.
References

.. [1] Learning Bounded Treewidth Bayesian Networks.
Gal Elidan, Stephen Gould; JMLR, 9(Dec):26992731, 2008.
http://jmlr.csail.mit.edu/papers/volume9/elidan08a/elidan08a.pdf
Input graph is not chordal.)
rrrZGraphZadd_edgesetrupdateaddr)rsttreewidth_boundHZ
Induced_nodesZtripletuvwnrrrr_s(5
rcCs"d}ttddtDS)aEReturns the set of maximal cliques of a chordal graph.
The algorithm breaks the graph in connected components and performs a
maximum cardinality search in each component to get the cliques.
Parameters

G : graph
A NetworkX graph
Returns

cliques : A set containing the maximal cliques in G.
Raises

NetworkXError
The algorithm does not support DiGraph, MultiGraph and MultiDiGraph.
If the input graph is an instance of one of these classes, a
:exc:`NetworkXError` is raised.
The algorithm can only be applied to chordal graphs. If the input
graph is found to be nonchordal, a :exc:`NetworkXError` is raised.
Examples

>>> e = [
... (1, 2),
... (1, 3),
... (2, 3),
... (2, 4),
... (3, 4),
... (3, 5),
... (3, 6),
... (4, 5),
... (4, 6),
... (5, 6),
... (7, 8),
... ]
>>> G = nx.Graph(e)
>>> G.add_node(9)
>>> setlist = nx.chordal_graph_cliques(G)
z$This will return a generator in 3.0.cSsh]}qSrr.0crrr z(chordal_graph_cliques..)warningswarnDeprecationWarning_chordal_graph_cliques)rmsgrrrrs+rcCs<tstdd}tD]}tt}q dS)a)Returns the treewidth of the chordal graph G.
Parameters

G : graph
A NetworkX graph
Returns

treewidth : int
The size of the largest clique in the graph minus one.
Raises

NetworkXError
The algorithm does not support DiGraph, MultiGraph and MultiDiGraph.
If the input graph is an instance of one of these classes, a
:exc:`NetworkXError` is raised.
The algorithm can only be applied to chordal graphs. If the input
graph is found to be nonchordal, a :exc:`NetworkXError` is raised.
Examples

>>> e = [
... (1, 2),
... (1, 3),
... (2, 3),
... (2, 4),
... (3, 4),
... (3, 5),
... (3, 6),
... (4, 5),
... (4, 6),
... (5, 6),
... (7, 8),
... ]
>>> G = nx.Graph(e)
>>> G.add_node(9)
>>> nx.chordal_graph_treewidth(G)
3
References

.. [1] https://en.wikipedia.org/wiki/Tree_decomposition#Treewidth
r)rrrrmaxr)rZ
max_cliqueZcliquerrrrs.
rcCsLtdkrtd}dkr,dS}dd}kS)z&Returns True if G is a complete graph.rz'Self loop found in _is_complete_graph()rTr/)rZnumber_of_selfloopsrnumber_of_nodesZnumber_of_edges)rr#eZ max_edgesrrr_is_complete_graphs
r3cCsHt}D]6}ttg}rfSqdS)z6 Given a noncomplete graph G, returns a missing edge.N)rlistkeyspop)rnodesr missingrrr_find_missing_edges
r9cs<d}D].}tfddD}kr}}qS)z`Returns a the node in choices that has more connections in G
to nodes in wanna_connect.
r.csg]}vrqSrr)r%y
wanna_connectrr
.r(z)_max_cardinality_node..)r)rchoicesr<Z
max_numberxnumberZmax_cardinality_noderr;r_max_cardinality_node(srAcCst}durt}h}d}rt}t@}}trtt}krt
dq,t\} }
 
fSq,dS)a( Given a graph G, starts a max cardinality search
(starting from s if s is given and from an arbitrary node otherwise)
trying to find a nonchordal cycle.
If it does find one, it returns (u,v,w) where u,v,w are the three
nodes that together with s are involved in the cycle.
Nr.ztreewidth_bound exceeded: r)rrremoverArsubgraphr3r0rrr r9)rrr
unnumberednumberedZcurrent_treewidthr!clique_wanna_besgr r"rrrr5s*
rc#ststdfddtDD]}dkrHtVq(t}t} h}h}rt
} t@}
}trkstV}qrtdqrtVq(dS)aReturns all maximal cliques of a chordal graph.
The algorithm breaks the graph in connected components and performs a
maximum cardinality search in each component to get the cliques.
Parameters

G : graph
A NetworkX graph
Returns

iterator
An iterator over maximal cliques, each of which is a frozenset of
nodes in `G`. The order of cliques is arbitrary.
Raises

NetworkXError
The algorithm does not support DiGraph, MultiGraph and MultiDiGraph.
If the input graph is an instance of one of these classes, a
:exc:`NetworkXError` is raised.
The algorithm can only be applied to chordal graphs. If the input
graph is found to be nonchordal, a :exc:`NetworkXError` is raised.
Examples

>>> e = [
... (1, 2),
... (1, 3),
... (2, 3),
... (2, 4),
... (3, 4),
... (3, 5),
... (3, 6),
... (4, 5),
... (4, 6),
... (5, 6),
... (7, 8),
... ]
>>> G = nx.Graph(e)
>>> G.add_node(9)
>>> cliques = [c for c in _chordal_graph_cliques(G)]
>>> cliques[0]
frozenset({1, 2, 3})
rc3s]}VqdSN)rCcopyr$rrr r(z)_chordal_graph_cliques..r/N)rrrrr1 frozensetr7rrrBrArZ neighborsrCr3)rCrDr!rErFZnew_clique_wanna_berGrrrr,Ys./
r,Zdirectedc s0}ddD}tr(fSt}ddDt}ttddD]}tfddd} <g}D]l}
rqfd d
D} t
 grfqD]}

d7<qq`fS)aReturn a copy of G completed to a chordal graph
Adds edges to a copy of G to create a chordal graph. A graph G=(V,E) is
called chordal if for each cycle with length bigger than 3, there exist
two nonadjacent nodes connected by an edge (called a chord).
Parameters

G : NetworkX graph
Undirected graph
Returns

H : NetworkX graph
The chordal enhancement of G
alpha : Dictionary
The elimination ordering of nodes of G
Notes

There are different approaches to calculate the chordal
enhancement of a graph. The algorithm used here is called
MCSM and gives at least minimal (local) triangulation of graph. Note
that this triangulation is not necessarily a global minimum.
https://en.wikipedia.org/wiki/Chordal_graph
References

.. [1] Berry, Anne & Blair, Jean & Heggernes, Pinar & Peyton, Barry. (2004)
Maximum Cardinality Search for Computing Minimal Triangulations of
Graphs. Algorithmica. 39. 287298. 10.1007/s0045300410843.
Examples

>>> from networkx.algorithms.chordal import complete_to_chordal_graph
>>> G = nx.wheel_graph(10)
>>> H, alpha = complete_to_chordal_graph(G)
cSsi]
}dqSrrr%noderrr
r(zcomplete_to_chordal_graph..cSsi]
}dqSrMrrNrrrrPr(rr.csSrHr)rO)weightrrr(z+complete_to_chordal_graph..)keycsg]}krqSrrrNrQZy_weightrrr=szcomplete_to_chordal_graph..r/)rIrrrr7r4rangerr0rBZhas_edgeappendZhas_pathrCrZadd_edges_from)rralphaZchordsZunnumbered_nodesizZupdate_nodesr:Zlower_nodesrOrrTrr
s4)
r
)rsysr)ZnetworkxrZnetworkx.algorithms.componentsrZnetworkx.utilsrr__all__ZNetworkXExceptionr rmaxsizerrrr3r9rArr,r
rrrrs$
@M07
$K