".github/vscode:/vscode.git/clone" did not exist on "fdec23188afd8821e3c441f01567e156e35a51ff"
ged.py 46.5 KB
Newer Older
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1
2
3
from copy import deepcopy
from heapq import heapify, heappop, heappush, nsmallest

4
5
import dgl
import numpy as np
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
6

7
8
9
10
# We use lapjv implementation (https://github.com/src-d/lapjv) to solve assignment problem, because of its scalability
# Also see https://github.com/berhane/LAP-solvers for benchmarking of LAP solvers
from lapjv import lapjv

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
11
12
EPSILON = 0.0000001

13

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
14
15
16
17
18
19
20
21
22
23
def validate_cost_functions(
    G1,
    G2,
    node_substitution_cost=None,
    edge_substitution_cost=None,
    G1_node_deletion_cost=None,
    G1_edge_deletion_cost=None,
    G2_node_insertion_cost=None,
    G2_edge_insertion_cost=None,
):
24
25
26
27
28
29
30
31
    """Validates cost functions (substitution, insertion, deletion) and initializes them with default=0 for substitution
    and default=1 for insertion/deletion
    if the provided ones are None.


    Parameters : see graph_edit_distance

    """
32
33
    num_G1_nodes = G1.num_nodes()
    num_G2_nodes = G2.num_nodes()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
34

35
36
    num_G1_edges = G1.num_edges()
    num_G2_edges = G2.num_edges()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
37

38
39
    # if any cost matrix is None, initialize it with default costs
    if node_substitution_cost is None:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
40
41
42
        node_substitution_cost = np.zeros(
            (num_G1_nodes, num_G2_nodes), dtype=float
        )
43
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
44
45
        assert node_substitution_cost.shape == (num_G1_nodes, num_G2_nodes)

46
    if edge_substitution_cost is None:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
47
48
49
        edge_substitution_cost = np.zeros(
            (num_G1_edges, num_G2_edges), dtype=float
        )
50
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
51
52
        assert edge_substitution_cost.shape == (num_G1_edges, num_G2_edges)

53
54
55
    if G1_node_deletion_cost is None:
        G1_node_deletion_cost = np.ones(num_G1_nodes, dtype=float)
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
56
57
        assert G1_node_deletion_cost.shape[0] == num_G1_nodes

58
59
60
    if G1_edge_deletion_cost is None:
        G1_edge_deletion_cost = np.ones(num_G1_edges, dtype=float)
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
61
62
        assert G1_edge_deletion_cost.shape[0] == num_G1_edges

63
64
65
    if G2_node_insertion_cost is None:
        G2_node_insertion_cost = np.ones(num_G2_nodes, dtype=float)
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
66
67
        assert G2_node_insertion_cost.shape[0] == num_G2_nodes

68
69
70
    if G2_edge_insertion_cost is None:
        G2_edge_insertion_cost = np.ones(num_G2_edges, dtype=float)
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        assert G2_edge_insertion_cost.shape[0] == num_G2_edges

    return (
        node_substitution_cost,
        edge_substitution_cost,
        G1_node_deletion_cost,
        G1_edge_deletion_cost,
        G2_node_insertion_cost,
        G2_edge_insertion_cost,
    )


def construct_cost_functions(
    G1,
    G2,
    node_substitution_cost,
    edge_substitution_cost,
    G1_node_deletion_cost,
    G1_edge_deletion_cost,
    G2_node_insertion_cost,
    G2_edge_insertion_cost,
):
93
94
95
96
97
98
    """Constructs cost matrices for LAP solution


    Parameters : see graph_edit_distance

    """
99
100
    num_G1_nodes = G1.num_nodes()
    num_G2_nodes = G2.num_nodes()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
101

102
103
    num_G1_edges = G1.num_edges()
    num_G2_edges = G2.num_edges()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
104

105
    # cost matrix of node mappings
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    cost_upper_bound = (
        node_substitution_cost.sum()
        + G1_node_deletion_cost.sum()
        + G2_node_insertion_cost.sum()
        + 1
    )
    C_node = np.zeros(
        (num_G1_nodes + num_G2_nodes, num_G1_nodes + num_G2_nodes), dtype=float
    )

    C_node[0:num_G1_nodes, 0:num_G2_nodes] = node_substitution_cost
    C_node[
        0:num_G1_nodes, num_G2_nodes : num_G2_nodes + num_G1_nodes
    ] = np.array(
        [
            G1_node_deletion_cost[i] if i == j else cost_upper_bound
            for i in range(num_G1_nodes)
            for j in range(num_G1_nodes)
        ]
    ).reshape(
        num_G1_nodes, num_G1_nodes
    )
    C_node[
        num_G1_nodes : num_G1_nodes + num_G2_nodes, 0:num_G2_nodes
    ] = np.array(
        [
            G2_node_insertion_cost[i] if i == j else cost_upper_bound
            for i in range(num_G2_nodes)
            for j in range(num_G2_nodes)
        ]
    ).reshape(
        num_G2_nodes, num_G2_nodes
    )

140
    # cost matrix of edge mappings
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    cost_upper_bound = (
        edge_substitution_cost.sum()
        + G1_edge_deletion_cost.sum()
        + G2_edge_insertion_cost.sum()
        + 1
    )
    C_edge = np.zeros(
        (num_G1_edges + num_G2_edges, num_G1_edges + num_G2_edges), dtype=float
    )

    C_edge[0:num_G1_edges, 0:num_G2_edges] = edge_substitution_cost
    C_edge[
        0:num_G1_edges, num_G2_edges : num_G2_edges + num_G1_edges
    ] = np.array(
        [
            G1_edge_deletion_cost[i] if i == j else cost_upper_bound
            for i in range(num_G1_edges)
            for j in range(num_G1_edges)
        ]
    ).reshape(
        num_G1_edges, num_G1_edges
    )
    C_edge[
        num_G1_edges : num_G1_edges + num_G2_edges, 0:num_G2_edges
    ] = np.array(
        [
            G2_edge_insertion_cost[i] if i == j else cost_upper_bound
            for i in range(num_G2_edges)
            for j in range(num_G2_edges)
        ]
    ).reshape(
        num_G2_edges, num_G2_edges
    )
    return C_node, C_edge

176
177
178
179
180
181
182

def get_edges_to_match(G, node_id, matched_nodes):
    # Find the edges in G with one end-point as node_id and other in matched_nodes or node_id
    incident_edges = np.array([], dtype=int)
    index = np.array([], dtype=int)
    direction = np.array([], dtype=int)
    if G.has_edge_between(node_id, node_id):
183
        self_edge_ids = G.edge_ids(node_id, node_id, return_array=True).numpy()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
