Exemple #1
0
def motif_counts(gmm,tau):
    """
    Returns dictionary keyed by graph motifs and values as the number of subgraph isomorphisms 
    for the given motif counted in the base structure of the given GMM object.

    Parameters
    ----------
    gmm : A graph motif model object.

    tau : An integer greater than or equal to 2, which designates the number of nodes in the 
        largest graph in set of graph motifs used in the given model.
        
    Returns
    ----------
    subgraph_counts : A list of tuples with the following construction (index,motif,count), where 
        "count" is the number of subgraph isomorphisms for the given motif counted in the base 
        structure of the given GMM object and index is the motif index.
    """
    base=gmm.get_base()
    base_direction=base.is_directed()   # Check if GMM base is directed, motifs must match
    motif_counts=get_motifs(tau,base_direction)
    # Performing the counting of subgraph isomorphism for every motif given the base structure
    for motif in motif_counts:
        index=motif[0]
        # Calculate subgraph ismorphisms
        if base_direction:
            GM=nx.DiGraphMatcher(base,motif[1])
        else:
            GM=nx.GraphMatcher(base,motif[1])
        # Count subgraph isomorphisms and update
        count=0
        for i in GM.subgraph_isomorphisms_iter():
            count+=1
        motif_counts[index]=(index,motif_counts[index][1],count)
    return motif_counts
Exemple #2
0
def is_isomorphic(G1, G2, weighted=False, rtol=1e-6, atol=1e-9):
    """Returns True if the graphs G1 and G2 are isomorphic and False otherwise.

    Parameters
    ----------
    G1, G2: NetworkX graph instances
       The two graphs G1 and G2 must be the same type.
       
    weighted: bool, optional
       Optionally check isomorphism for weighted graphs.
       G1 and G2 must be valid weighted graphs.

    rtol: float, optional
        The relative error tolerance when checking weighted edges

    atol: float, optional
        The absolute error tolerance when checking weighted edges
    
    Notes
    -----
    Uses the vf2 algorithm.
    Works for Graph, DiGraph, MultiGraph, and MultiDiGraph

    See Also
    --------
    isomorphvf2()

    """
    gm = None
    if weighted == True:
        #        assert(G1.weighted and G2.weighted)
        if not G1.is_directed() and not G1.is_multigraph():
            assert (not G2.is_directed() and not G2.is_multigraph())
            gm = nx.WeightedGraphMatcher(G1, G2, rtol, atol)
        elif not G1.is_directed() and G1.is_multigraph():
            assert (not G2.is_directed() and G2.is_multigraph())
            gm = nx.WeightedMultiGraphMatcher(G1, G2, rtol, atol)
        elif G1.is_directed() and not G1.is_multigraph():
            assert (G2.is_directed() and not G2.is_multigraph())
            gm = nx.WeightedDiGraphMatcher(G1, G2, rtol, atol)
        else:
            assert (G2.is_directed() and G2.is_multigraph())
            gm = nx.WeightedMultiDiGraphMatcher(G1, G2, rtol, atol)
    else:
        if G1.is_directed() and G2.is_directed():
            gm = nx.DiGraphMatcher(G1, G2)
        elif not (G1.is_directed() and G2.is_directed()):
            gm = nx.GraphMatcher(G1, G2)
    if gm == None:
        # Graphs are of mixed type. We could just return False,
        # but then there is the case of completely disconnected graphs...
        # which could be isomorphic.
        raise NetworkXError("Graphs G1 and G2 are not of the same type.")

    return gm.is_isomorphic()
def test_multiple():
    # Verify that we can use the graph matcher multiple times
    edges = [('A', 'B'), ('B', 'A'), ('B', 'C')]
    for g1, g2 in [(nx.Graph(), nx.Graph()), (nx.DiGraph(), nx.DiGraph())]:
        g1.add_edges_from(edges)
        g2.add_edges_from(edges)
        g3 = nx.subgraph(g2, ['A', 'B'])
        if not g1.is_directed():
            gmA = nx.GraphMatcher(g1, g2)
            gmB = nx.GraphMatcher(g1, g3)
        else:
            gmA = nx.DiGraphMatcher(g1, g2)
            gmB = nx.DiGraphMatcher(g1, g3)
        assert_true(gmA.is_isomorphic())
        g2.remove_node('C')
        assert_true(gmA.subgraph_is_isomorphic())
        assert_true(gmB.subgraph_is_isomorphic())
        for m in [gmB.mapping, gmB.mapping]:
            assert_true(m['A'] == 'A')
            assert_true(m['B'] == 'B')
            assert_true('C' not in m)
Exemple #4
0
def is_isomorphic(G1,G2):
    """Returns True if the graphs G1 and G2 are isomorphic and False otherwise.

    Uses the vf2 algorithm - see networkx.isomorphvf2
        
    """
    if G1.directed and G2.directed:
        return networkx.DiGraphMatcher(G1,G2).is_isomorphic()
    elif not (G1.directed and G2.directed):
        return networkx.GraphMatcher(G1,G2).is_isomorphic()
    else:
            # Graphs are of mixed type. We could just return False, 
            # but then there is the case of completely disconnected graphs...
            # which could be isomorphic.
        raise NetworkXError, "Both graphs were not directed or both graphs were not undirected."