K iddlmZddlmZddlZgdZejdZejdddZ ejddd Z ejdd Z ejdd Z Gd d Z GddZdZGddZGddej"Zy)) defaultdictdeepcopyN)check_planarity is_planarPlanarEmbeddingc"t|ddS)a$Returns True if and only if `G` is planar. A graph is *planar* iff it can be drawn in a plane without any edge intersections. Parameters ---------- G : NetworkX graph Returns ------- bool Whether the graph is planar. Examples -------- >>> G = nx.Graph([(0, 1), (0, 2)]) >>> nx.is_planar(G) True >>> nx.is_planar(nx.complete_graph(5)) False See Also -------- check_planarity : Check if graph is planar *and* return a `PlanarEmbedding` instance if True. F)counterexampler)r)Gs c/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/planarity.pyrr s< 1U 3A 66T) returns_graphcdt|}|j}||r dt|fSyd|fS)adCheck if a graph is planar and return a counterexample or an embedding. A graph is planar iff it can be drawn in a plane without any edge intersections. Parameters ---------- G : NetworkX graph counterexample : bool A Kuratowski subgraph (to proof non planarity) is only returned if set to true. Returns ------- (is_planar, certificate) : (bool, NetworkX graph) tuple is_planar is true if the graph is planar. If the graph is planar `certificate` is a PlanarEmbedding otherwise it is a Kuratowski subgraph. Examples -------- >>> G = nx.Graph([(0, 1), (0, 2)]) >>> is_planar, P = nx.check_planarity(G) >>> print(is_planar) True When `G` is planar, a `PlanarEmbedding` instance is returned: >>> P.get_data() {0: [1, 2], 1: [0], 2: [0]} Notes ----- A (combinatorial) embedding consists of cyclic orderings of the incident edges at each vertex. Given such an embedding there are multiple approaches discussed in literature to drawing the graph (subject to various constraints, e.g. integer coordinates), see e.g. [2]. The planarity check algorithm and extraction of the combinatorial embedding is based on the Left-Right Planarity Test [1]. A counterexample is only generated if the corresponding parameter is set, because the complexity of the counterexample generation is higher. See also -------- is_planar : Check for planarity without creating a `PlanarEmbedding` or counterexample. References ---------- .. [1] Ulrik Brandes: The Left-Right Planarity Test 2009 http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.217.9208 .. [2] Takao Nishizeki, Md Saidur Rahman: Planar graph drawing Lecture Notes Series on Computing: Volume 12 2004 FFNT) LRPlanarity lr_planarityget_counterexampler r planarity_state embeddings r rr*sE~"!nO,,.I ,Q// /Yr cdt|}|j}||r dt|fSyd|fS)z-Recursive version of :meth:`check_planarity`.FrT)rlr_planarity_recursiveget_counterexample_recursivers r check_planarity_recursivervsD"!nO668I 6q99 9Yr c`tj|}t|drtjdtj}|D]\}t ||}|D]G}|j ||t|ds$|j |||j ||I^|S)aObtains a Kuratowski subgraph. Raises nx.NetworkXException if G is planar. The function removes edges such that the graph is still not planar. At some point the removal of any edge would make the graph planar. This subgraph must be a Kuratowski subgraph. Parameters ---------- G : NetworkX graph Returns ------- subgraph : NetworkX graph A Kuratowski subgraph that proves that G is not planar. r!G is planar - no counter example.)nxGraphrNetworkXExceptionlist remove_edgeadd_edger subgraphunbrsvs r rrs*  Aq!""#FGGxxzH (AaDz (A MM!Q q!!$ 1a !!!Q'  (( Or c`tj|}t|drtjdtj}|D]\}t ||}|D]G}|j ||t|ds$|j |||j ||I^|S)z0Recursive version of :meth:`get_counterexample`.rr)rrrrr r!r"r#s r rrs  A #A&""#FGGxxzH (AaDz (A MM!Q (+A. 1a !!!Q'  (( Or c*eZdZdZddZdZdZdZy)IntervalzRepresents a set of return edges. All return edges in an interval induce a same constraint on the contained edges, which means that all edges must either have a left orientation or all edges must have a right orientation. Nc ||_||_yNlowhigh)selfr.r/s r __init__zInterval.__init__s r c>|jduxr|jduS)zCheck if the interval is emptyNr-r0s r emptyzInterval.emptysxx45DII$55r cBt|j|jS)zReturns a copy of this interval)r*r.r/r3s r copyz Interval.copys$)),,r cz|j xr)|j|j|j|kDS)z0Returns True if interval I conflicts with edge b)r4lowptr/)r0brs r conflictingzInterval.conflictings=  L%%dii0?3H3H3KK r )NN)__name__ __module__ __qualname____doc__r1r4r6r:r r r*r*s6- r r*c<eZdZdZeefdZdZdZy) ConflictPairzRepresents a different constraint between two intervals. The edges in the left interval must have a different orientation than the one in the right interval. c ||_||_yr,leftright)r0rDrEs r r1zConflictPair.__init__s  r cL|j}|j|_||_y)zSwap left and right intervalsNrC)r0temps r swapzConflictPair.swapsyyJJ  r c|jjr#|j|jjS|jjr#|j|jjSt |j|jj|j|jjS)z.Returns the lowest lowpoint of a conflict pair)rDr4r8rEr.min)r0rs r lowestzConflictPair.lowests 99?? "((8 8 ::   "((7 7  ! !$))-- 0/2G2G 2W  r N)r;r<r=r>r*r1rHrKr?r r rArAs! %Jhj  r rAc|sy|dS)z(Returns the element on top of the stack.Nr?)ls r top_of_stackrOs  R5Lr cfeZdZdZgdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZy)rz5A class to maintain the state during planarity check.)r rootsheightr8lowpt2 nesting_depth parent_edgeDGadjs ordered_adjsrefsideS stack_bottom lowpt_edgeleft_ref right_refrctj|_|jj|j|j D]0}|d|dk7s|jj |d|d2g|_td|_ i|_ i|_ i|_ td|_ tj|_|jj|ji|_i|_td|_td|_g|_i|_i|_i|_i|_t3|_y)Nrcyr,r?r?r r z&LRPlanarity.__init__..&r cyr,r?r?r r rcz&LRPlanarity.__init__..-rdr cyr,r?r?r r rcz&LRPlanarity.__init__..6rdr cy)Nrar?r?r r rcz&LRPlanarity.__init__..7rdr )rrr add_nodes_fromnodesedgesr"rQrrRr8rSrTrUDiGraphrVrWrXrYrZr[r\r]r^r_rr)r0r es r r1zLRPlanarity.__init__s agg& ,Atqt|!ad+ , ",/   '|4**, qww' |, *  (*r cjjdkDr<jjdjjzdz kDryjD]'tjj<)jD]Mj dj <j jjOd_d_ d_jD]-tjfdj</j D]jryd_d_d_d_d_jj$D]0}j'|j(|zj(|<2j*j-jj.jD]ctjfdj<d}jD]"}j*j1|| |}$ed_ d_d_j D]j5d_d_d_ d_d_d_j*S) zExecute the LR planarity test. Returns ------- embedding : dict If the graph is planar an embedding is returned. Otherwise None. Nrc&j|fSr,rTxr0r's r rcz*LRPlanarity.lr_planarity..c$*<*.wrur ccw)r ordersizer rWrRrQappenddfs_orientationrSrVsortedrX dfs_testingr8r[r\r]rjsignrTrrhri add_half_edgerY dfs_embeddingrUr^r_rZr0rl previous_nodewr's` @r rzLRPlanarity.lr_planarityCs 66<<>A $&&++-!dfflln2Dq2H"H +Aq ?DIIaL + (A{{1~%!" A !!!$$$Q'  (   A#) D$D  a    A##A&      IA$(IIaL43E3Ea3H$HD  q ! I %%dggmm4 "A#) D$D  a !M&&q) ",,Q},E !  " "! "A   q ! "    ~~r cnjjdkDr<jjdjjzdz kDryjD]Mjdj<jj j Od_jD]-tjfdj</jD]jryjjD]0}j|j|zj|<2jjjj jD]ctjfdj<d}jD]"}jj#|| |}$ejD]j%jS) z*Recursive version of :meth:`lr_planarity`.rnrorpNrc&j|fSr,rrrss r rcz4LRPlanarity.lr_planarity_recursive..rur rvc&j|fSr,rrrss r rcz4LRPlanarity.lr_planarity_recursive..rur ry)r r{r|rRrQr}dfs_orientation_recursiverVrrXdfs_testing_recursiverjsign_recursiverTrrhrirdfs_embedding_recursivers` @r rz"LRPlanarity.lr_planarity_recursives 66<<>A $&&++-!dfflln2Dq2H"H 2A{{1~%!" A !!!$..q1  2 A#) D$D  a    A--a0  SA$($7$7$:T=O=OPQ=R$RD  q ! S %%dggmm4 "A#) D$D  a !M&&q) ",,Q},E !  " " ,A  ( ( + ,~~r c|g}td}td}|r|j}|j|}|j|||dD]}||f}||s||f|jj vs||f|jj vr||xxdz cc<P|jj |||j||j|<|j||j|<|j|X||j|<|j|dz|j|<|j||j|d||<nx|j||j|<d|j|z|j|<|j||j|kr|j|xxdz cc<||j||j|krPt|j||j||j|<|j||j|<n|j||j|kDr4t|j||j||j|<n3t|j||j||j|<||xxdz cc<|ryy)z=Orient the graph by DFS, compute lowpoints and nesting order.cyNrr?r?r r rcz-LRPlanarity.dfs_orientation..rdr cyNFr?r?r r rcz-LRPlanarity.dfs_orientation..rdr NraTrn) rpoprUrWrVrjr"rRr8rSr}rTrJ)r0r' dfs_stackind skip_initrlrvws r r~zLRPlanarity.dfs_orientationsqC )$ .  A  #AYYq\#a&(+& V }1v.1a&DGGMM2IA!  GG$$Q*%)[[^DJJrN&*kk!nDKKO{{1~-.0((+)-Q!); A!((+!((+(, " )-Q 2*+TZZ^);""2&;;r?T[[^3&&r*a/*=zz"~ 1 5),TZZ]DKKO)L A(, 2 1 B$**Q-7),T[[^TZZ^)L A),T[[^T[[_)M AA! M&  r c|j|}|j|D]K}||f|jjvs||f|jjvr9||f}|jj |||j ||j |<|j ||j|<|j |@||j|<|j |dz|j |<|j|n|j ||j |<d|j |z|j|<|j||j |kr|j|xxdz cc<|U|j ||j |krQt|j ||j||j|<|j ||j |<|j ||j |kDr5t|j||j ||j|<t|j||j||j|<Ny)z-Recursive version of :meth:`dfs_orientation`.Nrarn) rUr rVrjr"rRr8rSrrTrJ)r0r'rlrrs r rz%LRPlanarity.dfs_orientation_recursives   Q  JA1v&1a&DGGMM*AQB GG  Q "![[^DJJrN"kk!nDKKO{{1~%&(  #!%Q!!3 A..q1!%Q 2&'B%7D  r "{{2Q/""2&!+&}::b>DJJqM1%(A B%HDKKN$(JJrNDJJqMZZ^djjm3%(QB%HDKKN%(QR%IDKKN9 Jr c |g}td}td}|rn|j}|j|}d}|j|||dD]}||f}||st |j |j |<||j|k(r+|j||j|d||<d}n||j|<|j jtt|||j||j|krF||j|dk(r|j||j|<n|j||sy||xxdz cc<|s||j||rny) zTest for LR partition.cyrr?r?r r rcz)LRPlanarity.dfs_testing..rdr cyrr?r?r r rcz)LRPlanarity.dfs_testing..rdr FNTrErra)rrrUrXrOr[r\r}r]rAr*r8rRadd_constraintsremove_back_edges) r0r'rrrrl skip_finalreis r rzLRPlanarity.dfs_testingsC )$ .  A  #AJ&&q)#a&(3 V },8,@D%%b)T--a00!((+!((+(, " %) .0+ l"b9I&JK::b>DKKN2D--a033-1__R-@*#33B:#(A! 3 6=**1-ILr c N|j|}|j|D]}||f}t|j|j|<||j|k(r|j |s@y||j |<|jjtt|||j||j|ks||j|dk(r|j ||j |<|j||ry||j|y)z)Recursive version of :meth:`dfs_testing`.FrrT)rUrXrOr[r\rr]r}rAr*r8rRrr)r0r'rlrrs r rz!LRPlanarity.dfs_testing_recursiveHs   Q ""1% %AQB$0$8D  b !T%%a((11!4 &(# l"b1ABCzz"~ A.))!,Q//)-)66<<>zz!''++&A677==?ggllnAG,-GGLLDHHQWW[[)ggkk (,(:%DFF#t'8'8'<<!$466"''33B= FFB % B%B& Aww""2t,ww""2t,$%GGLLDHHQWW[[ !ww{{&ggkk vv||~'(vv{{$AFFJ#466"''33B= FFB % B%B&& 177==? FFMM! r cZ|d}|jrt|jj||j|k(r|jj }|j j #d|j|j j <|jr5t|jj||j|k(r|jr|jj }|j j|j jd|k(re|j|j j|j _|j j|j jd|k(re|j j|j j k|jj |j|j j <d|j|j j <d|j _|jj|jjd|k(re|j|jj|j_|jj|jjd|k(re|jj|jj k|j j |j|jj <d|j|jj <d|j_|jj||j||j|krt|jj j}t|jjj}|1||j||j|kDr||j|<y||j|<yy)NrrMra) r[rOrKrRrrDr.rZr/rYrEr}r8)r0rlr%rhlhrs r rzLRPlanarity.remove_back_edgess aDffdff-44T:dkk!nL Avvzz%(* !&&**%ffdff-44T:dkk!nL 66 A&&++)affkk!n.A"hhqvv{{3 &&++)affkk!n.Avv{{"qvvzz'='(ww{{$(* !&&**%! '',,*qww||A!/C#xx 5 '',,*qww||A!/Cww||# (?() %)+ !''++&" FFMM!  ::a=4;;q> )dff%**//Bdff%++00B~2:B$**R.1P     *r cx|g}td}|r(|j}|j|||dD]}||xxdz cc<||f}||j|k(r^|jj ||||j |<||j|<|j||j|nz|j|dk(r,|jj|||j||jj|||j |||j |<|r'yy)zCompletes the embedding.cyrr?r?r r rcz+LRPlanarity.dfs_embedding..rdr Nrarycw) rrrXrUradd_half_edge_firstr^r_r}rZr)r0r'rrrrs r rzLRPlanarity.dfs_embeddings'C )$ A&&q)#a&(3 -A! V))!,,NN66q!<'(DMM!$()DNN1%$$Q'$$Q'yy})44Qt~~a?P4Q44QdmmA>N4O+, a(# -r c|j|D]}||f}||j|k(rL|jj||||j|<||j |<|j |e|j|dk(r,|jj|||j ||jj|||j|||j|<y)z+Recursive version of :meth:`dfs_embedding`.raryrN) rXrUrrr^r_rrZr)r0r'rrs r rz#LRPlanarity.dfs_embedding_recursives""1% )AQBT%%a((221a8#$ a $%q!,,Q/99R=A%NN00A4>>!;L0MNN00A$--:J0K'(DMM!$ )r cv|g}td}|r|j}|j|Q|j||j|j||j|||<d|j|<n'|j|xx|j||zcc<|r|j|S)z:Resolve the relative side of an edge to the absolute side.cyr,r?r?r r rcz"LRPlanarity.sign..rdr N)rrrYr}rZ)r0rlrold_refs r rzLRPlanarity.signsC l+ Axx{&  #  !-!XXa[ "  !  '!* 55 yy|r c|j|J|j||j|j|z|j|<d|j|<|j|S)z"Recursive version of :meth:`sign`.N)rYrZr)r0rls r rzLRPlanarity.sign_recursivesX 88A; "99Q<$*=*=dhhqk*JJDIIaLDHHQKyy|r N)r;r<r=r> __slots__r1rrr~rrrrrrrrrr?r r rrsW?I*&+PM^-^3jJB/b6*X%!N-:)$(r rceZdZdZdfd ZdZdZdZdZdZ d Z ddd fd Z d Z d Z dZdZdZdZdZdZddZdZdfd ZddZxZS)ra Represents a planar graph with its planar embedding. The planar embedding is given by a `combinatorial embedding `_. .. note:: `check_planarity` is the preferred way to check if a graph is planar. **Neighbor ordering:** In comparison to a usual graph structure, the embedding also stores the order of all neighbors for every vertex. The order of the neighbors can be given in clockwise (cw) direction or counterclockwise (ccw) direction. This order is stored as edge attributes in the underlying directed graph. For the edge (u, v) the edge attribute 'cw' is set to the neighbor of u that follows immediately after v in clockwise direction. In order for a PlanarEmbedding to be valid it must fulfill multiple conditions. It is possible to check if these conditions are fulfilled with the method :meth:`check_structure`. The conditions are: * Edges must go in both directions (because the edge attributes differ) * Every edge must have a 'cw' and 'ccw' attribute which corresponds to a correct planar embedding. As long as a PlanarEmbedding is invalid only the following methods should be called: * :meth:`add_half_edge` * :meth:`connect_components` Even though the graph is a subclass of nx.DiGraph, it can still be used for algorithms that require undirected graphs, because the method :meth:`is_directed` is overridden. This is possible, because a valid PlanarGraph must have edges in both directions. **Half edges:** In methods like `add_half_edge` the term "half-edge" is used, which is a term that is used in `doubly connected edge lists `_. It is used to emphasize that the edge is only in one direction and there exists another half-edge in the opposite direction. While conventional edges always have two faces (including outer face) next to them, it is possible to assign each half-edge *exactly one* face. For a half-edge (u, v) that is oriented such that u is below v then the face that belongs to (u, v) is to the right of this half-edge. See Also -------- is_planar : Preferred way to check if an existing graph is planar. check_planarity : A convenient way to create a `PlanarEmbedding`. If not planar, it returns a subgraph that shows this. Examples -------- Create an embedding of a star graph (compare `nx.star_graph(3)`): >>> G = nx.PlanarEmbedding() >>> G.add_half_edge(0, 1) >>> G.add_half_edge(0, 2, ccw=1) >>> G.add_half_edge(0, 3, ccw=2) >>> G.add_half_edge(1, 0) >>> G.add_half_edge(2, 0) >>> G.add_half_edge(3, 0) Alternatively the same embedding can also be defined in counterclockwise orientation. The following results in exactly the same PlanarEmbedding: >>> G = nx.PlanarEmbedding() >>> G.add_half_edge(0, 1) >>> G.add_half_edge(0, 3, cw=1) >>> G.add_half_edge(0, 2, cw=3) >>> G.add_half_edge(1, 0) >>> G.add_half_edge(2, 0) >>> G.add_half_edge(3, 0) After creating a graph, it is possible to validate that the PlanarEmbedding object is correct: >>> G.check_structure() Nc t|dd|i||j|_|j|_|j|_y)Nincoming_graph_datar?)superr1_PlanarEmbedding__forbiddenr"add_edges_fromadd_weighted_edges_from)r0rattr __class__s r r1zPlanarEmbedding.__init__[sB I-@IDI(( "..'+'7'7$r ctd)zForbidden operation Any edge additions to a PlanarEmbedding should be done using method `add_half_edge`. z=Use `add_half_edge` method to add edges to a PlanarEmbedding.)NotImplementedError)r0argskwargss r __forbiddenzPlanarEmbedding.__forbiddenas " K  r cRi}|D]}t|j|||<!|S)aConverts the adjacency structure into a better readable structure. Returns ------- embedding : dict A dict mapping all nodes to a list of neighbors sorted in clockwise order. See Also -------- set_data )r neighbors_cw_order)r0rr's r get_datazPlanarEmbedding.get_dataks7  ?  sA B 9B C$B>>CcR|D]"}||jvs|j|$y)aRemove multiple nodes. Parameters ---------- nodes : iterable container A container of nodes (list, dict, set, etc.). If a node in the container is not in the graph it is silently ignored. See Also -------- remove_node Notes ----- When removing nodes from an iterator over the graph you are changing, a `RuntimeError` will be raised with message: `RuntimeError: dictionary changed size during iteration`. This happens when the graph's underlying dictionary is modified during iteration. To avoid this error, evaluate the iterator into a separate object, e.g. by using `list(iterator_of_nodes)`, and pass this object to `G.remove_nodes_from`. N)rr)r0rirs r remove_nodes_fromz!PlanarEmbedding.remove_nodes_froms,0 $ADJJ  # $r c#K|j|}|sytt|}|||d}||k7r|||d}||k7ryyw)zGenerator for the neighbors of v in clockwise order. Parameters ---------- v : node Yields ------ node Nr)rnextr)r0r'succs start_node current_nodes r rz"PlanarEmbedding.neighbors_cw_orderse 1  (5/* Z(. L(  .t4LL(s A A Arrzc|jj|}|rtt|j|}|a||vrt j d|t j d||d}t ||||||||d<|||d<||k7}n^|G||vrt j d||d} t |||| |||| d<|||d<d}nt j d |r|j|||<yy||t j d t ||||| y) aAdds a half-edge from `start_node` to `end_node`. If the half-edge is not the first one out of `start_node`, a reference node must be provided either in the clockwise (parameter `cw`) or in the counterclockwise (parameter `ccw`) direction. Only one of `cw`/`ccw` can be specified (or neither in the case of the first edge). Note that specifying a reference in the clockwise (`cw`) direction means inserting the new edge in the first counterclockwise position with respect to the reference (and vice-versa). Parameters ---------- start_node : node Start node of inserted edge. end_node : node End node of inserted edge. cw, ccw: node End node of reference edge. Omit or pass `None` if adding the first out-half-edge of `start_node`. Raises ------ NetworkXException If the `cw` or `ccw` node is not a successor of `start_node`. If `start_node` has successors, but neither `cw` or `ccw` is provided. If both `cw` and `ccw` are specified. See Also -------- connect_components Nz!Invalid clockwise reference node.z$Only one of cw/ccw can be specified.rzrrz(Invalid counterclockwise reference node.TzLNode already has out-half-edge(s), either cw or ccw reference node required.zInvalid reference node.)rzr) rgetrrrrrr"r) r0rend_noderrzr leftmost_nbrref_ccwmove_leftmost_nbr_to_endref_cwrs r rzPlanarEmbedding.add_half_edgesD z* J)? @AL~U?**+NOO?**+QRR)E* X"' J'/gt$#+b % ,.+=(e#**+UVVsD) X&c J'/f e$#+c 4 +/(&&b(',ii &=l# (~&&'@AA G ZxH  Mr c|D]z} t|j|}t||}||k7rd}tj|||D]+}|j ||rd}tj||t}tj |D]}t|dk(rt|} d} d} |D]<}|j|D]&}| dz } ||f|vs| dz } |j|||(>| dz} | | z | zdk7ssd}tj|y#t$r }d|}tj||d}~wwxYw) a(Runs without exceptions if this object is valid. Checks that the following properties are fulfilled: * Edges go in both directions (because the edge attributes differ). * Every edge has a 'cw' and 'ccw' attribute which corresponds to a correct planar embedding. Running this method verifies that the underlying Graph must be planar. Raises ------ NetworkXException This exception is raised with a short explanation if the PlanarEmbedding is invalid. z5Bad embedding. Missing orientation for a neighbor of Nz3Bad embedding. Edge orientations not set correctly.z-Bad embedding. Opposite half-edge is missing.rarrnz7Bad embedding. The graph does not match Euler's formula) setrrrrhas_edgeconnected_componentslen traverse_face) r0r' sorted_nbrsrmsg unsorted_nbrsrcounted_half_edges component num_nodesnum_half_edges num_faces num_edgess r check_structurezPlanarEmbedding.check_structure<s$ 4A 9!$"9"9!"<=  QLMm+K**3//!W 4}}Q*IC..s33  4 4$!U006 0I9~"IINI E003EA"a'N1v%77!Q **1a1CD E E'!+I9$y0A5O**3//' 0! 9MaSQ**3/S8 9sD++ E4EEc,|j|||y)aAdds a half-edge from start_node to end_node. The half-edge is added counter clockwise next to the existing half-edge (start_node, reference_neighbor). Parameters ---------- start_node : node Start node of inserted edge. end_node : node End node of inserted edge. reference_neighbor: node End node of reference edge. Raises ------ NetworkXException If the reference_neighbor does not exist. See Also -------- add_half_edge add_half_edge_cw connect_components rNrr0rrreference_neighbors r add_half_edge_ccwz!PlanarEmbedding.add_half_edge_ccwvs6 :x4FGr c,|j|||y)axAdds a half-edge from start_node to end_node. The half-edge is added clockwise next to the existing half-edge (start_node, reference_neighbor). Parameters ---------- start_node : node Start node of inserted edge. end_node : node End node of inserted edge. reference_neighbor: node End node of reference edge. Raises ------ NetworkXException If the reference_neighbor does not exist. See Also -------- add_half_edge add_half_edge_ccw connect_components ryNrrs r add_half_edge_cwz PlanarEmbedding.add_half_edge_cws4 :x5GHr c |j|}|j|}||d}||d}||d}||d}||=|j||=||=|j||=||k7r|||d<|||d<||k7r|||d<|||d<tj |y#t$r"} tjd|d|d| d} ~ wwxYw)aRemove the edge between u and v. Parameters ---------- u, v : nodes Remove the half-edges (u, v) and (v, u) and update the edge ordering around the removed edge. Raises ------ NetworkXError If there is not an edge between u and v. See Also -------- remove_edges_from : remove a collection of edges rrzz The edge -rN)rrrrrr) r0r%r'rsuccs_vuv_cwuv_ccwvu_cwvu_ccwrs r r!zPlanarEmbedding.remove_edges$ jjmGjjmGAJt$EQZ&FAJt$EQZ&F  1 a   1 a Ez(.u%(-%Ez(.u%(-%   ""A3as"BC  sBB&& C/C  Cc|D]=}|dd\}}||jvs||j|vs,|j||?y)aRemove all edges specified in ebunch. Parameters ---------- ebunch: list or container of edge tuples Each pair of half-edges between the nodes given in the tuples will be removed from the graph. The nodes can be passed as: - 2-tuples (u, v) half-edges (u, v) and (v, u). - 3-tuples (u, v, k) where k is ignored. See Also -------- remove_edge : remove a single edge Notes ----- Will fail silently if an edge in ebunch is not in the graph. Examples -------- >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc >>> ebunch = [(1, 2), (2, 3)] >>> G.remove_edges_from(ebunch) Nrn)rr!)r0ebunchrlr%r's r remove_edges_fromz!PlanarEmbedding.remove_edges_fromsN4 'ARa5DAqDJJ1 1 #5  A&  'r cX||jvr1|j|r"tt|j|}nd}|j|||||jvr1|j|r"tt|j|}nd}|j|||y)a;Adds half-edges for (v, w) and (w, v) at some position. This method should only be called if v and w are in different components, or it might break the embedding. This especially means that if `connect_components(v, w)` is called it is not allowed to call `connect_components(w, v)` afterwards. The neighbor orientations in both directions are all set correctly after the first call. Parameters ---------- v : node w : node See Also -------- add_half_edge Nr)rrrr)r0r'rrYs r connect_componentsz"PlanarEmbedding.connect_componentss&  ?tzz!}x 1 ./CC 1aC(  ?tzz!}x 1 ./CC 1aC(r c|jj|}|rtt|nd}|j |||y)atAdd a half-edge and set end_node as start_node's leftmost neighbor. The new edge is inserted counterclockwise with respect to the current leftmost neighbor, if there is one. Parameters ---------- start_node : node end_node : node See Also -------- add_half_edge connect_components Nr)rrrrr)r0rrrrs r rz#PlanarEmbedding.add_half_edge_firsts?  z*16tHUO,4  :xLAr c |||d}||fS)zReturns the following half-edge left of a face. Parameters ---------- v : node w : node Returns ------- half-edge : tuple rzr?)r0r'rnew_nodes r next_face_half_edgez#PlanarEmbedding.next_face_half_edge-s 71:e$({r c<| t}|g}|j||f|}|}|||d}||k7s||k7r`|j||j||\}}||f|vrt j d|j||f||k7rZ||k7r`|S)aReturns nodes on the face that belong to the half-edge (v, w). The face that is traversed lies to the right of the half-edge (in an orientation where v is below w). Optionally it is possible to pass a set to which all encountered half edges are added. Before calling this method, this set must not include any half-edges that belong to the face. Parameters ---------- v : node Start node of half-edge. w : node End node of half-edge. mark_half_edges: set, optional Set to which all encountered half-edges are added. Returns ------- face : list A list of nodes that lie on this face. rz&Bad planar embedding. Impossible face.)raddr}r rr)r0r'rmark_half_edges face_nodes prev_nodecur_node incoming_nodes r rzPlanarEmbedding.traverse_face<s0  "!eOS QF# Q 4( !myM9   h '"&":":9h"O Ix8$7**+STT   H 5 6 !myM9r cy)zA valid PlanarEmbedding is undirected. All reverse edges are contained, i.e. for every existing half-edge (v, w) the half-edge in the opposite direction (w, v) is also contained. Fr?r3s r is_directedzPlanarEmbedding.is_directedgsr c|durtjj|S|j}|jj |j|j d|jjDt|j|+d|jjD|S)NTc3FK|]\}}||jfywr,)r6.0rds r z'PlanarEmbedding.copy..usF41a!QVVXFs!c3xK|]2\}}|jD]\}}|||jf4ywr,)itemsr6)rr%r&r'datadicts r rz'PlanarEmbedding.copy..vsH0 4#zz|0 88==? #0 #0 s8:) r graphviewsgeneric_graph_viewrgraphupdaterhrrrr_adj)r0as_viewr rs r r6zPlanarEmbedding.copyps d?==33D9 9 NN  tzz" F4::3C3C3EFF dnna/0 99??,0  r c|r td|r td|j}|}|jjt |j|j d|j jD|jd|jjD|S)a Returns a non-embedding undirected representation of the graph. This method strips the planar embedding information and provides a simple undirected graph representation. While creating the undirected graph, all edge attributes are retained except the ``"cw"`` and ``"ccw"`` attributes which are removed from the edge data. Those attributes are specific to the requirements of planar embeddings. Parameters ---------- reciprocal : bool (optional) Not supported for PlanarEmbedding. This parameter raises an exception if used. All valid embeddings include reciprocal half-edges by definition, making this parameter unnecessary. as_view : bool (optional, default=False) Not supported for PlanarEmbedding. This parameter raises an exception if used. Returns ------- G : Graph An undirected graph with the same name and nodes as the PlanarEmbedding. Edges are included with their data, except for the ``"cw"`` and ``"ccw"`` attributes, which are omitted. Notes ----- - If edges exist in both directions ``(u, v)`` and ``(v, u)`` in the PlanarEmbedding, attributes for the resulting undirected edge will be combined, excluding ``"cw"`` and ``"ccw"``. - A deep copy is made of the other edge attributes as well as the node and graph attributes, ensuring independence of the resulting graph. - Subclass-specific data structures used in the original graph may not transfer to the undirected graph. The resulting graph will be of type ``nx.Graph``. z'reciprocal=True' is not supported for PlanarEmbedding. All valid embeddings include reciprocal half-edges by definition, making this parameter unnecessary.z4'as_view=True' is not supported for PlanarEmbedding.c3<K|]\}}|t|fywr,rrs r rz0PlanarEmbedding.to_undirected..sIda!Xa[)Isc 3K|]T\}}|jD]<\}}|||jDcic]\}}|dvs |t|c}}f>Vycc}}ww)>rrzN)rr)rr%r&r'rks r rz0PlanarEmbedding.to_undirected..si 4  1qwwyStq!A]r1rrrrrrrrrrr!rrrr rrr6r, __classcell__)rs@r rrsWr8  &,'R$:5.9=$INV80tH:I8'R'B)<B, )V :r rr-) collectionsrr6rnetworkxr__all__ _dispatchablerrrrrr*rArOrrkrr?r r r3s# =77@%H&HV% & %#&#L%&,  8  8{{|v bjjv r