184
185
186
        incident_edges = np.concatenate((incident_edges, self_edge_ids))
        index = np.concatenate((index, [-1] * len(self_edge_ids)))
        direction = np.concatenate((direction, [0] * len(self_edge_ids)))
187
    # Find predecessors
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    src, _, eid = G.in_edges([node_id], "all")
    eid = eid.numpy()
    src = src.numpy()
    filtered_indices = [
        (i, matched_nodes.index(src[i]))
        for i in range(len(src))
        if src[i] in matched_nodes
    ]
    matched_index = np.array([_[1] for _ in filtered_indices], dtype=int)
    eid_index = np.array([_[0] for _ in filtered_indices], dtype=int)
    index = np.concatenate((index, matched_index))
    incident_edges = np.concatenate((incident_edges, eid[eid_index]))
    direction = np.concatenate(
        (direction, np.array([-1] * len(filtered_indices), dtype=int))
    )
203
    # Find successors
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    _, dst, eid = G.out_edges([node_id], "all")
    eid = eid.numpy()
    dst = dst.numpy()
    filtered_indices = [
        (i, matched_nodes.index(dst[i]))
        for i in range(len(dst))
        if dst[i] in matched_nodes
    ]
    matched_index = np.array([_[1] for _ in filtered_indices], dtype=int)
    eid_index = np.array([_[0] for _ in filtered_indices], dtype=int)
    index = np.concatenate((index, matched_index))
    incident_edges = np.concatenate((incident_edges, eid[eid_index]))
    direction = np.concatenate(
        (direction, np.array([1] * len(filtered_indices), dtype=int))
    )
    return incident_edges, index, direction

221
222
223
224

def subset_cost_matrix(cost_matrix, row_ids, col_ids, num_rows, num_cols):
    # Extract thr subset of cost matrix corresponding to rows/cols in arrays row_ids/col_ids
    # Note that the shape of cost_matrix is (num_rows+num_cols) * (num_rows+num_cols)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
225
226
227
228
229
230
    extended_row_ids = np.concatenate(
        (row_ids, np.array([k + num_rows for k in col_ids]))
    )
    extended_col_ids = np.concatenate(
        (col_ids, np.array([k + num_cols for k in row_ids]))
    )
231
232
    return cost_matrix[extended_row_ids, :][:, extended_col_ids]

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
233

234
class search_tree_node:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
    def __init__(
        self,
        G1,
        G2,
        parent_matched_cost,
        parent_matched_nodes,
        parent_matched_edges,
        node_G1,
        node_G2,
        parent_unprocessed_nodes_G1,
        parent_unprocessed_nodes_G2,
        parent_unprocessed_edges_G1,
        parent_unprocessed_edges_G2,
        cost_matrix_nodes,
        cost_matrix_edges,
    ):
        self.matched_cost = parent_matched_cost
        self.future_approximate_cost = 0.0
        self.matched_nodes = deepcopy(parent_matched_nodes)
        self.matched_nodes[0].append(node_G1)
        self.matched_nodes[1].append(node_G2)
        self.matched_edges = deepcopy(parent_matched_edges)
        self.unprocessed_nodes_G1 = [
            _ for _ in parent_unprocessed_nodes_G1 if _ != node_G1
        ]
        self.unprocessed_nodes_G2 = [
            _ for _ in parent_unprocessed_nodes_G2 if _ != node_G2
        ]

264
        # Add the cost of matching nodes at this tree-node to the matched cost
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
265
266
267
268
269
270
        if (
            node_G1 is not None and node_G2 is not None
        ):  # Substitute node_G1 with node_G2
            self.matched_cost += cost_matrix_nodes[node_G1, node_G2]
        elif node_G1 is not None:  # Delete node_G1
            self.matched_cost += cost_matrix_nodes[
271
                node_G1, node_G1 + G2.num_nodes()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
272
273
274
            ]
        elif node_G2 is not None:  # Insert node_G2
            self.matched_cost += cost_matrix_nodes[
275
                node_G2 + G1.num_nodes(), node_G2
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
276
277
            ]

278
        # Add the cost of matching edges at this tree-node to the matched cost
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        incident_edges_G1 = []
        if (
            node_G1 is not None
        ):  # Find the edges with one end-point as node_G1 and other in matched nodes or node_G1
            incident_edges_G1, index_G1, direction_G1 = get_edges_to_match(
                G1, node_G1, parent_matched_nodes[0]
            )

        incident_edges_G2 = np.array([])
        if (
            node_G2 is not None
        ):  # Find the edges with one end-point as node_G2 and other in matched nodes or node_G2
            incident_edges_G2, index_G2, direction_G2 = get_edges_to_match(
                G2, node_G2, parent_matched_nodes[1]
            )

        if (
            len(incident_edges_G1) > 0 and len(incident_edges_G2) > 0
        ):  # Consider substituting
            matched_edges_cost_matrix = subset_cost_matrix(
                cost_matrix_edges,
                incident_edges_G1,
                incident_edges_G2,
302
303
                G1.num_edges(),
                G2.num_edges(),
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
304
305
            )
            max_sum = matched_edges_cost_matrix.sum()
306
307
308
309
            # take care of impossible assignments by assigning maximum cost
            for i in range(len(incident_edges_G1)):
                for j in range(len(incident_edges_G2)):
                    # both edges need to have same direction and the other end nodes are matched
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
310
311
312
313
314
                    if (
                        direction_G1[i] == direction_G2[j]
                        and index_G1[i] == index_G2[j]
                    ):
                        continue
315
                    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
316
                        matched_edges_cost_matrix[i, j] = max_sum
317
            # Match the edges as per the LAP solution
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
318
            row_ind, col_ind, _ = lapjv(matched_edges_cost_matrix)
