B
    ฒ๔`A  ใ               @   sH  d Z ddlZddlmZ ddlmZ G dd dejZ	ej
dddd	Ze	e eกZe ก Ze	e eกZe ก Ze	e eกZeefeefeefgZxReD ]J\ZZd
d e eกD Zdd e eกD ZxeD ]ZeeksาtqาW qW xVeD ]N\ZZdd e eกD Zdd e eกD ZxeD ]Zeeks&tq&W q๐W x๎eD ]ๆ\ZZee ก d Zee ก dd Ze  eกe  eกkste!dd e  ก D e!dd e  ก D ksภte!dd e  ก D e!dd ej ddD ks๖te!dd e  eกD e!dd e  eกD ksHtqHW e "eก e #ก  dS )a  
=========
Antigraph
=========

Complement graph class for small footprint when working on dense graphs.

This class allows you to add the edges that *do not exist* in the dense
graph. However, when applying algorithms to this complement graph data
structure, it behaves as if it were the dense version. So it can be used
directly in several NetworkX algorithms.

This subclass has only been tested for k-core, connected_components,
and biconnected_components algorithms but might also work for other
algorithms.

้    N)ฺNetworkXErrorc               @   sF   e Zd ZdZddiZdd ZeZdd Zdd	 ZdddZ	dd Z
d
S )ฺ	AntiGraphaฅ  
    Class for complement graphs.

    The main goal is to be able to work with big and dense graphs with
    a low memory footprint.

    In this class you add the edges that *do not exist* in the dense graph,
    the report methods of the class return the neighbors, the edges and
    the degree as if it was the dense graph. Thus it's possible to use
    an instance of this class with some of NetworkX functions.
    ฺweight้   c             C   s   | j S )N)ฺall_edge_dict)ฺselfฉ r   ๚o/home/dcms/DCMS/lib/python3.7/site-packages/../../../share/doc/networkx-2.5/examples/subclass/plot_antigraph.pyฺsingle_edge_dict&   s    zAntiGraph.single_edge_dictc                s,    fddt  jt  j|  |h D S )a  Return a dict of neighbors of node n in the dense graph.

        Parameters
        ----------
        n : node
           A node in the graph.

        Returns
        -------
        adj_dict : dictionary
           The adjacency dictionary for nodes connected to n.

        c                s   i | ]} j |qS r   )r   )ฺ.0ฺnode)r   r   r	   ๚
<dictcomp>9   s   z)AntiGraph.__getitem__.<locals>.<dictcomp>)ฺsetฺadj)r   ฺnr   )r   r	   ฺ__getitem__+   s    
zAntiGraph.__getitem__c          
   C   s\   y"t t| jt| j|  |h S  tk
rV } ztd| d|W dd}~X Y nX dS )zXReturn an iterator over all neighbors of node n in the
           dense graph.

        z	The node z is not in the graph.N)ฺiterr   r   ฺKeyErrorr   )r   r   ฺer   r   r	   ฺ	neighbors=   s    "zAntiGraph.neighborsNc                s   |dkr  fdd   ก D }nH| krPt   ก t j|  |h }t|S  fdd  |กD }dkr~dd |D S fdd|D S dS )a๕  Return an iterator for (node, degree) in the dense graph.

        The node degree is the number of edges adjacent to the node.

        Parameters
        ----------
        nbunch : iterable container, optional (default=all nodes)
            A container of nodes.  The container will be iterated
            through once.

        weight : string or None, optional (default=None)
           The edge attribute that holds the numerical value used
           as a weight.  If None, then each edge has weight 1.
           The degree is the sum of the edge weights adjacent to the node.

        Returns
        -------
        nd_iter : iterator
            The iterator returns two-tuples of (node, degree).

        See Also
        --------
        degree

        Examples
        --------
        >>> G = nx.path_graph(4)  # or DiGraph, MultiGraph, MultiDiGraph, etc
        >>> list(G.degree(0))  # node 0 with degree 1
        [(0, 1)]
        >>> list(G.degree([0, 1]))
        [(0, 1), (1, 2)]

        Nc             3   s>   | ]6}| fd dt  jt  j|  |h D fV  qdS )c                s   i | ]} j |qS r   )r   )r   ฺv)r   r   r	   r   m   s   z.AntiGraph.degree.<locals>.<genexpr>.<dictcomp>N)r   r   )r   r   )r   r   r	   ๚	<genexpr>k   s   z#AntiGraph.degree.<locals>.<genexpr>c             3   s@   | ]8}| fd dt   ก t  j|  |h D fV  qdS )c                s   i | ]} j |qS r   )r   )r   r   )r   r   r	   r   {   s   z.AntiGraph.degree.<locals>.<genexpr>.<dictcomp>N)r   ฺnodesr   )r   r   )r   r   r	   r   y   s   c             s   s   | ]\}}|t |fV  qd S )N)ฺlen)r   r   ฺnbrsr   r   r	   r      s    c             3   s.   | ]&\} |t  fd d D fV  qdS )c             3   s   | ]} |   d กV  qdS )r   N)ฺget)r   Znbr)r   r   r   r	   r      s    z-AntiGraph.degree.<locals>.<genexpr>.<genexpr>N)ฺsum)r   r   )r   )r   r	   r      s   )r   r   r   r   Znbunch_iter)r   Znbunchr   Z
nodes_nbrsr   r   )r   r   r	   ฺdegreeG   s    "
 

zAntiGraph.degreec             c   s8   x2| j D ](}|t| j t| j |  |h fV  qW dS )az  Return an iterator of (node, adjacency set) tuples for all nodes
           in the dense graph.

        This is the fastest way to look at every edge.
        For directed graphs, only outgoing adjacencies are included.

        Returns
        -------
        adj_iter : iterator
           An iterator of (node, adjacency set) for all nodes in
           the graph.

        N)r   r   )r   r   r   r   r	   ฺadjacency_iter   s    zAntiGraph.adjacency_iter)NN)ฺ__name__ฺ
__module__ฺ__qualname__ฺ__doc__r   r
   Zedge_attr_dict_factoryr   r   r   r   r   r   r   r	   r      s   

Er   ้   g้?้*   )ฺseedc             C   s   g | ]}t |qS r   )r   )r   ฺcr   r   r	   ๚
<listcomp>ช   s    r'   c             C   s   g | ]}t |qS r   )r   )r   r&   r   r   r	   r'   ซ   s    c             C   s   g | ]}t |qS r   )r   )r   r&   r   r   r	   r'   ฐ   s    c             C   s   g | ]}t |qS r   )r   )r   r&   r   r   r	   r'   ฑ   s    r   ้   c             c   s   | ]\}}|V  qd S )Nr   )r   r   ฺdr   r   r	   r   น   s    r   c             c   s   | ]\}}|V  qd S )Nr   )r   r   r)   r   r   r	   r   ป   s    r   )r   c             c   s   | ]\}}|V  qd S )Nr   )r   r   r)   r   r   r	   r   ผ   s    )$r"   ZnetworkxZnxZnetworkx.exceptionr   Zmatplotlib.pyplotZpyplotZpltZGraphr   Zgnp_random_graphZGnpZ
complementZAnpZdavis_southern_women_graphZGdZAdZkarate_club_graphZGkZAkฺpairsฺGฺAZconnected_componentsฺgcฺacฺcompฺAssertionErrorZbiconnected_componentsฺlistr   r   r   r   Zdrawฺshowr   r   r   r	   ฺ<module>   s>    

26<
