U
    9%ey
                  	   @   sb  d Z ddlZddlmZ ddlmZ ddlZ	dd Z
dd Ze Zed ed	 ee ee	e d
 dD ]b\ZZede de d z$e	eeeZeD ]Zee qW qr e	jk
r   ed Y qrX qree	eeZejedd ejedd eee Ze	edZdde dZe	eZe	j eefe e	j!eedd e"  dS )aC  
==================
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 of

    Donald E. Knuth, "The Stanford GraphBase: A Platform for Combinatorial
    Computing", ACM Press, New York, 1993.
    http://www-cs-faculty.stanford.edu/~knuth/sgb.html

The data file can be found at:

- https://github.com/networkx/networkx/blob/main/examples/graph/words_dat.txt.gz
    N)ascii_lowercasec                    sf   t jdd}dd tD fdd  fddtD }| |D ]\}}||| qL|S )	Nwords)namec                 S   s   i | ]}|t |qS  )	lowercaseindex).0cr   r   x/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/examples/graph/plot_words.py
<dictcomp>   s      z"generate_graph.<locals>.<dictcomp>c                 3   sj   t t| D ]X}| d| | | | |d d    }}} | }t|d d  D ]}|| | V  qPqd S )Nr      )rangelenr   )wordileftr	   rightjcc)lookupr   r
   edit_distance_one   s
    (z)generate_graph.<locals>.edit_distance_onec                 3   s,   | ]$} |D ]}|kr||fV  qqd S )Nr   )r   r   cand)r   r   r   r
   	<genexpr>%   s
   
 z!generate_graph.<locals>.<genexpr>)nxZGraphr   sortedadd_nodes_fromZadd_edge)r   GZcandgenr   r   r   )r   r   r   r
   generate_graph   s    
r   c                  C   sV   t dd} t }|  D ]2}| }|dr2qt|dd }|| qt|S )z:Return the words example graph from the Stanford GraphBasezwords_dat.txt.gzr*r      )	gzipopenset	readlinesdecode
startswithstraddr   )fhr   linewr   r   r
   words_graph1   s    
r,   z?Loaded words_dat.txt containing 5757 five-letter English words.z5Two words are connected if they differ in one letter.z connected components))Zchaosorder)Znodesgraph)poundZmarkszShortest path between z and z isNonered)colorbluer2   i  g333333?)Z	node_sizealphaZ
node_colorbold)Zfont_weight)#__doc__r!   stringr   r   Zmatplotlib.pyplotZpyplotZpltZnetworkxr   r   r,   r   printZnumber_connected_componentssourcetargetZshortest_pathnZNetworkXNoPathlistZnode_boundaryboundaryr   ZsubgraphHZget_node_attributescolorsvaluesoptionsZkamada_kawai_layoutposZdrawZdraw_networkx_labelsshowr   r   r   r
   <module>   s:   