319
320
            lap_cost = 0.00
            for i in range(len(row_ind)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
321
322
323
                lap_cost += matched_edges_cost_matrix[i, row_ind[i]]

            # Update matched edges
324
325
            for i in range(len(row_ind)):
                if i < len(incident_edges_G1):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
326
                    self.matched_edges[0].append(incident_edges_G1[i])
327
                    if row_ind[i] < len(incident_edges_G2):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
328
329
330
                        self.matched_edges[1].append(
                            incident_edges_G2[row_ind[i]]
                        )
331
                    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
332
                        self.matched_edges[1].append(None)
333
                elif row_ind[i] < len(incident_edges_G2):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
334
335
336
337
338
339
                    self.matched_edges[0].append(None)
                    self.matched_edges[1].append(incident_edges_G2[row_ind[i]])
            self.matched_cost += lap_cost

        elif len(incident_edges_G1) > 0:  # only deletion possible
            edge_deletion_cost = 0.0
340
            for edge in incident_edges_G1:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
341
                edge_deletion_cost += cost_matrix_edges[
342
                    edge, G2.num_edges() + edge
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
343
344
                ]
            # Update matched edges
345
            for edge in incident_edges_G1:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
346
347
348
349
350
351
352
353
354
                self.matched_edges[0].append(edge)
                self.matched_edges[1].append(None)

                # Update matched edges

            self.matched_cost += edge_deletion_cost

        elif len(incident_edges_G2) > 0:  # only insertion possible
            edge_insertion_cost = 0.0
355
            for edge in incident_edges_G2:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
356
                edge_insertion_cost += cost_matrix_edges[
357
                    G1.num_edges() + edge, edge
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
358
359
                ]
            # Update matched edges
360
            for edge in incident_edges_G2:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
361
362
363
364
365
                self.matched_edges[0].append(None)
                self.matched_edges[1].append(edge)

            self.matched_cost += edge_insertion_cost

366
        # Add the cost of matching of unprocessed nodes to the future approximate cost
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
367
368
369
370
371
372
373
374
        if (
            len(self.unprocessed_nodes_G1) > 0
            and len(self.unprocessed_nodes_G2) > 0
        ):  # Consider substituting
            unmatched_nodes_cost_matrix = subset_cost_matrix(
                cost_matrix_nodes,
                self.unprocessed_nodes_G1,
                self.unprocessed_nodes_G2,
375
376
                G1.num_nodes(),
                G2.num_nodes(),
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
377
            )
378
            # Match the edges as per the LAP solution
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
379
            row_ind, col_ind, _ = lapjv(unmatched_nodes_cost_matrix)
380
381
            lap_cost = 0.00
            for i in range(len(row_ind)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
382
                lap_cost += unmatched_nodes_cost_matrix[i, row_ind[i]]
383

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
384
385
386
387
            self.future_approximate_cost += lap_cost

        elif len(self.unprocessed_nodes_G1) > 0:  # only deletion possible
            node_deletion_cost = 0.0
388
            for node in self.unprocessed_nodes_G1:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
389
                node_deletion_cost += cost_matrix_nodes[
390
                    node, G2.num_nodes() + node
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
391
392
393
                ]

            self.future_approximate_cost += node_deletion_cost
394

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
395
396
        elif len(self.unprocessed_nodes_G2) > 0:  # only insertion possible
            node_insertion_cost = 0.0
397
            for node in self.unprocessed_nodes_G2:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
398
                node_insertion_cost += cost_matrix_nodes[
399
                    G1.num_nodes() + node, node
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
400
401
402
                ]

            self.future_approximate_cost += node_insertion_cost
403
404

        # Add the cost of LAP matching of unprocessed edges to the future approximate cost
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
        self.unprocessed_edges_G1 = [
            _ for _ in parent_unprocessed_edges_G1 if _ not in incident_edges_G1
        ]
        self.unprocessed_edges_G2 = [
            _ for _ in parent_unprocessed_edges_G2 if _ not in incident_edges_G2
        ]
        if (
            len(self.unprocessed_edges_G1) > 0
            and len(self.unprocessed_edges_G2) > 0
        ):  # Consider substituting
            unmatched_edges_cost_matrix = subset_cost_matrix(
                cost_matrix_edges,
                self.unprocessed_edges_G1,
                self.unprocessed_edges_G2,
419
420
                G1.num_edges(),
                G2.num_edges(),
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
421
            )
422
            # Match the edges as per the LAP solution
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
423
            row_ind, col_ind, _ = lapjv(unmatched_edges_cost_matrix)
424
425
            lap_cost = 0.00
            for i in range(len(row_ind)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
426
427
428
                lap_cost += unmatched_edges_cost_matrix[i, row_ind[i]]

            self.future_approximate_cost += lap_cost
429

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
430
431
        elif len(self.unprocessed_edges_G1) > 0:  # only deletion possible
            edge_deletion_cost = 0.0
432
            for edge in self.unprocessed_edges_G1:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
433
                edge_deletion_cost += cost_matrix_edges[
434
                    edge, G2.num_edges() + edge
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
435
                ]
436

Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
437
438
439
440
            self.future_approximate_cost += edge_deletion_cost

        elif len(self.unprocessed_edges_G2) > 0:  # only insertion possible
            edge_insertion_cost = 0.0
441
            for edge in self.unprocessed_edges_G2:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
442
                edge_insertion_cost += cost_matrix_edges[
443
                    G1.num_edges() + edge, edge
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
444
445
446
                ]

            self.future_approximate_cost += edge_insertion_cost
447
448
449

    # For heap insertion order
    def __lt__(self, other):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
450
451
452
453
454
455
456
457
458
459
        if (
            abs(
                (self.matched_cost + self.future_approximate_cost)
                - (other.matched_cost + other.future_approximate_cost)
            )
            > EPSILON
        ):
            return (self.matched_cost + self.future_approximate_cost) < (
                other.matched_cost + other.future_approximate_cost
            )
460
        elif abs(self.matched_cost - other.matched_cost) > EPSILON:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
461
462
            return other.matched_cost < self.matched_cost
            # matched cost is closer to reality
463
        else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
            return (
                len(self.unprocessed_nodes_G1)
                + len(self.unprocessed_nodes_G2)
                + len(self.unprocessed_edges_G1)
                + len(self.unprocessed_edges_G2)
            ) < (
                len(other.unprocessed_nodes_G1)
                + len(other.unprocessed_nodes_G2)
                + len(other.unprocessed_edges_G1)
                + len(other.unprocessed_edges_G2)
            )


def edit_cost_from_node_matching(
    G1, G2, cost_matrix_nodes, cost_matrix_edges, node_matching
):
    matched_cost = 0.0
481
482
483
    matched_nodes = ([], [])
    matched_edges = ([], [])
    # Add the cost of matching nodes
484
    for i in range(G1.num_nodes()):
485
        matched_cost += cost_matrix_nodes[i, node_matching[i]]
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
486
        matched_nodes[0].append(i)
487
        if node_matching[i] < G2.num_nodes():
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
488
            matched_nodes[1].append(node_matching[i])
489
        else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
490
            matched_nodes[1].append(None)
491
    for i in range(G1.num_nodes(), len(node_matching)):
492
        matched_cost += cost_matrix_nodes[i, node_matching[i]]
493
        if node_matching[i] < G2.num_nodes():
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
494
495
496
            matched_nodes[0].append(None)
            matched_nodes[1].append(node_matching[i])

497
498
    for i in range(len(matched_nodes[0])):
        # Add the cost of matching edges
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
        incident_edges_G1 = []
        if (
            matched_nodes[0][i] is not None
        ):  # Find the edges with one end-point as node_G1 and other in matched nodes or node_G1
            incident_edges_G1, index_G1, direction_G1 = get_edges_to_match(
                G1, matched_nodes[0][i], matched_nodes[0][:i]
            )

        incident_edges_G2 = np.array([])
        if (
            matched_nodes[1][i] is not None
        ):  # Find the edges with one end-point as node_G2 and other in matched nodes or node_G2
            incident_edges_G2, index_G2, direction_G2 = get_edges_to_match(
                G2, matched_nodes[1][i], matched_nodes[1][:i]
            )

        if (
            len(incident_edges_G1) > 0 and len(incident_edges_G2) > 0
        ):  # Consider substituting
            matched_edges_cost_matrix = subset_cost_matrix(
                cost_matrix_edges,
                incident_edges_G1,
                incident_edges_G2,
522
523
                G1.num_edges(),
                G2.num_edges(),
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
524
525
            )
            max_sum = matched_edges_cost_matrix.sum()
526
527
528
529
            # take care of impossible assignments by assigning maximum cost
            for i in range(len(incident_edges_G1)):
                for j in range(len(incident_edges_G2)):
                    # both edges need to have same direction and the other end nodes are matched
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
530
531
532
533
534
                    if (
                        direction_G1[i] == direction_G2[j]
                        and index_G1[i] == index_G2[j]
                    ):
                        continue
535
                    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
536
                        matched_edges_cost_matrix[i, j] = max_sum
537
            # Match the edges as per the LAP solution
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
538
            row_ind, col_ind, _ = lapjv(matched_edges_cost_matrix)
539
540
            lap_cost = 0.00
            for i in range(len(row_ind)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
541
542
543
                lap_cost += matched_edges_cost_matrix[i, row_ind[i]]

            # Update matched edges
544
545
            for i in range(len(row_ind)):
                if i < len(incident_edges_G1):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
546
                    matched_edges[0].append(incident_edges_G1[i])
547
                    if row_ind[i] < len(incident_edges_G2):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
548
                        matched_edges[1].append(incident_edges_G2[row_ind[i]])
549
                    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
550
                        matched_edges[1].append(None)
551
                elif row_ind[i] < len(incident_edges_G2):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
552
553
554
555
556
557
                    matched_edges[0].append(None)
                    matched_edges[1].append(incident_edges_G2[row_ind[i]])
            matched_cost += lap_cost

        elif len(incident_edges_G1) > 0:  # only deletion possible
            edge_deletion_cost = 0.0
558
            for edge in incident_edges_G1:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
559
                edge_deletion_cost += cost_matrix_edges[
560
                    edge, G2.num_edges() + edge
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
561
562
                ]
            # Update matched edges
563
            for edge in incident_edges_G1:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
564
565
566
567
568
569
570
571
572
                matched_edges[0].append(edge)
                matched_edges[1].append(None)

                # Update matched edges

            matched_cost += edge_deletion_cost

        elif len(incident_edges_G2) > 0:  # only insertion possible
            edge_insertion_cost = 0.0
573
            for edge in incident_edges_G2:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
574
                edge_insertion_cost += cost_matrix_edges[
575
                    G1.num_edges() + edge, edge
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
576
577
                ]
            # Update matched edges
578
            for edge in incident_edges_G2:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
                matched_edges[0].append(None)
                matched_edges[1].append(edge)

            matched_cost += edge_insertion_cost

    return (matched_cost, matched_nodes, matched_edges)


def contextual_cost_matrix_construction(
    G1,
    G2,
    node_substitution_cost,
    edge_substitution_cost,
    G1_node_deletion_cost,
    G1_edge_deletion_cost,
    G2_node_insertion_cost,
    G2_edge_insertion_cost,
):
597
598
    # Calculates approximate GED using linear assignment on the nodes with bipartite algorithm
    # cost matrix of node mappings
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
599

600
601
    num_G1_nodes = G1.num_nodes()
    num_G2_nodes = G2.num_nodes()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
602

603
604
    num_G1_edges = G1.num_edges()
    num_G2_edges = G2.num_edges()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

    cost_upper_bound = 2 * (
        node_substitution_cost.sum()
        + G1_node_deletion_cost.sum()
        + G2_node_insertion_cost.sum()
        + 1
    )
    cost_matrix = np.zeros(
        (num_G1_nodes + num_G2_nodes, num_G1_nodes + num_G2_nodes), dtype=float
    )

    cost_matrix[0:num_G1_nodes, 0:num_G2_nodes] = node_substitution_cost
    cost_matrix[
        0:num_G1_nodes, num_G2_nodes : num_G2_nodes + num_G1_nodes
    ] = np.array(
        [
            G1_node_deletion_cost[i] if i == j else cost_upper_bound
            for i in range(num_G1_nodes)
            for j in range(num_G1_nodes)
        ]
    ).reshape(
        num_G1_nodes, num_G1_nodes
    )
    cost_matrix[
        num_G1_nodes : num_G1_nodes + num_G2_nodes, 0:num_G2_nodes
    ] = np.array(
        [
            G2_node_insertion_cost[i] if i == j else cost_upper_bound
            for i in range(num_G2_nodes)
            for j in range(num_G2_nodes)
        ]
    ).reshape(
        num_G2_nodes, num_G2_nodes
    )

    self_edge_list_G1 = [np.array([], dtype=int)] * num_G1_nodes
    self_edge_list_G2 = [np.array([], dtype=int)] * num_G2_nodes
    incoming_edges_G1 = [np.array([], dtype=int)] * num_G1_nodes
    incoming_edges_G2 = [np.array([], dtype=int)] * num_G2_nodes
    outgoing_edges_G1 = [np.array([], dtype=int)] * num_G1_nodes
    outgoing_edges_G2 = [np.array([], dtype=int)] * num_G2_nodes

647
648
    for i in range(num_G1_nodes):
        if G1.has_edge_between(i, i):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
649
            self_edge_list_G1[i] = sorted(
650
                G1.edge_ids(i, i, return_array=True).numpy()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
651
652
653
654
655
656
657
658
659
            )
        incoming_edges_G1[i] = G1.in_edges([i], "eid").numpy()
        incoming_edges_G1[i] = np.setdiff1d(
            incoming_edges_G1[i], self_edge_list_G1[i]
        )
        outgoing_edges_G1[i] = G1.out_edges([i], "eid").numpy()
        outgoing_edges_G1[i] = np.setdiff1d(
            outgoing_edges_G1[i], self_edge_list_G1[i]
        )
660
661
    for i in range(num_G2_nodes):
        if G2.has_edge_between(i, i):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
662
            self_edge_list_G2[i] = sorted(
663
                G2.edge_ids(i, i, return_array=True).numpy()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
            )
        incoming_edges_G2[i] = G2.in_edges([i], "eid").numpy()
        incoming_edges_G2[i] = np.setdiff1d(
            incoming_edges_G2[i], self_edge_list_G2[i]
        )
        outgoing_edges_G2[i] = G2.out_edges([i], "eid").numpy()
        outgoing_edges_G2[i] = np.setdiff1d(
            outgoing_edges_G2[i], self_edge_list_G2[i]
        )

    selected_deletion_G1 = [
        G1_edge_deletion_cost[
            np.concatenate(
                (
                    self_edge_list_G1[i],
                    incoming_edges_G1[i],
                    outgoing_edges_G1[i],
                )
            )
        ]
684
        for i in range(G1.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
685
686
687
688
689
690
691
692
693
694
695
    ]
    selected_insertion_G2 = [
        G2_edge_insertion_cost[
            np.concatenate(
                (
                    self_edge_list_G2[i],
                    incoming_edges_G2[i],
                    outgoing_edges_G2[i],
                )
            )
        ]
696
        for i in range(G2.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
697
698
699
    ]

    # Add the cost of edge edition which are dependent of a node (see this as the cost associated with a substructure)
700
701
    for i in range(num_G1_nodes):
        for j in range(num_G2_nodes):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
702
703
704
705
706
707
708
709
710
711
712
713
714
            m = (
                len(self_edge_list_G1[i])
                + len(incoming_edges_G1[i])
                + len(outgoing_edges_G1[i])
            )
            n = (
                len(self_edge_list_G2[j])
                + len(incoming_edges_G2[j])
                + len(outgoing_edges_G2[j])
            )

            matrix_dim = m + n

715
            if matrix_dim == 0:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
                continue
            temp_edge_cost_matrix = np.empty((matrix_dim, matrix_dim))
            temp_edge_cost_matrix.fill(cost_upper_bound)

            temp_edge_cost_matrix[
                : len(self_edge_list_G1[i]), : len(self_edge_list_G2[j])
            ] = edge_substitution_cost[self_edge_list_G1[i], :][
                :, self_edge_list_G2[j]
            ]
            temp_edge_cost_matrix[
                len(self_edge_list_G1[i]) : len(self_edge_list_G1[i])
                + len(incoming_edges_G1[i]),
                len(self_edge_list_G2[j]) : len(self_edge_list_G2[j])
                + len(incoming_edges_G2[j]),
            ] = edge_substitution_cost[incoming_edges_G1[i], :][
                :, incoming_edges_G2[j]
            ]
            temp_edge_cost_matrix[
                len(self_edge_list_G1[i]) + len(incoming_edges_G1[i]) : m,
                len(self_edge_list_G2[j]) + len(incoming_edges_G2[j]) : n,
            ] = edge_substitution_cost[outgoing_edges_G1[i], :][
                :, outgoing_edges_G2[j]
            ]

            np.fill_diagonal(
                temp_edge_cost_matrix[:m, n:], selected_deletion_G1[i]
            )
            np.fill_diagonal(
                temp_edge_cost_matrix[m:, :n], selected_insertion_G2[j]
            )

            temp_edge_cost_matrix[m:, n:].fill(0)
            row_ind, col_ind, _ = lapjv(temp_edge_cost_matrix)
749
750
            lap_cost = 0.00
            for k in range(len(row_ind)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
751
752
753
754
                lap_cost += temp_edge_cost_matrix[k, row_ind[k]]

            cost_matrix[i, j] += lap_cost

755
    for i in range(num_G1_nodes):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
756
757
        cost_matrix[i, num_G2_nodes + i] += selected_deletion_G1[i].sum()

758
    for i in range(num_G2_nodes):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
        cost_matrix[num_G1_nodes + i, i] += selected_insertion_G2[i].sum()

    return cost_matrix


def hausdorff_matching(
    G1,
    G2,
    node_substitution_cost,
    edge_substitution_cost,
    G1_node_deletion_cost,
    G1_edge_deletion_cost,
    G2_node_insertion_cost,
    G2_edge_insertion_cost,
):
774
775
    # Calculates approximate GED using hausdorff_matching
    # cost matrix of node mappings
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
776

777
778
    num_G1_nodes = G1.num_nodes()
    num_G2_nodes = G2.num_nodes()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
779

780
781
    num_G1_edges = G1.num_edges()
    num_G2_edges = G2.num_edges()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
782
783
784
785
786
787
788
789

    self_edge_list_G1 = [np.array([], dtype=int)] * num_G1_nodes
    self_edge_list_G2 = [np.array([], dtype=int)] * num_G2_nodes
    incoming_edges_G1 = [np.array([], dtype=int)] * num_G1_nodes
    incoming_edges_G2 = [np.array([], dtype=int)] * num_G2_nodes
    outgoing_edges_G1 = [np.array([], dtype=int)] * num_G1_nodes
    outgoing_edges_G2 = [np.array([], dtype=int)] * num_G2_nodes

790
791
    for i in range(num_G1_nodes):
        if G1.has_edge_between(i, i):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
792
            self_edge_list_G1[i] = sorted(
793
                G1.edge_ids(i, i, return_array=True).numpy()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
794
795
796
797
798
799
800
801
802
            )
        incoming_edges_G1[i] = G1.in_edges([i], "eid").numpy()
        incoming_edges_G1[i] = np.setdiff1d(
            incoming_edges_G1[i], self_edge_list_G1[i]
        )
        outgoing_edges_G1[i] = G1.out_edges([i], "eid").numpy()
        outgoing_edges_G1[i] = np.setdiff1d(
            outgoing_edges_G1[i], self_edge_list_G1[i]
        )
803
804
    for i in range(num_G2_nodes):
        if G2.has_edge_between(i, i):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
805
            self_edge_list_G2[i] = sorted(
806
                G2.edge_ids(i, i, return_array=True).numpy()
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
807
808
809
810
811
812
813
814
815
816
817
818
            )
        incoming_edges_G2[i] = G2.in_edges([i], "eid").numpy()
        incoming_edges_G2[i] = np.setdiff1d(
            incoming_edges_G2[i], self_edge_list_G2[i]
        )
        outgoing_edges_G2[i] = G2.out_edges([i], "eid").numpy()
        outgoing_edges_G2[i] = np.setdiff1d(
            outgoing_edges_G2[i], self_edge_list_G2[i]
        )

    selected_deletion_self_G1 = [
        G1_edge_deletion_cost[self_edge_list_G1[i]]
819
        for i in range(G1.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
820
821
822
    ]
    selected_insertion_self_G2 = [
        G2_edge_insertion_cost[self_edge_list_G2[i]]
823
        for i in range(G2.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
824
825
826
827
    ]

    selected_deletion_incoming_G1 = [
        G1_edge_deletion_cost[incoming_edges_G1[i]]
828
        for i in range(G1.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
829
830
831
    ]
    selected_insertion_incoming_G2 = [
        G2_edge_insertion_cost[incoming_edges_G2[i]]
832
        for i in range(G2.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
833
834
835
836
    ]

    selected_deletion_outgoing_G1 = [
        G1_edge_deletion_cost[outgoing_edges_G1[i]]
837
        for i in range(G1.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
838
839
840
    ]
    selected_insertion_outgoing_G2 = [
        G2_edge_insertion_cost[outgoing_edges_G2[i]]
841
        for i in range(G2.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
842
843
844
845
846
847
848
849
850
851
852
853
    ]

    selected_deletion_G1 = [
        G1_edge_deletion_cost[
            np.concatenate(
                (
                    self_edge_list_G1[i],
                    incoming_edges_G1[i],
                    outgoing_edges_G1[i],
                )
            )
        ]
854
        for i in range(G1.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
855
856
857
858
859
860
861
862
863
864
865
    ]
    selected_insertion_G2 = [
        G2_edge_insertion_cost[
            np.concatenate(
                (
                    self_edge_list_G2[i],
                    incoming_edges_G2[i],
                    outgoing_edges_G2[i],
                )
            )
        ]
866
        for i in range(G2.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
    ]

    cost_G1 = np.array(
        [
            (G1_node_deletion_cost[i] + selected_deletion_G1[i].sum() / 2)
            for i in range(num_G1_nodes)
        ]
    )
    cost_G2 = np.array(
        [
            (G2_node_insertion_cost[i] + selected_insertion_G2[i].sum() / 2)
            for i in range(num_G2_nodes)
        ]
    )

882
883
884
885
886
887
888
889
    for i in range(num_G1_nodes):
        for j in range(num_G2_nodes):
            c1_self = deepcopy(selected_deletion_self_G1[i])
            c2_self = deepcopy(selected_insertion_self_G2[j])
            c1_incoming = deepcopy(selected_deletion_incoming_G1[i])
            c2_incoming = deepcopy(selected_insertion_incoming_G2[j])
            c1_outgoing = deepcopy(selected_deletion_outgoing_G1[i])
            c2_outgoing = deepcopy(selected_insertion_outgoing_G2[j])
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966

            for k, a in enumerate(self_edge_list_G1[i]):
                for l, b in enumerate(self_edge_list_G2[j]):
                    c1_self[k] = min(
                        c1_self[k], edge_substitution_cost[a, b] / 2
                    )
                    c2_self[l] = min(
                        c2_self[l], edge_substitution_cost[a, b] / 2
                    )

            for k, a in enumerate(incoming_edges_G1[i]):
                for l, b in enumerate(incoming_edges_G2[j]):
                    c1_incoming[k] = min(
                        c1_incoming[k], edge_substitution_cost[a, b] / 2
                    )
                    c2_incoming[l] = min(
                        c2_incoming[l], edge_substitution_cost[a, b] / 2
                    )

            for k, a in enumerate(outgoing_edges_G1[i]):
                for l, b in enumerate(outgoing_edges_G2[j]):
                    c1_outgoing[k] = min(
                        c1_outgoing[k], edge_substitution_cost[a, b] / 2
                    )
                    c2_outgoing[l] = min(
                        c2_outgoing[l], edge_substitution_cost[a, b] / 2
                    )

            edge_hausdorff_lower_bound = 0.0

            if len(selected_deletion_G1[i]) > len(selected_insertion_G2[j]):
                idx = np.argpartition(
                    selected_deletion_G1[i],
                    (
                        len(selected_deletion_G1[i])
                        - len(selected_insertion_G2[j])
                    ),
                )
                edge_hausdorff_lower_bound = selected_deletion_G1[i][
                    idx[
                        : (
                            len(selected_deletion_G1[i])
                            - len(selected_insertion_G2[j])
                        )
                    ]
                ].sum()
            elif len(selected_deletion_G1[i]) < len(selected_insertion_G2[j]):
                idx = np.argpartition(
                    selected_insertion_G2[j],
                    (
                        len(selected_insertion_G2[j])
                        - len(selected_deletion_G1[i])
                    ),
                )
                edge_hausdorff_lower_bound = selected_insertion_G2[j][
                    idx[
                        : (
                            len(selected_insertion_G2[j])
                            - len(selected_deletion_G1[i])
                        )
                    ]
                ].sum()

            sc_cost = 0.5 * (
                node_substitution_cost[i, j]
                + 0.5
                * max(
                    c1_self.sum()
                    + c2_self.sum()
                    + c1_incoming.sum()
                    + c2_incoming.sum()
                    + c1_outgoing.sum()
                    + c2_outgoing.sum(),
                    edge_hausdorff_lower_bound,
                )
            )

967
            if cost_G1[i] > sc_cost:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
968
                cost_G1[i] = sc_cost
969
            if cost_G2[j] > sc_cost:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
970
971
972
                cost_G2[j] = sc_cost

    graph_hausdorff_lower_bound = 0.0
973
    if num_G1_nodes > num_G2_nodes:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
974
975
976
977
978
979
        idx = np.argpartition(
            G1_node_deletion_cost, (num_G1_nodes - num_G2_nodes)
        )
        graph_hausdorff_lower_bound = G1_node_deletion_cost[
            idx[: (num_G1_nodes - num_G2_nodes)]
        ].sum()
980
    elif num_G1_nodes < num_G2_nodes:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
981
982
983
984
985
986
987
988
989
990
991
992
993
        idx = np.argpartition(
            G2_node_insertion_cost, (num_G2_nodes - num_G1_nodes)
        )
        graph_hausdorff_lower_bound = G2_node_insertion_cost[
            idx[: (num_G2_nodes - num_G1_nodes)]
        ].sum()

    graph_hausdorff_cost = max(
        graph_hausdorff_lower_bound, cost_G1.sum() + cost_G2.sum()
    )
    return graph_hausdorff_cost


994
995
def a_star_search(G1, G2, cost_matrix_nodes, cost_matrix_edges, max_beam_size):
    # A-star traversal
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
996
    open_list = []
997
    # Create first nodes in the A-star search tree, matching node 0 of G1 with all possibilities (each node of G2, and deletion)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
998
999
1000
1001
1002
1003
    matched_cost = 0.0
    matched_nodes = ([], [])
    # No nodes matched in the beginning
    matched_edges = ([], [])
    # No edges matched in the beginning
    unprocessed_nodes_G1 = [
1004
        i for i in range(G1.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1005
1006
    ]  # No nodes matched in the beginning
    unprocessed_nodes_G2 = [
1007
        i for i in range(G2.num_nodes())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1008
1009
    ]  # No nodes matched in the beginning
    unprocessed_edges_G1 = [
1010
        i for i in range(G1.num_edges())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1011
1012
    ]  # No edges matched in the beginning
    unprocessed_edges_G2 = [
1013
        i for i in range(G2.num_edges())
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1014
1015
    ]  # No edges matched in the beginning

1016
    for i in range(len(unprocessed_nodes_G2)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
        tree_node = search_tree_node(
            G1,
            G2,
            matched_cost,
            matched_nodes,
            matched_edges,
            unprocessed_nodes_G1[0],
            unprocessed_nodes_G2[i],
            unprocessed_nodes_G1,
            unprocessed_nodes_G2,
            unprocessed_edges_G1,
            unprocessed_edges_G2,
            cost_matrix_nodes,
            cost_matrix_edges,
        )
1032
        # Insert into open-list, implemented as a heap
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1033

1034
        heappush(open_list, tree_node)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1035

1036
    # Consider node deletion
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
    tree_node = search_tree_node(
        G1,
        G2,
        matched_cost,
        matched_nodes,
        matched_edges,
        unprocessed_nodes_G1[0],
        None,
        unprocessed_nodes_G1,
        unprocessed_nodes_G2,
        unprocessed_edges_G1,
        unprocessed_edges_G2,
        cost_matrix_nodes,
        cost_matrix_edges,
    )
1052
1053
    # Insert into open-list, implemented as a heap
    heappush(open_list, tree_node)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1054

1055
    while len(open_list) > 0:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
        # TODO: Create a node that processes multi node insertion deletion in one search node,
        # as opposed in multiple search nodes here
        parent_tree_node = heappop(open_list)
        matched_cost = parent_tree_node.matched_cost
        matched_nodes = parent_tree_node.matched_nodes
        matched_edges = parent_tree_node.matched_edges
        unprocessed_nodes_G1 = parent_tree_node.unprocessed_nodes_G1
        unprocessed_nodes_G2 = parent_tree_node.unprocessed_nodes_G2
        unprocessed_edges_G1 = parent_tree_node.unprocessed_edges_G1
        unprocessed_edges_G2 = parent_tree_node.unprocessed_edges_G2

1067
        if len(unprocessed_nodes_G1) == 0 and len(unprocessed_nodes_G2) == 0:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1068
            return (matched_cost, matched_nodes, matched_edges)
1069
1070
        elif len(unprocessed_nodes_G1) > 0:
            for i in range(len(unprocessed_nodes_G2)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
                tree_node = search_tree_node(
                    G1,
                    G2,
                    matched_cost,
                    matched_nodes,
                    matched_edges,
                    unprocessed_nodes_G1[0],
                    unprocessed_nodes_G2[i],
                    unprocessed_nodes_G1,
                    unprocessed_nodes_G2,
                    unprocessed_edges_G1,
                    unprocessed_edges_G2,
                    cost_matrix_nodes,
                    cost_matrix_edges,
                )
1086
1087
1088
1089
                # Insert into open-list, implemented as a heap
                heappush(open_list, tree_node)

            # Consider node deletion
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
            tree_node = search_tree_node(
                G1,
                G2,
                matched_cost,
                matched_nodes,
                matched_edges,
                unprocessed_nodes_G1[0],
                None,
                unprocessed_nodes_G1,
                unprocessed_nodes_G2,
                unprocessed_edges_G1,
                unprocessed_edges_G2,
                cost_matrix_nodes,
                cost_matrix_edges,
            )
1105
1106
            # Insert into open-list, implemented as a heap
            heappush(open_list, tree_node)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1107

1108
1109
        elif len(unprocessed_nodes_G2) > 0:
            for i in range(len(unprocessed_nodes_G2)):
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
                tree_node = search_tree_node(
                    G1,
                    G2,
                    matched_cost,
                    matched_nodes,
                    matched_edges,
                    None,
                    unprocessed_nodes_G2[i],
                    unprocessed_nodes_G1,
                    unprocessed_nodes_G2,
                    unprocessed_edges_G1,
                    unprocessed_edges_G2,
                    cost_matrix_nodes,
                    cost_matrix_edges,
                )
1125
1126
                # Insert into open-list, implemented as a heap
                heappush(open_list, tree_node)
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1127

1128
1129
        # Retain the top-k elements in open-list iff algorithm is beam
        if max_beam_size > 0 and len(open_list) > max_beam_size:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1130
1131
1132
1133
1134
            open_list = nsmallest(max_beam_size, open_list)
            heapify(open_list)

    return None

1135
1136
1137

def get_sorted_mapping(mapping_tuple, len1, len2):
    # Get sorted mapping of nodes/edges
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1138
1139
    result_0 = [None] * len1
    result_1 = [None] * len2
1140
1141
    for i in range(len(mapping_tuple[0])):
        if mapping_tuple[0][i] is not None and mapping_tuple[1][i] is not None:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
            result_0[mapping_tuple[0][i]] = mapping_tuple[1][i]
            result_1[mapping_tuple[1][i]] = mapping_tuple[0][i]
    return (result_0, result_1)


def graph_edit_distance(
    G1,
    G2,
    node_substitution_cost=None,
    edge_substitution_cost=None,
    G1_node_deletion_cost=None,
    G2_node_insertion_cost=None,
    G1_edge_deletion_cost=None,
    G2_edge_insertion_cost=None,
    algorithm="bipartite",
    max_beam_size=100,
):
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
    """Returns GED (graph edit distance) between DGLGraphs G1 and G2.


    Parameters
    ----------
    G1, G2: DGLGraphs

    node_substitution_cost, edge_substitution_cost : 2D numpy arrays
        node_substitution_cost[i,j] is the cost of substitution node i of G1 with node j of G2,
        similar definition for edge_substitution_cost. If None, default cost of 0 is used.

    G1_node_deletion_cost, G1_edge_deletion_cost : 1D numpy arrays
        G1_node_deletion_cost[i] is the cost of deletion of node i of G1,
        similar definition for G1_edge_deletion_cost. If None, default cost of 1 is used.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1173

1174
1175
1176
1177
1178
1179
1180
    G2_node_insertion_cost, G2_edge_insertion_cost : 1D numpy arrays
        G2_node_insertion_cost[i] is the cost of insertion of node i of G2,
        similar definition for G2_edge_insertion_cost. If None, default cost of 1 is used.

    algorithm : string
        Algorithm to use to calculate the edit distance.
        For now, 4 algorithms are supported
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1181
        i) astar: Calculates exact GED using A* graph traversal algorithm,
1182
        the heuristic used is the one proposed in (Riesen and Bunke, 2009) [1].
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1183
        ii) beam: Calculates approximate GED using A* graph traversal algorithm,
1184
        with a maximum number of nodes in the open list. [2]
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1185
        iii) bipartite (default): Calculates approximate GED using linear assignment on the nodes,
1186
1187
1188
1189
1190
        with jv (Jonker-Volgerand) algorithm. [3]
        iv) hausdorff: Approximation of graph edit distance based on Hausdorff matching [4].

    max_beam_size : int
        Maximum number of nodes in the open list, in case the algorithm is 'beam'.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1191
1192


1193
1194
1195
1196
1197
1198
1199
    Returns
    -------
    A tuple of three objects: (edit_distance, node_mapping, edge_mapping)
    edit distance is the calculated edit distance (float)
    node_mapping is a tuple of size two, containing the node assignments of the two graphs respectively
    eg., node_mapping[0][i] is the node mapping of node i of graph G1 (None means that the node is deleted)
    Similar definition for the edge_mapping
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1200

1201
1202
1203
1204
1205
1206
1207
1208
    For 'hausdorff', node_mapping and edge_mapping are returned as None, as this approximation does not return a unique edit path

    Examples
    --------
    >>> src1 = [0, 1, 2, 3, 4, 5];
    >>> dst1 = [1, 2, 3, 4, 5, 6];
    >>> src2 = [0, 1, 3, 4, 5];
    >>> dst2 = [1, 2, 4, 5, 6];
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1209

1210
1211
1212
1213
1214
1215
1216
1217
    >>> G1 = dgl.DGLGraph((src1, dst1))
    >>> G2 = dgl.DGLGraph((src2, dst2))
    >>> distance, node_mapping, edge_mapping = graph_edit_distance(G1, G1, algorithm='astar')
    >>> print(distance)
    0.0
    >>> distance, node_mapping, edge_mapping = graph_edit_distance(G1, G2, algorithm='astar')
    >>> print(distance)
    1.0
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1218

1219
1220
    References
    ----------
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1221
1222
    [1] Riesen, Kaspar, Stefan Fankhauser, and Horst Bunke.
    "Speeding Up Graph Edit Distance Computation with a Bipartite Heuristic."
1223
    MLG. 2007.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1224
1225
1226
    [2] Neuhaus, Michel, Kaspar Riesen, and Horst Bunke.
    "Fast suboptimal algorithms for the computation of graph edit distance."
    Joint IAPR International Workshops on Statistical Techniques in Pattern Recognition (SPR)
1227
    and Structural and Syntactic Pattern Recognition (SSPR). 2006.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1228
1229
    [3] Fankhauser, Stefan, Kaspar Riesen, and Horst Bunke.
    "Speeding up graph edit distance computation through fast bipartite matching."
1230
    International Workshop on Graph-Based Representations in Pattern Recognition. 2011.
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1231
1232
    [4] Fischer, Andreas, et al. "A hausdorff heuristic for efficient computation of graph edit distance."
    Joint IAPR International Workshops on Statistical Techniques in Pattern Recognition (SPR)
1233
1234
1235
1236
1237
    and Structural and Syntactic Pattern Recognition (SSPR). 2014.

    """
    # Handle corner cases
    if G1 is None and G2 is None:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1238
        return (0.0, ([], []), ([], []))
1239
    elif G1 is None:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1240
1241
1242
        edit_cost = 0.0

    # Validate
1243
    if algorithm != "beam":
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
        max_beam_size = -1
    (
        node_substitution_cost,
        edge_substitution_cost,
        G1_node_deletion_cost,
        G1_edge_deletion_cost,
        G2_node_insertion_cost,
        G2_edge_insertion_cost,
    ) = validate_cost_functions(
        G1,
        G2,
        node_substitution_cost,
        edge_substitution_cost,
        G1_node_deletion_cost,
        G1_edge_deletion_cost,
        G2_node_insertion_cost,
        G2_edge_insertion_cost,
    )

1263
    # cost matrices for LAP solution
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
    cost_matrix_nodes, cost_matrix_edges = construct_cost_functions(
        G1,
        G2,
        node_substitution_cost,
        edge_substitution_cost,
        G1_node_deletion_cost,
        G1_edge_deletion_cost,
        G2_node_insertion_cost,
        G2_edge_insertion_cost,
    )

1275
    if algorithm == "astar" or algorithm == "beam":
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1276
1277
1278
1279
1280
        (matched_cost, matched_nodes, matched_edges) = a_star_search(
            G1, G2, cost_matrix_nodes, cost_matrix_edges, max_beam_size
        )
        return (
            matched_cost,
1281
1282
            get_sorted_mapping(matched_nodes, G1.num_nodes(), G2.num_nodes()),
            get_sorted_mapping(matched_edges, G1.num_edges(), G2.num_edges()),
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1283
1284
        )

1285
    elif algorithm == "hausdorff":
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
        hausdorff_cost = hausdorff_matching(
            G1,
            G2,
            node_substitution_cost,
            edge_substitution_cost,
            G1_node_deletion_cost,
            G1_edge_deletion_cost,
            G2_node_insertion_cost,
            G2_edge_insertion_cost,
        )

        return (hausdorff_cost, None, None)

1299
    else:
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
        cost_matrix = contextual_cost_matrix_construction(
            G1,
            G2,
            node_substitution_cost,
            edge_substitution_cost,
            G1_node_deletion_cost,
            G1_edge_deletion_cost,
            G2_node_insertion_cost,
            G2_edge_insertion_cost,
        )
1310
        # Match the nodes as per the LAP solution
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
        row_ind, col_ind, _ = lapjv(cost_matrix)

        (
            matched_cost,
            matched_nodes,
            matched_edges,
        ) = edit_cost_from_node_matching(
            G1, G2, cost_matrix_nodes, cost_matrix_edges, row_ind
        )

        return (
            matched_cost,
1323
1324
            get_sorted_mapping(matched_nodes, G1.num_nodes(), G2.num_nodes()),
            get_sorted_mapping(matched_edges, G1.num_edges(), G2.num_edges()),
Hongzhi (Steve), Chen's avatar
Hongzhi (Steve), Chen committed
1325
        )