Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / isomorphism / isomorph.py @ 5cef0f13

History | View | Annotate | Download (6.6 KB)

1
"""
2
Graph isomorphism functions.
3
"""
4
import networkx as nx
5
from networkx.exception import NetworkXError
6
__author__ = """\n""".join(['Aric Hagberg (hagberg@lanl.gov)',
7
                            'Pieter Swart (swart@lanl.gov)',
8
                            'Christopher Ellison cellison@cse.ucdavis.edu)'])
9
#    Copyright (C) 2004-2019 by
10
#    Aric Hagberg <hagberg@lanl.gov>
11
#    Dan Schult <dschult@colgate.edu>
12
#    Pieter Swart <swart@lanl.gov>
13
#    All rights reserved.
14
#    BSD license.
15
__all__ = ['could_be_isomorphic',
16
           'fast_could_be_isomorphic',
17
           'faster_could_be_isomorphic',
18
           'is_isomorphic']
19

    
20

    
21
def could_be_isomorphic(G1, G2):
22
    """Returns False if graphs are definitely not isomorphic.
23
    True does NOT guarantee isomorphism.
24

25
    Parameters
26
    ----------
27
    G1, G2 : graphs
28
       The two graphs G1 and G2 must be the same type.
29

30
    Notes
31
    -----
32
    Checks for matching degree, triangle, and number of cliques sequences.
33
    """
34

    
35
    # Check global properties
36
    if G1.order() != G2.order():
37
        return False
38

    
39
    # Check local properties
40
    d1 = G1.degree()
41
    t1 = nx.triangles(G1)
42
    c1 = nx.number_of_cliques(G1)
43
    props1 = [[d, t1[v], c1[v]] for v, d in d1]
44
    props1.sort()
45

    
46
    d2 = G2.degree()
47
    t2 = nx.triangles(G2)
48
    c2 = nx.number_of_cliques(G2)
49
    props2 = [[d, t2[v], c2[v]] for v, d in d2]
50
    props2.sort()
51

    
52
    if props1 != props2:
53
        return False
54

    
55
    # OK...
56
    return True
57

    
58

    
59
graph_could_be_isomorphic = could_be_isomorphic
60

    
61

    
62
def fast_could_be_isomorphic(G1, G2):
63
    """Returns False if graphs are definitely not isomorphic.
64

65
    True does NOT guarantee isomorphism.
66

67
    Parameters
68
    ----------
69
    G1, G2 : graphs
70
       The two graphs G1 and G2 must be the same type.
71

72
    Notes
73
    -----
74
    Checks for matching degree and triangle sequences.
75
    """
76
    # Check global properties
77
    if G1.order() != G2.order():
78
        return False
79

    
80
    # Check local properties
81
    d1 = G1.degree()
82
    t1 = nx.triangles(G1)
83
    props1 = [[d, t1[v]] for v, d in d1]
84
    props1.sort()
85

    
86
    d2 = G2.degree()
87
    t2 = nx.triangles(G2)
88
    props2 = [[d, t2[v]] for v, d in d2]
89
    props2.sort()
90

    
91
    if props1 != props2:
92
        return False
93

    
94
    # OK...
95
    return True
96

    
97

    
98
fast_graph_could_be_isomorphic = fast_could_be_isomorphic
99

    
100

    
101
def faster_could_be_isomorphic(G1, G2):
102
    """Returns False if graphs are definitely not isomorphic.
103

104
    True does NOT guarantee isomorphism.
105

106
    Parameters
107
    ----------
108
    G1, G2 : graphs
109
       The two graphs G1 and G2 must be the same type.
110

111
    Notes
112
    -----
113
    Checks for matching degree sequences.
114
    """
115
    # Check global properties
116
    if G1.order() != G2.order():
117
        return False
118

    
119
    # Check local properties
120
    d1 = sorted(d for n, d in G1.degree())
121
    d2 = sorted(d for n, d in G2.degree())
122

    
123
    if d1 != d2:
124
        return False
125

    
126
    # OK...
127
    return True
