ó
ú£Õ\c           @   s•   d  Z  d d l Z d d l m Z d j d d d g ƒ Z d d	 d
 d d d g Z d d d „ Z	 d „  Z
 d „  Z d „  Z d „  Z d d „ Z d S(   sA   
Operations on graphs including union, intersection, difference.
iÿÿÿÿN(   t   is_string_likes   
s   Aric Hagberg (hagberg@lanl.gov)s   Pieter Swart (swart@lanl.gov)s   Dan Schult(dschult@colgate.edu)t   uniont   composet   disjoint_uniont   intersectiont
   differencet   symmetric_differencec         C   sŠ  |  j  ƒ  } | d	 k r1 d |  j | j f } n  | | _ d „  } | |  | d ƒ }  | | | d ƒ } t |  ƒ t | ƒ @r” t j d d ƒ ‚ n  |  j ƒ  r» |  j d t d t ƒ } n |  j d t ƒ } | j ƒ  rô | j d t d t ƒ } n | j d t ƒ } | j	 |  ƒ | j
 | ƒ | j	 | ƒ | j
 | ƒ | j j |  j ƒ | j j | j ƒ | j j |  j ƒ | j j | j ƒ | S(
   sØ   Return the union of graphs G and H.

    Graphs G and H must be disjoint, otherwise an exception is raised.

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

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise

    rename : bool , 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

    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.

    See Also
    --------
    disjoint_union
    s   union( %s, %s )c            s/   ˆ  d  k r |  S‡  f d †  } t j |  | ƒ S(   Nc            s-   t  |  ƒ r ˆ  |  } n ˆ  t |  ƒ } | S(   N(   R    t   repr(   t   xt   name(   t   prefix(    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyt   labelB   s    (   t   Nonet   nxt   relabel_nodes(   t   graphR
   R   (    (   R
   s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyt
   add_prefix?   s    i    i   s*   The node sets of G and H are not disjoint.sC   Use appropriate rename=(Gprefix,Hprefix)or use disjoint_union(G,H).t   keyst   dataN(   t	   __class__R   R	   t   setR   t   NetworkXErrort   is_multigrapht
   edges_itert   Truet   add_nodes_fromt   add_edges_fromt   nodet   updateR   (   t   Gt   Ht   renameR	   t   RR   t   G_edgest   H_edges(    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyR      s2    '		
	c         C   s|   t  j |  ƒ } t  j | d t | ƒ ƒ} t | | ƒ } d |  j | j f | _ | j j |  j ƒ | j j | j ƒ | S(   s­   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.
    t   first_labels   disjoint_union( %s, %s )(   R   t   convert_node_labels_to_integerst   lenR   R	   R   R   (   R   R   t   R1t   R2R    (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyR   h   s    c         C   s+  t  j |  ƒ } d |  j | j f | _ t |  ƒ t | ƒ k rR t  j d ƒ ‚ n  |  j ƒ  | j ƒ  k rÊ |  j ƒ  r‹ |  j d t ƒ } n |  j ƒ  } x | D]% } | j	 | Œ  rž | j
 | Œ  qž qž Wn] | j ƒ  rë | j d t ƒ } n | j ƒ  } x- | D]% } |  j	 | Œ  rþ | j
 | Œ  qþ qþ W| S(   sº  Return a new graph that contains only the edges that exist in
    both G and 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
    -------
    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)
    s   Intersection of (%s and %s)s!   Node sets of graphs are not equalR   (   R   t   create_empty_copyR	   R   R   t   number_of_edgesR   R   R   t   has_edget   add_edge(   R   R   R    t   edgest   e(    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyR   ‹   s$    c         C   s³   t  j |  ƒ } d |  j | j f | _ t |  ƒ t | ƒ k rR t  j d ƒ ‚ n  |  j ƒ  rs |  j d t ƒ } n |  j ƒ  } x- | D]% } | j | Œ  s† | j	 | Œ  q† q† W| S(   s²  Return 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
    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)
    s   Difference of (%s and %s)s   Node sets of graphs not equalR   (
   R   R(   R	   R   R   R   R   R   R*   R+   (   R   R   R    R,   R-   (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyR   Á   s    c         C   sD  t  j |  ƒ } d |  j | j f | _ t |  ƒ t | ƒ k rR t  j d ƒ ‚ n  t |  ƒ } t | ƒ } | j | ƒ } | j | ƒ |  j ƒ  r§ |  j d t	 ƒ } n |  j ƒ  } x- | D]% } | j
 | Œ  sº | j | Œ  qº qº W| j ƒ  r| j d t	 ƒ } n | j ƒ  } x- | D]% } |  j
 | Œ  s| j | Œ  qqW| S(   s™  Return 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.
    s#   Symmetric difference of (%s and %s)s   Node sets of graphs not equalR   (   R   R(   R	   R   R   R   R   R   R   R   R*   R+   (   R   R   R    t   gnodest   hnodest   nodesR,   R-   (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyR   ë   s*    c         C   s>  | d k r% d |  j | j f } n  |  j ƒ  } | | _ | j | j ƒ  ƒ | j |  j ƒ  ƒ | j ƒ  rŽ | j | j d t d t ƒ ƒ n | j | j d t ƒ ƒ |  j ƒ  rÕ | j |  j d t d t ƒ ƒ n | j |  j d t ƒ ƒ | j	 j
 |  j	 ƒ | j	 j
 | j	 ƒ | j j
 |  j ƒ | j j
 | j ƒ | S(   s  Return 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 need not be disjoint.

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

    name : string
       Specify name for new 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.
    s   compose( %s, %s )R   R   N(   R   R	   R   R   R0   R   R   R   R   R   R   R   (   R   R   R	   R    (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyR     s"    	""(   NN(   t   __doc__t   networkxR   t   networkx.utilsR    t   joint
   __author__t   __all__R   R   R   R   R   R   R   (    (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/operators/binary.pyt   <module>   s   	V	#	6	*	2