a
    œç1bµ)  ã                   @   sT   d Z ddlZg d¢Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	ddd„Z
dS )zA
Operations on graphs including union, intersection, difference.
é    N)ÚunionÚcomposeÚdisjoint_unionÚintersectionÚ
differenceÚsymmetric_differenceÚ	full_join©NNc                 C   s0   |dur ddl }|jdtdd t | |g|¡S )a~  Return the union of graphs G and H.

    Graphs G and H must be disjoint after the renaming takes place,
    otherwise an exception is raised.

    Parameters
    ----------
    G,H : graph
       A NetworkX graph

    rename : tuple , default=(None, None)
       Node names of G and H can be changed by specifying the tuple
       rename=('G-','H-') (for example).  Node "u" in G is then renamed
       "G-u" and "v" in H is renamed "H-v".

    name : string
       Specify the name for the union graph

       .. deprecated:: 2.7
           This is deprecated and will be removed in version v3.0.

    Returns
    -------
    U : A union graph with the same type as G.

    Notes
    -----
    To force a disjoint union with node relabeling, use
    disjoint_union(G,H) or convert_node_labels_to integers().

    Graph, edge, and node attributes are propagated from G and H
    to the union graph.  If a graph attribute is present in both
    G and H the value from H is used.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
    >>> H = nx.Graph([(0, 1), (0, 3), (1, 3), (1, 2)])
    >>> U = nx.union(G, H, rename=("G", "H"))
    >>> U.nodes
    NodeView(('G0', 'G1', 'G2', 'H0', 'H1', 'H3', 'H2'))
    >>> U.edges
    EdgeView([('G0', 'G1'), ('G0', 'G2'), ('G1', 'G2'), ('H0', 'H1'), ('H0', 'H3'), ('H1', 'H3'), ('H1', 'H2')])

    See Also
    --------
    disjoint_union
    Nr   z?name parameter is deprecated and will be removed in version 3.0é   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningÚnxZ	union_all)ÚGÚHÚrenameÚnamer   © r   úClib/python3.9/site-packages/networkx/algorithms/operators/binary.pyr      s    1ýr   c                 C   s   t  | |g¡S )aI  Return the disjoint union of graphs G and H.

    This algorithm forces distinct integer node labels.

    Parameters
    ----------
    G,H : graph
       A NetworkX graph

    Returns
    -------
    U : A union graph with the same type as G.

    Notes
    -----
    A new graph is created, of the same class as G.  It is recommended
    that G and H be either both directed or both undirected.

    The nodes of G are relabeled 0 to len(G)-1, and the nodes of H are
    relabeled len(G) to len(G)+len(H)-1.

    Graph, edge, and node attributes are propagated from G and H
    to the union graph.  If a graph attribute is present in both
    G and H the value from H is used.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
    >>> H = nx.Graph([(0, 3), (1, 2), (2, 3)])
    >>> G.nodes[0]["key1"] = 5
    >>> H.nodes[0]["key2"] = 10
    >>> U = nx.disjoint_union(G, H)
    >>> U.nodes(data=True)
    NodeDataView({0: {'key1': 5}, 1: {}, 2: {}, 3: {'key2': 10}, 4: {}, 5: {}, 6: {}})
    >>> U.edges
    EdgeView([(0, 1), (0, 2), (1, 2), (3, 4), (4, 6), (5, 6)])
    )r   Zdisjoint_union_all©r   r   r   r   r   r   N   s    &r   c                 C   s   t  | |g¡S )a^  Returns a new graph that contains only the nodes and the edges that exist in
    both G and H.

    Parameters
    ----------
    G,H : graph
       A NetworkX graph. G and H can have different node sets but must be both graphs or both multigraphs.

    Raises
    ------
    NetworkXError
        If one is a MultiGraph and the other one is a graph.

    Returns
    -------
    GH : A new graph with the same type as G.

    Notes
    -----
    Attributes from the graph, nodes, and edges are not copied to the new
    graph.  If you want a new graph of the intersection of G and H
    with the attributes (including edge data) from G use remove_nodes_from()
    as follows

    >>> G = nx.path_graph(3)
    >>> H = nx.path_graph(5)
    >>> R = G.copy()
    >>> R.remove_nodes_from(n for n in G if n not in H)
    >>> R.remove_edges_from(e for e in G.edges if e not in H.edges)

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2)])
    >>> H = nx.Graph([(0, 3), (1, 2), (2, 3)])
    >>> R = nx.intersection(G, H)
    >>> R.nodes
    NodeView((0, 1, 2))
    >>> R.edges
    EdgeView([(1, 2)])
    )r   Zintersection_allr   r   r   r   r   w   s    )r   c                 C   s~   |   ¡ |  ¡ kst d¡‚t | ¡}t| ƒt|ƒkr>t d¡‚|   ¡ rT| jdd}n|  ¡ }|D ]}|j|Ž s`|j|Ž  q`|S )a®  Returns a new graph that contains the edges that exist in G but not in H.

    The node sets of H and G must be the same.

    Parameters
    ----------
    G,H : graph
       A NetworkX graph. G and H must have the same node sets.

    Returns
    -------
    D : A new graph with the same type as G.

    Notes
    -----
    Attributes from the graph, nodes, and edges are not copied to the new
    graph.  If you want a new graph of the difference of G and H with
    the attributes (including edge data) from G use remove_nodes_from()
    as follows:

    >>> G = nx.path_graph(3)
    >>> H = nx.path_graph(5)
    >>> R = G.copy()
    >>> R.remove_nodes_from(n for n in G if n in H)

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3)])
    >>> H = nx.Graph([(0, 1), (1, 2), (0, 3)])
    >>> R = nx.difference(G, H)
    >>> R.nodes
    NodeView((0, 1, 2, 3))
    >>> R.edges
    EdgeView([(0, 2), (1, 3)])
    ú+G and H must both be graphs or multigraphs.úNode sets of graphs not equalT©Úkeys)Úis_multigraphr   ÚNetworkXErrorÚcreate_empty_copyÚsetÚedgesÚhas_edgeÚadd_edge)r   r   ÚRr   Úer   r   r   r   £   s    %



