m๒
ฅwDc           @   sx  d  k  Z  d  k Z d e f d     YZ d e f d     YZ d   Z d   Z d   Z d   Z d	   Z	 d
   Z
 d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d  g  d  Z d   Z e d j o e d d  Z e i Ge i GHe GHe i d! d" d# d$ g  e i Ge i GHe GHe i e d d g   e i Ge i GHe GHn d  S(%   Nt   Digraphc           B   sก   t  Z d  Z g  d  Z d   Z e i Z d   Z e i	 Z
 e i Z d   Z d   Z d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z RS(   s๘   
    Directed graph implementation based on dictionaries.  Dictionary has an
    entry for each vertex that refers to the set of all adjacent
    vertices. Attributes 'order' and 'size' store the number of vertices and
    edges, respectively.
    c         C   sj   t  |  t j o t |  } n t i h   x | D] } t   |  | <q7 Wt	 |  |  _
 d |  _ d S(   s   
        Initialize Graph with vertex set V. V may also be a nonnegative
        integer, in which case the vertex set is range(n)={0,...,n-1}
        i    N(   t   typet   Vt   intt   ranget   dictt   __init__t   vt   sett   selft   lent   ordert   size(   R	   R   R   (    (    t   graph.pyR      s      c         C   sr   g  } x\ |  D]T } | t |  d g 7} x( |  | D] } | d t |  g 7} q4 W| d g 7} q Wd i |  S(   Ns    ->t    s   
t    (   t   sR	   R   t   strt   wt   join(   R	   R   R   R   (    (    R   t   __str__   s      c         C   s&   |  i |  o | |  | j Sn t S(   N(   R	   t   has_keyR   R   t   False(   R	   R   R   (    (    R   t   has_edge)   s    c         C   sC   g  } x6 |  D]. } x% |  | D] } | i | | f  q Wq W| S(   s   Return list of all edges.N(   t   lR	   R   R   t   append(   R	   R   R   R   (    (    R   t   edges4   s       c         C   s4   |  i |  p  t   |  | <|  i d 7_ n d  S(   Ni   (   R	   R   R   R   R   (   R	   R   (    (    R   t
   add_vertex<   s    c         C   s"   x | D] } |  i |  q Wd  S(   N(   t   XR   R	   R   (   R	   R   R   (    (    R   t   add_verticesA   s     c         C   s$   |  | i |  |  i d 7_ d  S(   Ni   (   R	   R   t   addR   R   (   R	   R   R   (    (    R   t   add_edgeE   s    c         C   s+   x$ | D] \ } } |  i | |  q Wd  S(   N(   t   esR   R   R	   R   (   R	   R    R   R   (    (    R   t	   add_edgesI   s     c         C   sq   | |  j o` |  | =|  i d 8_ xG |  D]; } | |  | j o$ |  | i |  |  i d 8_ q* q* Wn d  S(   Ni   (   R   R	   R   R   t   removeR   (   R	   R   R   (    (    R   t   delete_vertexM   s     c         C   s"   x | D] } |  i |  q Wd  S(   N(   R   R   R	   R#   (   R	   R   R   (    (    R   t   delete_verticesV   s     c         C   s9   | |  | j o$ |  | i |  |  i d 8_ n d  S(   Ni   (   R   R	   R   t   discardR   (   R	   R   R   (    (    R   t   delete_edgeZ   s    c         C   s+   x$ | D] \ } } |  i | |  q Wd  S(   N(   t   YR   R   R	   R&   (   R	   R'   R   R   (    (    R   t   delete_edges_   s     c         C   s#   t  | d  } t i |  |  d S(   s!   Write graph into file 'filename'.t   wbN(   t   opent   filenamet   filet   picklet   dumpR	   (   R	   R+   R,   (    (    R   R.   c   s     (   t   __name__t
   __module__t   __doc__R   R   R   R   t
   has_vertexR   t   gett   adj_vertt   keyst   verticesR   R   R   R   R!   R#   R$   R&   R(   R.   (    (    (    R   R    
   s&    					 	 										t   Graphc           B   s)   t  Z d  Z d   Z d   Z d   Z RS(   s   
    Undirected graphs.
    c         C   s5   |  | i |  |  | i |  |  i d 7_ d  S(   Ni   (   R	   R   R   R   R   (   R	   R   R   (    (    R   R   m   s    c         C   sT   g  } xG |  D]? } x6 |  | D]* } | | j o | i | | f  q q Wq W| S(   N(   R   R	   R   R   R   (   R	   R   R   R   (    (    R   R   r   s      c         C   sJ   | |  | j o5 |  | i |  |  | i |  |  i d 8_ n d  S(   Ni   (   R   R	   R   R%   R   (   R	   R   R   (    (    R   R&   z   s    (   R/   R0   R1   R   R   R&   (    (    (    R   R7   i   s    		c         C   s   t  |  d  } t i |  S(   Nt   rb(   R*   R+   R,   R-   t   load(   R+   R,   (    (    R   t
   load_graph   s    c         C   sZ   |  i |  } xD | D]< } x3 |  | D]' } | | j o | i | |  q' q' Wq W| S(   s   
    G Graph or Digraph, X Set.
    Computes indiced subgraph of G with vertex set S.
    Returns Digraph if G Digraph and Graph if G graph.
    N(   t   Gt	   __class__R   t   HR   R   R   (   R;   R   R   R   R=   (    (    R   t   induced_subgraph   s       c         C   ss   t  |   } | i |  |  i |  } xD | D]< } x3 |  | D]' } | | j o | i	 | |  q@ q@ Wq/ W| S(   N(
   R   R;   R   R%   t   xR<   R=   R   R   R   (   R;   R?   R=   R   R   R   (    (    R   t   minus_vertex   s      c         C   ss   t  |   } | i |  |  i |  } xD | D]< } x3 |  | D]' } | | j o | i	 | |  q@ q@ Wq/ W| S(   N(
   R   R;   R   t   difference_updateR   R<   R=   R   R   R   (   R;   R   R=   R   R   R   (    (    R   t   minus_verticesก   s      c         C   s   t  |   i t  |   } |  i |  } x3 |  D]+ } x" |  | D] } | i	 | |  qB Wq1 Wx3 | D]+ } x" | | D] } | i	 | |  qx Wqg W| S(   N(
   R   R;   t   unionR=   R   R<   t   IR   R   R   (   R;   R=   RD   R   R   R   (    (    R   RC   ซ   s        c         C   sึ   t  |   i t  |   } |  i |  } xH | D]@ } x7 |  | D]+ } | | | j o | i	 | |  qB qB Wq1 W| SxV | D]N } | |  j o | i
 |  n x' | i |  D] } | i	 | |  qด Wq W| S(   N(   R   R;   t   intersectionR=   R   R<   RD   R   R   R   R   R4   (   R;   R=   RD   R   R   R   (    (    R   RE   ถ   s"        c         C   sf   t  |   } xS | D]K } xB | D]: } | | j  o' t i   | j  o | i | |  q  q  Wq W| S(   N(   R7   R   R;   R   R   t   randomt   pR   (   R   RG   R;   R   R   (    (    R   t   random_graphา   s       c         C   sS   t  |   } x@ | D]8 } x/ | D]' } | | j  o | i | |  q  q  Wq W| S(   N(   R7   R   R;   R   R   R   (   R   R   R   R;   (    (    R   t   complete_graphฺ   s      c         C   s   t  |   t j o t |   }  n t  |  t j o t |  } n t |  |  } x/ |  D]' } x | D] } | i	 | |  qj Wq] W| S(   N(
   R   R   R   R   t   WR7   R;   R   R   R   (   R   RJ   R   R;   R   (    (    R   t   complete_bipartite_graphโ   s        c         C   sร  t  |   t j o t |   }  n t  |  t j o t |  } n g  } |  D]  } | D] }	 | | |	 f q[ qQ ~ }
 t	 |
  } t |   } t |  } xห t | d  D]น } xx t | d  D]f } | i |  | | | f |  | d | | f  | i |  | | | f |  | | | d f  qฦ W| i |  | | | d f |  | d | | d f  qฏ WxP t | d  D]> } | i |  | d | | f |  | d | | d f  q}W| S(   Ni   (   R   R   R   R   R'   t   _[1]R?   t   yR   R7   R;   R
   t   mt   nt   it   jR   (   R   R'   R?   RP   R;   RQ   RN   RO   RL   RM   R   (    (    R   t   grid๋   s&      4  04< <c         C   sv   t  |   t j o t |   }  n t |   } t |   } x4 t | d  D]" } | i	 |  | |  | d  qL W| S(   Ni   (
   R   R   R   R   R7   R;   R
   RO   RP   R   (   R   R;   RO   RP   (    (    R   t   path๛   s       c         C   s   t  |   t j o t |   }  n t |   } t |   } x4 t | d  D]" } | i	 |  | |  | d  qL W| i	 |  | d |  d  | S(   Ni   i    (
   R   R   R   R   R7   R;   R
   RO   RP   R   (   R   R;   RO   RP   (    (    R   t   cycle  s       c         C   s\   | i |  | i |  x; |  i |  D]* } | | j o t |  | | |  q* q* Wd  S(   N(	   t   visitedR   R   t	   dfs_orderR   R;   R4   R   t   _rec_dfs(   R;   R   RU   RV   R   (    (    R   RW     s     c         C   s&   t    } g  } t |  | | |  | S(   N(   R   RU   RV   RW   R;   R   (   R;   R   RV   RU   (    (    R   t   dfs_from  s    	c         C   sH   t    } g  } x2 |  D]* } | | j o t |  | | |  q q W| S(   N(   R   RU   RV   R;   R   RW   (   R;   RV   R   RU   (    (    R   t   dfs  s    	 c         C   sa   t    } g  } xK |  D]C } | | j o0 g  } t |  | | |  | i t  |   q q W| S(   N(   R   RU   t
   componentsR;   R   RV   RW   R   (   R;   R   RZ   RV   RU   (    (    R   t   connected_components&  s    	 c         C   sน   | d  j o t | g  } n | i |  | i |  | g } xo | og | d } | d } xL |  i	   D]> } | | j o+ | i |  | i |  | i |  qn qn WqF W| S(   Ni    i   (   RU   t   NoneR   R   R   t	   bfs_orderR   t   queueR;   R4   R   (   R;   R   RU   R]   R^   R   (    (    R   t   bfs_from0  s     	 

 c         C   sH   t    } g  } x2 |  D]* } | | j o t |  | | |  q q W| S(   N(   R   RU   R]   R;   R   R_   (   R;   R]   R   RU   (    (    R   t   bfsA  s    	 t   __main__i
   f0.29999999999999999i   i   i	   i   i   i   i    (   i   i   (   i	   i   (   i   i   (   i    i   (   RF   R-   R   R    R7   R:   R>   R@   RB   RC   RE   RH   RI   RK   RR   RS   RT   RW   RX   RY   R[   R\   R_   R`   R/   R;   R   R   R(   R$   R   (   R[   RB   R7   R_   RF   R:   RW   R>   RH   RY   RX   R;   R`   R@   RR   RS   RE   RT   R    RK   RI   RC   R-   (    (    R   t   ?   s>   		_			
	
													
	