Source code for pyrigi.graph._flexibility.nac.existence
"""This module checks whether a graph can have a :prf:ref:`NAC-colorings <def-nac>`."""importnetworkxasnxfrompyrigi.graph._flexibility.nac.coreimportNACColoring,can_have_NAC_coloringfrompyrigi.graph._flexibility.nac.mono_classesimportMonoClassType,find_mono_classesfrompyrigi.graph._rigidity.genericimportis_min_rigiddef_check_for_vertex_out_of_3_cycle(graph:nx.Graph,)->NACColoring|None:""" Search for a vertex with stable neighborhood. A certificate NAC-coloring is returned if it can be constructed from the stable neighborhood of a vertex, ``None`` otherwise. If there is a single vertex outside any triangle-connected component, we can trivially find a :prf:ref:`NAC-coloring <def-nac>` for the graph according to :cite:p:`GraseggerLegerskySchicho2019{Thm 4.4,Cor 4.5}`. Definitions ----------- * :prf:ref:`NAC-coloring <def-nac>` * :prf:ref:`Triangle-connected components <def-triangle-connected-comp>` * :prf:ref:`Stable set <def-stable-set>` * :prf:ref:`Separating set <def-separating-set>` Parameters ---------- graph: The graph to work with, basic a :prf:ref:`NAC-coloring <def-nac>` constraints should be already checked. """# remove isolated verticesvertices_outside_triangle_components:set[int]=set(uforu,dingraph.degree()ifd>0)# remove vertices that are part of a triangle connected component_,component_to_edge=find_mono_classes(graph,MonoClassType.TRI_CONNECTED)forcomponent_edgesincomponent_to_edge:iflen(component_edges)==1:continuevertices_outside_triangle_components.difference_update(vforedgeincomponent_edgesforvinedge)iflen(vertices_outside_triangle_components)==0:returnNone# create corresponding NAC-coloring certificateforvinvertices_outside_triangle_components:red=set((v,u)foruingraph.neighbors(v))iflen(red)==graph.number_of_edges():# we found a wrong vertex# this may happen if the red part is the whole graphcontinueblue=set(graph.edges)# remove shared edgesblue.difference_update(red)blue.difference_update((u,v)forv,uinred)iflen(red)==0orlen(blue)==0:raiseRuntimeError("Construction of a NAC-coloring from a stable separating set failed.")return(red,blue)raiseRuntimeError("NAC-coloring was not found even though it should exist.")def_check_is_min_rigid_and_NAC_coloring_exists(graph:nx.Graph,)->bool|None:""" Check if a NAC-coloring exists for minimally 2-rigid graphs. If the graph is not minimally 2-rigid, ``None`` is returned. Otherwise, the existence is decided using the following: for minimally 2-rigid graphs it holds that there exists a :prf:ref:`NAC-coloring <def-nac>` iff the graph is not triangle connected according to :cite:p:`ClinchGaramvölgyiEtAl2024{Thm 3.4}`. Definitions ----------- * :prf:ref:`Minimal dim-rigidity <def-min-rigid-graph>` * :prf:ref:`NAC-coloring <def-nac>` * :prf:ref:`2-rigidity <def-gen-rigidity>` Parameters ---------- graph: The graph to check. """min_rigid=is_min_rigid(graph,dim=2)ifnotmin_rigid:returnNone_,components_to_edges=find_mono_classes(graph,MonoClassType.TRI_CONNECTED)returnlen(components_to_edges)!=1
[docs]defhas_NAC_coloring_checks(graph:nx.Graph)->bool|None:""" Return whether the graph has a NAC-coloring. Implementation of has_NAC_coloring, but without falling back to single_NAC_coloring. May be used before an exhaustive search that would not find anything anyway. Definitions ----------- * :prf:ref:`NAC-coloring <def-nac>` Parameters ---------- graph: The graph to check. """ifgraph.number_of_edges()<=1:returnFalseif_check_for_vertex_out_of_3_cycle(graph)isnotNone:returnTrueifnx.algorithms.connectivity.node_connectivity(graph)<2:returnTrue# Needs to be run after connectivity checksifnotcan_have_NAC_coloring(graph):returnFalseres=_check_is_min_rigid_and_NAC_coloring_exists(graph)ifresisnotNone:returnresreturnNone