ó žÃÒYc@sØdZddlmZddlZddlmZddddd gZd „Zd „Z d „Z d „Z d„Z d„Z d„Zd„Zd„Zd„Zd„Zd„Zedƒedƒd„ƒƒZdS(s Graph products. iÿÿÿÿ(tproductN(tnot_implemented_forttensor_producttcartesian_producttlexicographic_producttstrong_producttpowercs-t‡‡fd†tˆƒtˆƒBDƒƒS(Nc3s3|])}|ˆj|ƒˆj|ƒffVqdS(N(tget(t.0tk(td1td2(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pys s(tdicttset(R R ((R R s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt _dict_productsccsLxEt||ƒD]4\}}||ft|j||j|ƒfVqWdS(N(RRtnodes(tGtHtutv((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt _node_productsc csc|jƒ rŒ|jƒ rŒxo|jdtƒD]X\}}}xF|jdtƒD]2\}}}||f||ft||ƒfVqOWq-Wn|jƒ r#|jƒr#x{|jdtƒD]d\}}}xR|jdtdtƒD]8\}}}}||f||f|t||ƒfVqàWq¸Wn|jƒrº|jƒ rºx{|jdtdtƒD]^\}}}}xI|jdtƒD]5\}}}||f||f|t||ƒfVqzWqUWn|jƒr_|jƒr_xŠ|jdtdtƒD]m\}}} }xX|jdtdtƒD]>\}}}}||f||f| |ft||ƒfVqWqëWndS(Ntdatatkeys(t is_multigraphtedgestTrueR( RRRRtctxtytdR tj((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt_directed_edges_cross_edges#s ""."+1+"1++c csc|jƒ rŒ|jƒ rŒxo|jdtƒD]X\}}}xF|jdtƒD]2\}}}||f||ft||ƒfVqOWq-Wn|jƒ r#|jƒr#x{|jdtƒD]d\}}}xR|jdtdtƒD]8\}}}}||f||f|t||ƒfVqàWq¸Wn|jƒrº|jƒ rºx{|jdtdtƒD]^\}}}}xI|jdtƒD]5\}}}||f||f|t||ƒfVqzWqUWn|jƒr_|jƒr_xŠ|jdtdtƒD]m\}}} }xX|jdtdtƒD]>\}}}}||f||f| |ft||ƒfVqWqëWndS(NRR(RRRR( RRRRRRRRR R((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt_undirected_edges_cross_edges6s ""."+1+"1++ccsí|jƒrlxÚ|jdtdtƒD]@\}}}}x+|D]#}||f||f||fVq>Wq%Wn}xz|jdtƒD]f\}}}xT|D]L}|jƒrÇ||f||fd|fVq•||f||f|fVq•WqWdS(NRR(RRRtNone(RRRRR RR((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt_edges_cross_nodesIs + ("   ccsí|jƒrlxÚ|D]R}xI|jdtdtƒD]/\}}}}||f||f||fVq2WqWn}xz|D]r}xi|jdtƒD]U\}}}|jƒrÇ||f||fd|fVqŒ||f||f|fVqŒWqsWdS(NRR(RRRR!(RRRRRR R((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt_nodes_cross_edgesWs  +( "  ccs|jƒr}xü|jdtdtƒD]Q\}}}}x<|D]4}x+|D]#}||f||f||fVqKWq>Wq%WnŽx‹|jdtƒD]w\}}}xe|D]]}xT|D]L}|jƒrå||f||fd|fVq³||f||f|fVq³Wq¦WqWdS(NRR(RRRR!(RRRRR RRR((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt_edges_cross_nodes_and_nodeses +  ,"    cCs|jƒ|jƒks-tjddƒ‚n|jƒsE|jƒrTtjƒ}n tjƒ}|jƒr{|jƒ}n|S(Ns G and H must be both directed orsboth undirected(t is_directedtnxt NetworkXErrorRt MultiGraphtGrapht to_directed(RRtGH((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyt_init_product_graphus    cCsƒt||ƒ}|jt||ƒƒ|jt||ƒƒ|jƒs`|jt||ƒƒnd|jd|jd|_|S(sûReturn the tensor product of G and H. The tensor product $P$ of the graphs $G$ and $H$ has a node set that is the tensor product of the node sets, $V(P)=V(G) \times V(H)$. $P$ has an edge $((u,v), (x,y))$ if and only if $(u,x)$ is an edge in $G$ and $(v,y)$ is an edge in $H$. Tensor product is sometimes also referred to as the categorical product, direct product, cardinal product or conjunction. Parameters ---------- G, H: graphs Networkx graphs. Returns ------- P: NetworkX graph The tensor product of G and H. P will be a multi-graph if either G or H is a multi-graph, will be a directed if G and H are directed, and undirected if G and H are undirected. Raises ------ NetworkXError If G and H are not both directed or both undirected. Notes ----- Node attributes in P are two-tuple of the G and H node attributes. Missing attributes are assigned None. Examples -------- >>> G = nx.Graph() >>> H = nx.Graph() >>> G.add_node(0, a1=True) >>> H.add_node('a', a2='Spam') >>> P = nx.tensor_product(G, H) >>> list(P) [(0, 'a')] Edge attributes and edge keys (for multigraphs) are also copied to the new product graph sTensor product(t,t)(R,tadd_nodes_fromRtadd_edges_fromRR%R tname(RRR+((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyR‚s/ cCs¡|jƒ|jƒks-tjddƒ‚nt||ƒ}|jt||ƒƒ|jt||ƒƒ|jt||ƒƒd|j d|j d|_ |S(sØReturn the Cartesian product of G and H. The Cartesian product $P$ of the graphs $G$ and $H$ has a node set that is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$. $P$ has an edge $((u,v),(x,y))$ if and only if either $u$ is equal to $x$ and both $v$ and $y$ are adjacent in $H$ or if $v$ is equal to $y$ and both $u$ and $x$ are adjacent in $G$. Parameters ---------- G, H: graphs Networkx graphs. Returns ------- P: NetworkX graph The Cartesian product of G and H. P will be a multi-graph if either G or H is a multi-graph. Will be a directed if G and H are directed, and undirected if G and H are undirected. Raises ------ NetworkXError If G and H are not both directed or both undirected. Notes ----- Node attributes in P are two-tuple of the G and H node attributes. Missing attributes are assigned None. Examples -------- >>> G = nx.Graph() >>> H = nx.Graph() >>> G.add_node(0, a1=True) >>> H.add_node('a', a2='Spam') >>> P = nx.cartesian_product(G, H) >>> list(P) [(0, 'a')] Edge attributes and edge keys (for multigraphs) are also copied to the new product graph s G and H must be both directed orsboth undirectedsCartesian product(R-R.( R%R&R'R,R/RR0R"R#R1(RRR+((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyRºs,  cCstt||ƒ}|jt||ƒƒ|jt||ƒƒ|jt||ƒƒd|jd|jd|_|S(sžReturn the lexicographic product of G and H. The lexicographical product $P$ of the graphs $G$ and $H$ has a node set that is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$. $P$ has an edge $((u,v), (x,y))$ if and only if $(u,v)$ is an edge in $G$ or $u==v$ and $(x,y)$ is an edge in $H$. Parameters ---------- G, H: graphs Networkx graphs. Returns ------- P: NetworkX graph The Cartesian product of G and H. P will be a multi-graph if either G or H is a multi-graph. Will be a directed if G and H are directed, and undirected if G and H are undirected. Raises ------ NetworkXError If G and H are not both directed or both undirected. Notes ----- Node attributes in P are two-tuple of the G and H node attributes. Missing attributes are assigned None. Examples -------- >>> G = nx.Graph() >>> H = nx.Graph() >>> G.add_node(0, a1=True) >>> H.add_node('a', a2='Spam') >>> P = nx.lexicographic_product(G, H) >>> list(P) [(0, 'a')] Edge attributes and edge keys (for multigraphs) are also copied to the new product graph sLexicographic product(R-R.(R,R/RR0R$R#R1(RRR+((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyRñs +cCs¯t||ƒ}|jt||ƒƒ|jt||ƒƒ|jt||ƒƒ|jt||ƒƒ|jƒsŒ|jt||ƒƒnd|j d|j d|_ |S(s×Return the strong product of G and H. The strong product $P$ of the graphs $G$ and $H$ has a node set that is the Cartesian product of the node sets, $V(P)=V(G) \times V(H)$. $P$ has an edge $((u,v), (x,y))$ if and only if $u==v$ and $(x,y)$ is an edge in $H$, or $x==y$ and $(u,v)$ is an edge in $G$, or $(u,v)$ is an edge in $G$ and $(x,y)$ is an edge in $H$. Parameters ---------- G, H: graphs Networkx graphs. Returns ------- P: NetworkX graph The Cartesian product of G and H. P will be a multi-graph if either G or H is a multi-graph. Will be a directed if G and H are directed, and undirected if G and H are undirected. Raises ------ NetworkXError If G and H are not both directed or both undirected. Notes ----- Node attributes in P are two-tuple of the G and H node attributes. Missing attributes are assigned None. Examples -------- >>> G = nx.Graph() >>> H = nx.Graph() >>> G.add_node(0, a1=True) >>> H.add_node('a', a2='Spam') >>> P = nx.strong_product(G, H) >>> list(P) [(0, 'a')] Edge attributes and edge keys (for multigraphs) are also copied to the new product graph sStrong product(R-R.( R,R/RR0R#R"RR%R R1(RRR+((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyR&s- tdirectedt multigraphcsü|dkrtdƒ‚ntjƒ}|j|ƒxÁ|D]¹‰i}d}|ˆ}x}|rÖ|}i}xJ|D]B}|ˆkr‹qsn||krs|||<|j||ƒqsqsW||krÉPn|d7}qZW|j‡fd†|Dƒƒq;W|S(s0Returns the specified power of a graph. The $k$th power of a simple graph $G$, denoted $G^k$, is a graph on the same set of nodes in which two distinct nodes $u$ and $v$ are adjacent in $G^k$ if and only if the shortest path distance between $u$ and $v$ in $G$ is at most $k$. Parameters ---------- G : graph A NetworkX simple graph object. k : positive integer The power to which to raise the graph `G`. Returns ------- NetworkX simple graph `G` to the power `k`. Raises ------ ValueError If the exponent `k` is not positive. NetworkXNotImplemented If `G` is not a simple graph. Examples -------- The number of edges will never decrease when taking successive powers: >>> G = nx.path_graph(4) >>> list(nx.power(G, 2).edges) [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3)] >>> list(nx.power(G, 3).edges) [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] The `k`th power of a cycle graph on *n* nodes is the complete graph on *n* nodes, if `k` is at least ``n // 2``: >>> G = nx.cycle_graph(5) >>> H = nx.complete_graph(5) >>> nx.is_isomorphic(nx.power(G, 2), H) True >>> G = nx.cycle_graph(8) >>> H = nx.complete_graph(8) >>> nx.is_isomorphic(nx.power(G, 4), H) True References ---------- .. [1] J. A. Bondy, U. S. R. Murty, *Graph Theory*. Springer, 2008. Notes ----- This definition of "power graph" comes from Exercise 3.1.6 of *Graph Theory* by Bondy and Murty [1]_. isk must be a positive integeric3s|]}ˆ|fVqdS(N((Rtnbr(tn(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pys ³s(t ValueErrorR&R)R/tupdateR0(RR Rtseentlevelt nextlevelt thislevelR((R5s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyR^s,@           !(t__doc__t itertoolsRtnetworkxR&tnetworkx.utilsRt__all__RRRR R"R#R$R,RRRRR(((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/operators/product.pyts&         8 7 5 8