
    Gdv
                     ,   d Z ddlmZ ddlmZ ddlZddlZ	ddl
mZ ddlmZ d Z ej        d          Ze                     e ej        e                              Z ej        e          Z ee          Z ej        eed	          Z ej        ed
d          Z ej        d            ej        eedd           d e                                D             Zd e                    d          D             Z i Z!eD ]FZ" e	j#        d ej        e"         d         D                       Z$e$%                    d          e!e"<   G ej        d            ej        ee!ee d            ej&        d            ej'                     dS )a}  
==========
Blockmodel
==========

Example of creating a block model using the quotient_graph function in NX.  Data
used is the Hartford, CT drug users network::

    @article{weeks2002social,
      title={Social networks of drug users in high-risk sites: Finding the connections},
      url = {https://doi.org/10.1023/A:1015457400897},
      doi = {10.1023/A:1015457400897},
      author={Weeks, Margaret R and Clair, Scott and Borgatti, Stephen P and Radda, Kim and Schensul, Jean J},
      journal={{AIDS and Behavior}},
      volume={6},
      number={2},
      pages={193--206},
      year={2002},
      publisher={Springer}
    }

    )defaultdictN)	hierarchy)distancec           	         t          j        |           }t          j        t	          |           t	          |           f          }|D ]*\  }}|                                D ]\  }}|||         |<   +t          j        |          }t          j	        |          }t          t          j        |d                    }	t          t                    }
t          t          t          t	          |                               |	          D ] \  }}|
|                             |           !t          |
                                          S )z<Creates hierarchical cluster of graph G from distance matrixgffffff?)t)nxall_pairs_shortest_path_lengthnpzeroslenitemsr   
squareformr   completelistfclusterr   ziprangeappendvalues)Gpath_length	distancesupvdYZ
membership	partitionns               =share/doc/networkx-3.1/examples/algorithms/plot_blockmodel.py	create_hcr#   !   s&   3A66K#a&&#a&&)**I    1GGII 	  	 DAqIaLOO	  	I&&A1Ai(d33344JD!!IDs1vv''44  1!A	  ""###    zhartford_drug.edgelistT)relabeld   S   )
iterationsseed   F
   )with_labels	node_sizec                 D    g | ]}t           j        |         d          dz  S )nnodesr+   )BMnodes).0xs     r"   
<listcomp>r4   E   s(    <<<ARXa["R'<<<r$   c                 ,    g | ]\  }}}d |d         z  S )   weight )r2   r   r   r   s       r"   r4   r4   F   s&    EEEIQ1q1X;EEEr$   )datac                 (    g | ]}t           |         S r8   )pos)r2   r   s     r"   r4   r4   J   s    888a3q6888r$   graph)axis   )r-   widthr,   off)(__doc__collectionsr   matplotlib.pyplotpyplotpltnetworkxr   numpyr
   scipy.clusterr   scipy.spatialr   r#   read_edgelistr   subgraphnextconnected_componentsHconvert_node_labels_to_integers
partitionsquotient_graphr0   spring_layoutr;   subplotdrawr1   r-   edges
edge_widthposBMr!   arrayxymeanr=   showr8   r$   r"   <module>r\      s#   . $ # # # # #               # # # # # # " " " " " "$ $ $& B-.. JJtt+B+A..//00&B&q))Yq\\
Rq*d333 bqSr222 C    3ER 0 0 0 0 =<<<<	EEt1D1DEEE

	  A	8828A;w#7888	9	9BwwAwE!HH C    EYje L L L L  




r$   