Yc@sdZddlZddlmZddddgZd Zdejfd YZdej fd YZ defd YZ de fd YZ dS(s5 Module to simplify the specification of user-defined equality functions for node and edge attributes during isomorphism checks. During the construction of an isomorphism, the algorithm considers two candidate nodes n1 in G1 and n2 in G2. The graphs G1 and G2 are then compared with respect to properties involving n1 and n2, and if the outcome is good, then the candidate nodes are considered isomorphic. NetworkX provides a simple mechanism for users to extend the comparisons to include node and edge attributes. Node attributes are handled by the node_match keyword. When considering n1 and n2, the algorithm passes their node attribute dictionaries to node_match, and if it returns False, then n1 and n2 cannot be considered to be isomorphic. Edge attributes are handled by the edge_match keyword. When considering n1 and n2, the algorithm must verify that outgoing edges from n1 are commensurate with the outgoing edges for n2. If the graph is directed, then a similar check is also performed for incoming edges. Focusing only on outgoing edges, we consider pairs of nodes (n1, v1) from G1 and (n2, v2) from G2. For graphs and digraphs, there is only one edge between (n1, v1) and only one edge between (n2, v2). Those edge attribute dictionaries are passed to edge_match, and if it returns False, then n1 and n2 cannot be considered isomorphic. For multigraphs and multidigraphs, there can be multiple edges between (n1, v1) and also multiple edges between (n2, v2). Now, there must exist an isomorphism from "all the edges between (n1, v1)" to "all the edges between (n2, v2)". So, all of the edge attribute dictionaries are passed to edge_match, and it must determine if there is an isomorphism between the two sets of edges. iNi(t isomorphvf2t GraphMatchertDiGraphMatchertMultiGraphMatchertMultiDiGraphMatcherc Cs|jdk rB|j|jj||jj|}|sBtSn|jdk r|j}|j}|j }|j}x}||D]n}||kr|||||||stSq||kr||||||||stSqqWnt S(sIReturns True if mapping G1_node to G2_node is semantically feasible. N( t node_matchtNonetG1tnodestG2tFalset edge_matchtG1_adjtG2_adjtcore_1tTrue( tselftG1_nodetG2_nodetnmR R RR tneighbor((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyt_semantic_feasibility-s&&      cBs#eZdZdddZeZRS(s3VF2 isomorphism checker for undirected graphs. cCsJtjj|||||_||_|jj|_|jj|_ dS(sInitialize graph matcher. Parameters ---------- G1, G2: graph The graphs to be tested. node_match: callable A function that returns True iff node n1 in G1 and n2 in G2 should be considered equal during the isomorphism test. The function will be called like:: node_match(G1.nodes[n1], G2.nodes[n2]) That is, the function will receive the node attribute dictionaries of the nodes under consideration. If None, then no attributes are considered when testing for an isomorphism. edge_match: callable A function that returns True iff the edge attribute dictionary for the pair of nodes (u1, v1) in G1 and (u2, v2) in G2 should be considered equal during the isomorphism test. The function will be called like:: edge_match(G1[u1][v1], G2[u2][v2]) That is, the function will receive the edge attribute dictionaries of the edges under consideration. If None, then no attributes are considered when testing for an isomorphism. N( tvf2Rt__init__RR RtadjR R R (RRR RR ((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyRQs   N(t__name__t __module__t__doc__RRRtsemantic_feasibility(((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyRNs)cBs&eZdZdddZdZRS(s1VF2 isomorphism checker for directed graphs. cCsJtjj|||||_||_|jj|_|jj|_ dS(sInitialize graph matcher. Parameters ---------- G1, G2 : graph The graphs to be tested. node_match : callable A function that returns True iff node n1 in G1 and n2 in G2 should be considered equal during the isomorphism test. The function will be called like:: node_match(G1.nodes[n1], G2.nodes[n2]) That is, the function will receive the node attribute dictionaries of the nodes under consideration. If None, then no attributes are considered when testing for an isomorphism. edge_match : callable A function that returns True iff the edge attribute dictionary for the pair of nodes (u1, v1) in G1 and (u2, v2) in G2 should be considered equal during the isomorphism test. The function will be called like:: edge_match(G1[u1][v1], G2[u2][v2]) That is, the function will receive the edge attribute dictionaries of the edges under consideration. If None, then no attributes are considered when testing for an isomorphism. N( RRRRR RRR R R (RRR RR ((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyRs   cCsnt|||}|stS|jj|_|jj|_t|||}|jj|_|jj|_|S(sDReturns True if mapping G1_node to G2_node is semantically feasible.(RR RtpredR R R R(RRRtfeasible((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyRsN(RRRRRR(((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyR}s*cBseZdZRS(s4VF2 isomorphism checker for undirected multigraphs. (RRR(((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyRscBseZdZRS(s2VF2 isomorphism checker for directed multigraphs. (RRR(((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyRs( RtnetworkxtnxtRRt__all__RRRRR(((s/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/isomorphism/vf2userfunc.pyt s   !/B