ó
l™xTc           @   sh   d  d l  m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d	 „  Z d
 S(   iÿÿÿÿ(   t   divisiont   Graphc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s€  
  Basic graph implementation

  Graph interface:
      * Properties:
        - vertices: an iterable that support constant time membership tests
        - edges: an iterable returning a unique list of edges

      * Methods:
        - add_edge: insert an edge, and the vertices it connects
        - add vertex: insert a vertex
        - edges_from: return edges out from a vertex
  c         C   s   t  ƒ  |  _ t  ƒ  |  _ d  S(   N(   t   sett   verticest   edges(   t   self(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   __init__   s    c         C   s'   |  j  j | j  ƒ |  j j | ƒ d  S(   N(   R   t   updateR   t   add(   R   t   edge(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   add_edge   s    c         C   s   |  j  j | ƒ d  S(   N(   R   R   (   R   t   vertex(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt
   add_vertex   s    c            s   ‡  f d †  |  j  Dƒ S(   Nc         3   s$   |  ] } ˆ  | j  k r | Vq d  S(   N(   R   (   t   .0t   e(   R   (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pys	   <genexpr>%   s    (   R   (   R   R   (    (   R   sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt
   edges_from#   s    (   t   __name__t
   __module__t   __doc__R   R
   R   R   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR      s
   			t   VertexIndexedGraphc           B   sP   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d „  Z	 RS(   sI   
  Represents a graph, with a quick way to get all edges from a vertex
  c         C   s   t  ƒ  |  _ d  S(   N(   t   dictt   _edges_from(   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   -   s    c         C   s   |  j  S(   N(   R   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   2   s    c         C   s#   t  ƒ  } | j |  j j ƒ  Œ  | S(   N(   R   R   R   t   values(   R   t   result(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   8   s    	c         C   s7   x0 | j  D]% } |  j j | t ƒ  ƒ j | ƒ q
 Wd  S(   N(   R   R   t
   setdefaultR   R   (   R   R	   t   v(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR
   @   s    c         C   s   |  j  j | t ƒ  ƒ d  S(   N(   R   R   R   (   R   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   F   s    c         C   s   |  j  j | t ƒ  ƒ S(   N(   R   t   getR   (   R   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   K   s    (
   R   R   R   R   t   propertyR   R   R
   R   R   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   (   s   			t   Edgec           B   s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s!   
  Edge in an undirected graph
  c         C   s   t  | | f ƒ |  _ d  S(   N(   t	   frozensetR   (   R   t   vertex1t   vertex2(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   U   s    c         C   sm   | |  j  k s t ‚ t |  j  ƒ d k re |  j  j | g ƒ } t | ƒ d k sW t ‚ t | ƒ d S| Sd  S(   Ni   i   i    (   R   t   AssertionErrort   lent
   differencet   list(   R   t   currentt   diff(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   other_vertexZ   s    c         C   s   |  j  | j  k S(   N(   R   (   R   t   other(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   __eq__g   s    c         C   s   |  | k S(   N(    (   R   R'   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   __ne__l   s    c         C   s   t  |  j ƒ S(   N(   t   hashR   (   R   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   __hash__q   s    (   R   R   R   R   R&   R(   R)   R+   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyR   P   s   				c         c   s¸   |  | j  k s t ‚ |  Vt |  g ƒ } |  g } x | r³ | d } xh | j d | ƒ D]J } | j d | ƒ } | | k r‚ qX n  | V| j | ƒ | j | ƒ PqX W| j ƒ  q5 Wd S(   s/   
  Preorder depth-first traversal of a graph
  iÿÿÿÿR   R$   N(   R   R    R   R   R&   R   t   appendt   pop(   R   t   grapht   discoveredst   stackR$   R	   R'   (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   preorder_depth_first_iterationv   s    		
c         c   sY   t  |  j ƒ } xC | rT | j ƒ  } t t d | d |  ƒ ƒ } | j | ƒ | Vq Wd  S(   NR   R.   (   R   R   R-   R#   R1   t   difference_update(   R.   t   unusedR   t	   component(    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   connected_components“   s    	N(   t
   __future__R    t   objectR   R   R   R1   R5   (    (    (    sJ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/scitbx/math/undirected_graph.pyt   <module>   s
   %(&	