ó
 ,µ[c           @   s  d  Z  d d l Z d d l m Z d d l Z d „  Z d „  Z e	 d k re ƒ  Z
 d GHd GHd	 e j e
 ƒ e j e
 ƒ f GHd
 e j e
 ƒ GHxz d d d g D]f \ Z Z d e e f GHy/ e j e
 e e ƒ Z x e D] Z e GHqÜ WWq¢ e j k
 rd GHq¢ Xq¢ Wn  d S(   s  
=====
Words
=====

Words/Ladder Graph
------------------
Generate  an undirected graph over the 5757 5-letter words in the
datafile `words_dat.txt.gz`.  Two words are connected by an edge
if they differ in one letter, resulting in 14,135 edges. This example
is described in Section 1.1 in Knuth's book (see [1]_ and [2]_).

References
----------
.. [1] Donald E. Knuth,
   "The Stanford GraphBase: A Platform for Combinatorial Computing",
   ACM Press, New York, 1993.
.. [2] http://www-cs-faculty.stanford.edu/~knuth/sgb.html
iÿÿÿÿN(   t   ascii_lowercasec            sŽ   t  j d d ƒ } t d „  t Dƒ ƒ ‰ ‡ f d †  ‰  ‡  ‡ f d †  t ˆ ƒ Dƒ } | j ˆ ƒ x$ | D] \ } } | j | | ƒ qj W| S(   Nt   namet   wordsc         s   s$   |  ] } | t  j | ƒ f Vq d  S(   N(   t	   lowercaset   index(   t   .0t   c(    (    s.   share/doc/networkx-2.2/examples/graph/words.pys	   <genexpr>+   s    c         3   sx   xq t  t |  ƒ ƒ D]] } |  d | !|  | |  | d } } } ˆ  | } x# t | d D] } | | | VqY Wq Wd  S(   Ni    i   (   t   ranget   lenR   (   t   wordt   it   leftR   t   rightt   jt   cc(   t   lookup(    s.   share/doc/networkx-2.2/examples/graph/words.pyt   edit_distance_one-   s
    '
c         3   s:   |  ]0 } ˆ  | ƒ D] } | ˆ k r | | f Vq q d  S(   N(    (   R   R	   t   cand(   R   R   (    s.   share/doc/networkx-2.2/examples/graph/words.pys	   <genexpr>3   s    (   t   nxt   Grapht   dictR   t   sortedt   add_nodes_fromt   add_edge(   R   t   Gt   candgenR	   R   (    (   R   R   R   s.   share/doc/networkx-2.2/examples/graph/words.pyt   generate_graph)   s    c          C   s}   t  j d d ƒ }  t ƒ  } xU |  j ƒ  D]G } | j ƒ  } | j d ƒ rO q( n  t | d d !ƒ } | j | ƒ q( Wt | ƒ S(   s:   Return the words example graph from the Stanford GraphBases   words_dat.txt.gzt   rt   *i    i   (	   t   gzipt   opent   sett	   readlinest   decodet
   startswitht   strt   addR   (   t   fhR   t   linet   w(    (    s.   share/doc/networkx-2.2/examples/graph/words.pyt   words_graph;   s    	t   __main__s?   Loaded words_dat.txt containing 5757 five-letter English words.s5   Two words are connected if they differ in one letter.s    Graph has %d nodes with %d edgess   %d connected componentst   chaost   ordert   nodest   grapht   poundt   markss"   Shortest path between %s and %s ist   None(   R*   R+   (   R,   R-   (   R.   R/   (   t   __doc__R   t   stringR    R   t   networkxR   R   R(   t   __name__R   t   number_of_nodest   number_of_edgest   number_connected_componentst   sourcet   targett   shortest_patht   spt   nt   NetworkXNoPath(    (    (    s.   share/doc/networkx-2.2/examples/graph/words.pyt   <module>   s,   			