128

    
129

    
130
faster_graph_could_be_isomorphic = faster_could_be_isomorphic
131

    
132

    
133
def is_isomorphic(G1, G2, node_match=None, edge_match=None):
134
    """Returns True if the graphs G1 and G2 are isomorphic and False otherwise.
135

136
    Parameters
137
    ----------
138
    G1, G2: graphs
139
        The two graphs G1 and G2 must be the same type.
140

141
    node_match : callable
142
        A function that returns True if node n1 in G1 and n2 in G2 should
143
        be considered equal during the isomorphism test.
144
        If node_match is not specified then node attributes are not considered.
145

146
        The function will be called like
147

148
           node_match(G1.nodes[n1], G2.nodes[n2]).
149

150
        That is, the function will receive the node attribute dictionaries
151
        for n1 and n2 as inputs.
152

153
    edge_match : callable
154
        A function that returns True if the edge attribute dictionary
155
        for the pair of nodes (u1, v1) in G1 and (u2, v2) in G2 should
156
        be considered equal during the isomorphism test.  If edge_match is
157
        not specified then edge attributes are not considered.
158

159
        The function will be called like
160

161
           edge_match(G1[u1][v1], G2[u2][v2]).
162

163
        That is, the function will receive the edge attribute dictionaries
164
        of the edges under consideration.
165

166
    Notes
167
    -----
168
    Uses the vf2 algorithm [1]_.
169

170
    Examples
171
    --------
172
    >>> import networkx.algorithms.isomorphism as iso
173

174
    For digraphs G1 and G2, using 'weight' edge attribute (default: 1)
175

176
    >>> G1 = nx.DiGraph()
177
    >>> G2 = nx.DiGraph()
178
    >>> nx.add_path(G1, [1,2,3,4], weight=1)
179
    >>> nx.add_path(G2, [10,20,30,40], weight=2)
180
    >>> em = iso.numerical_edge_match('weight', 1)
181
    >>> nx.is_isomorphic(G1, G2)  # no weights considered
182
    True
183
    >>> nx.is_isomorphic(G1, G2, edge_match=em) # match weights
184
    False
185

186
    For multidigraphs G1 and G2, using 'fill' node attribute (default: '')
187

188
    >>> G1 = nx.MultiDiGraph()
189
    >>> G2 = nx.MultiDiGraph()
190
    >>> G1.add_nodes_from([1,2,3], fill='red')
191
    >>> G2.add_nodes_from([10,20,30,40], fill='red')
192
    >>> nx.add_path(G1, [1,2,3,4], weight=3, linewidth=2.5)
193
    >>> nx.add_path(G2, [10,20,30,40], weight=3)
194
    >>> nm = iso.categorical_node_match('fill', 'red')
195
    >>> nx.is_isomorphic(G1, G2, node_match=nm)
196
    True
197

198
    For multidigraphs G1 and G2, using 'weight' edge attribute (default: 7)
199

200
    >>> G1.add_edge(1,2, weight=7)
201
    1
202
    >>> G2.add_edge(10,20)
203
    1
204
    >>> em = iso.numerical_multiedge_match('weight', 7, rtol=1e-6)
205
    >>> nx.is_isomorphic(G1, G2, edge_match=em)
206
    True
207

208
    For multigraphs G1 and G2, using 'weight' and 'linewidth' edge attributes
209
    with default values 7 and 2.5. Also using 'fill' node attribute with
210
    default value 'red'.
211

212
    >>> em = iso.numerical_multiedge_match(['weight', 'linewidth'], [7, 2.5])
213
    >>> nm = iso.categorical_node_match('fill', 'red')
214
    >>> nx.is_isomorphic(G1, G2, edge_match=em, node_match=nm)
215
    True
216

217
    See Also
218
    --------
219
    numerical_node_match, numerical_edge_match, numerical_multiedge_match
220
    categorical_node_match, categorical_edge_match, categorical_multiedge_match
221

222
    References
223
    ----------
224
    .. [1]  L. P. Cordella, P. Foggia, C. Sansone, M. Vento,
225
       "An Improved Algorithm for Matching Large Graphs",
226
       3rd IAPR-TC15 Workshop  on Graph-based Representations in
227
       Pattern Recognition, Cuen, pp. 149-159, 2001.
228
       http://amalfi.dis.unina.it/graph/db/papers/vf-algorithm.pdf
229
    """
230
    if G1.is_directed() and G2.is_directed():
231
        GM = nx.algorithms.isomorphism.DiGraphMatcher
232
    elif (not G1.is_directed()) and (not G2.is_directed()):
233
        GM = nx.algorithms.isomorphism.GraphMatcher
234
    else:
235
        raise NetworkXError("Graphs G1 and G2 are not of the same type.")
236

    
237
    gm = GM(G1, G2, node_match=node_match, edge_match=edge_match)
238

    
239
    return gm.is_isomorphic()