r   c                 C   sÞ   |   ¡ |  ¡ kst d¡‚t | ¡}t| ƒt|ƒkr>t d¡‚t| ƒ}t|ƒ}| |¡}| |¡ |   ¡ rx| jdd}n|  ¡ }|D ]}|j|Ž s„|j	|Ž  q„|  ¡ r´|jdd}n| ¡ }|D ]}| j|Ž sÀ|j	|Ž  qÀ|S )a§  Returns new graph with edges that exist in either G or H but not both.

    The node sets of H and G must be the same.

    Parameters
    ----------
    G,H : graph
       A NetworkX graph.  G and H must have the same node sets.

    Returns
    -------
    D : A new graph with the same type as G.

    Notes
    -----
    Attributes from the graph, nodes, and edges are not copied to the new
    graph.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2), (1, 2), (1, 3)])
    >>> H = nx.Graph([(0, 1), (1, 2), (0, 3)])
    >>> R = nx.symmetric_difference(G, H)
    >>> R.nodes
    NodeView((0, 1, 2, 3))
    >>> R.edges
    EdgeView([(0, 2), (0, 3), (1, 3)])
    r   r   Tr   )
r   r   r   r   r   r   Zadd_nodes_fromr   r    r!   )r   r   r"   ZgnodesZhnodesZnodesr   r#   r   r   r   r   Ù   s,    






r   c                 C   s   t  | |g¡S )a¨  Returns a new graph of G composed with H.

    Composition is the simple union of the node sets and edge sets.
    The node sets of G and H do not need to be disjoint.

    Parameters
    ----------
    G, H : graph
       A NetworkX graph

    Returns
    -------
    C: A new graph  with the same type as G

    Notes
    -----
    It is recommended that G and H be either both directed or both undirected.
    Attributes from H take precedent over attributes from G.

    For MultiGraphs, the edges are identified by incident nodes AND edge-key.
    This can cause surprises (i.e., edge `(1, 2)` may or may not be the same
    in two graphs) if you use MultiGraph without keeping track of edge keys.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2)])
    >>> H = nx.Graph([(0, 1), (1, 2)])
    >>> R = nx.compose(G, H)
    >>> R.nodes
    NodeView((0, 1, 2))
    >>> R.edges
    EdgeView([(0, 1), (0, 2), (1, 2)])
    )r   Zcompose_allr   r   r   r   r     s    "r   c                 C   s|   t | ||ƒ}dd„ }|| |d ƒ} |||d ƒ}| D ]}|D ]}| ||¡ q<q4| ¡ rx|D ]}| D ]}| ||¡ qdq\|S )aZ  Returns the full join of graphs G and H.

    Full join is the union of G and H in which all edges between
    G and H are added.
    The node sets of G and H must be disjoint,
    otherwise an exception is raised.

    Parameters
    ----------
    G, H : graph
       A NetworkX graph

    rename : tuple , default=(None, None)
       Node names of G and H can be changed by specifying the tuple
       rename=('G-','H-') (for example).  Node "u" in G is then renamed
       "G-u" and "v" in H is renamed "H-v".

    Returns
    -------
    U : The full join graph with the same type as G.

    Notes
    -----
    It is recommended that G and H be either both directed or both undirected.

    If G is directed, then edges from G to H are added as well as from H to G.

    Note that full_join() does not produce parallel edges for MultiGraphs.

    The full join operation of graphs G and H is the same as getting
    their complement, performing a disjoint union, and finally getting
    the complement of the resulting graph.

    Graph, edge, and node attributes are propagated from G and H
    to the union graph.  If a graph attribute is present in both
    G and H the value from H is used.

    Examples
    --------
    >>> G = nx.Graph([(0, 1), (0, 2)])
    >>> H = nx.Graph([(3, 4)])
    >>> R = nx.full_join(G, H, rename=("G", "H"))
    >>> R.nodes
    NodeView(('G0', 'G1', 'G2', 'H3', 'H4'))
    >>> R.edges
    EdgeView([('G0', 'G1'), ('G0', 'G2'), ('G0', 'H3'), ('G0', 'H4'), ('G1', 'H3'), ('G1', 'H4'), ('G2', 'H3'), ('G2', 'H4'), ('H3', 'H4')])

    See Also
    --------
    union
    disjoint_union
    c                    s$   ˆ d u r| S ‡ fdd„}t  | |¡S )Nc                    s$   t | tƒrˆ |  }nˆ t| ƒ }|S )N)Ú
isinstanceÚstrÚrepr)Úxr   ©Úprefixr   r   Úlabelw  s    

z,full_join.<locals>.add_prefix.<locals>.label)r   Zrelabel_nodes)Zgraphr)   r*   r   r(   r   Ú
add_prefixs  s    zfull_join.<locals>.add_prefixr   é   )r   r!   Zis_directed)r   r   r   r"   r+   ÚiÚjr   r   r   r   <  s    5r   )r	   N)r	   )Ú__doc__Znetworkxr   Ú__all__r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s   
=),6>%