summaryrefslogtreecommitdiff
path: root/networkx/algorithms/tests/test_graph_hashing.py
blob: 70130b3999d563f69abb988cebb8769b9018e2d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
140
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
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
647
648
649
650
651
652
653
654
655
656
from networkx.generators import directed
import pytest
import networkx as nx

# Unit tests for the :func:`~networkx.weisfeiler_lehman_graph_hash` function


def test_empty_graph_hash():
    """
    empty graphs should give hashes regardless of other params
    """
    G1 = nx.empty_graph()
    G2 = nx.empty_graph()

    h1 = nx.weisfeiler_lehman_graph_hash(G1)
    h2 = nx.weisfeiler_lehman_graph_hash(G2)
    h3 = nx.weisfeiler_lehman_graph_hash(G2, edge_attr="edge_attr1")
    h4 = nx.weisfeiler_lehman_graph_hash(G2, node_attr="node_attr1")
    h5 = nx.weisfeiler_lehman_graph_hash(
        G2, edge_attr="edge_attr1", node_attr="node_attr1"
    )
    h6 = nx.weisfeiler_lehman_graph_hash(G2, iterations=10)

    assert h1 == h2
    assert h1 == h3
    assert h1 == h4
    assert h1 == h5
    assert h1 == h6


def test_directed():
    """
    A directed graph with no bi-directional edges should yield different a graph hash
    to the same graph taken as undirected if there are no hash collisions.
    """
    r = 10
    for i in range(r):
        G_directed = nx.gn_graph(10 + r, seed=100 + i)
        G_undirected = nx.to_undirected(G_directed)

        h_directed = nx.weisfeiler_lehman_graph_hash(G_directed)
        h_undirected = nx.weisfeiler_lehman_graph_hash(G_undirected)

        assert h_directed != h_undirected


def test_reversed():
    """
    A directed graph with no bi-directional edges should yield different a graph hash
    to the same graph taken with edge directions reversed if there are no hash collisions.
    Here we test a cycle graph which is the minimal counterexample
    """
    G = nx.cycle_graph(5, create_using=nx.DiGraph)
    nx.set_node_attributes(G, {n: str(n) for n in G.nodes()}, name="label")

    G_reversed = G.reverse()

    h = nx.weisfeiler_lehman_graph_hash(G, node_attr="label")
    h_reversed = nx.weisfeiler_lehman_graph_hash(G_reversed, node_attr="label")

    assert h != h_reversed


def test_isomorphic():
    """
    graph hashes should be invariant to node-relabeling (when the output is reindexed
    by the same mapping)
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=200 + i)
        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g1_hash = nx.weisfeiler_lehman_graph_hash(G1)
        g2_hash = nx.weisfeiler_lehman_graph_hash(G2)

        assert g1_hash == g2_hash


def test_isomorphic_edge_attr():
    """
    Isomorphic graphs with differing edge attributes should yield different graph
    hashes if the 'edge_attr' argument is supplied and populated in the graph,
    and there are no hash collisions.
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=300 + i)

        for a, b in G1.edges:
            G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
            G1[a][b]["edge_attr2"] = f"{a}-{b}-2"

        g1_hash_with_edge_attr1 = nx.weisfeiler_lehman_graph_hash(
            G1, edge_attr="edge_attr1"
        )
        g1_hash_with_edge_attr2 = nx.weisfeiler_lehman_graph_hash(
            G1, edge_attr="edge_attr2"
        )
        g1_hash_no_edge_attr = nx.weisfeiler_lehman_graph_hash(G1, edge_attr=None)

        assert g1_hash_with_edge_attr1 != g1_hash_no_edge_attr
        assert g1_hash_with_edge_attr2 != g1_hash_no_edge_attr
        assert g1_hash_with_edge_attr1 != g1_hash_with_edge_attr2

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_with_edge_attr1 = nx.weisfeiler_lehman_graph_hash(
            G2, edge_attr="edge_attr1"
        )
        g2_hash_with_edge_attr2 = nx.weisfeiler_lehman_graph_hash(
            G2, edge_attr="edge_attr2"
        )

        assert g1_hash_with_edge_attr1 == g2_hash_with_edge_attr1
        assert g1_hash_with_edge_attr2 == g2_hash_with_edge_attr2


def test_missing_edge_attr():
    """
    If the 'edge_attr' argument is supplied but is missing from an edge in the graph,
    we should raise a KeyError
    """
    G = nx.Graph()
    G.add_edges_from([(1, 2, {"edge_attr1": "a"}), (1, 3, {})])
    pytest.raises(KeyError, nx.weisfeiler_lehman_graph_hash, G, edge_attr="edge_attr1")


def test_isomorphic_node_attr():
    """
    Isomorphic graphs with differing node attributes should yield different graph
    hashes if the 'node_attr' argument is supplied and populated in the graph, and
    there are no hash collisions.
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=400 + i)

        for u in G1.nodes():
            G1.nodes[u]["node_attr1"] = f"{u}-1"
            G1.nodes[u]["node_attr2"] = f"{u}-2"

        g1_hash_with_node_attr1 = nx.weisfeiler_lehman_graph_hash(
            G1, node_attr="node_attr1"
        )
        g1_hash_with_node_attr2 = nx.weisfeiler_lehman_graph_hash(
            G1, node_attr="node_attr2"
        )
        g1_hash_no_node_attr = nx.weisfeiler_lehman_graph_hash(G1, node_attr=None)

        assert g1_hash_with_node_attr1 != g1_hash_no_node_attr
        assert g1_hash_with_node_attr2 != g1_hash_no_node_attr
        assert g1_hash_with_node_attr1 != g1_hash_with_node_attr2

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_with_node_attr1 = nx.weisfeiler_lehman_graph_hash(
            G2, node_attr="node_attr1"
        )
        g2_hash_with_node_attr2 = nx.weisfeiler_lehman_graph_hash(
            G2, node_attr="node_attr2"
        )

        assert g1_hash_with_node_attr1 == g2_hash_with_node_attr1
        assert g1_hash_with_node_attr2 == g2_hash_with_node_attr2


def test_missing_node_attr():
    """
    If the 'node_attr' argument is supplied but is missing from a node in the graph,
    we should raise a KeyError
    """
    G = nx.Graph()
    G.add_nodes_from([(1, {"node_attr1": "a"}), (2, {})])
    G.add_edges_from([(1, 2), (2, 3), (3, 1), (1, 4)])
    pytest.raises(KeyError, nx.weisfeiler_lehman_graph_hash, G, node_attr="node_attr1")


def test_isomorphic_edge_attr_and_node_attr():
    """
    Isomorphic graphs with differing node attributes should yield different graph
    hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
    the graph, and there are no hash collisions.
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=500 + i)

        for u in G1.nodes():
            G1.nodes[u]["node_attr1"] = f"{u}-1"
            G1.nodes[u]["node_attr2"] = f"{u}-2"

        for a, b in G1.edges:
            G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
            G1[a][b]["edge_attr2"] = f"{a}-{b}-2"

        g1_hash_edge1_node1 = nx.weisfeiler_lehman_graph_hash(
            G1, edge_attr="edge_attr1", node_attr="node_attr1"
        )
        g1_hash_edge2_node2 = nx.weisfeiler_lehman_graph_hash(
            G1, edge_attr="edge_attr2", node_attr="node_attr2"
        )
        g1_hash_edge1_node2 = nx.weisfeiler_lehman_graph_hash(
            G1, edge_attr="edge_attr1", node_attr="node_attr2"
        )
        g1_hash_no_attr = nx.weisfeiler_lehman_graph_hash(G1)

        assert g1_hash_edge1_node1 != g1_hash_no_attr
        assert g1_hash_edge2_node2 != g1_hash_no_attr
        assert g1_hash_edge1_node1 != g1_hash_edge2_node2
        assert g1_hash_edge1_node2 != g1_hash_edge2_node2
        assert g1_hash_edge1_node2 != g1_hash_edge1_node1

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_edge1_node1 = nx.weisfeiler_lehman_graph_hash(
            G2, edge_attr="edge_attr1", node_attr="node_attr1"
        )
        g2_hash_edge2_node2 = nx.weisfeiler_lehman_graph_hash(
            G2, edge_attr="edge_attr2", node_attr="node_attr2"
        )

        assert g1_hash_edge1_node1 == g2_hash_edge1_node1
        assert g1_hash_edge2_node2 == g2_hash_edge2_node2


def test_digest_size():
    """
    The hash string lengths should be as expected for a variety of graphs and
    digest sizes
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=1000 + i)

        h16 = nx.weisfeiler_lehman_graph_hash(G)
        h32 = nx.weisfeiler_lehman_graph_hash(G, digest_size=32)

        assert h16 != h32
        assert len(h16) == 16 * 2
        assert len(h32) == 32 * 2


# Unit tests for the :func:`~networkx.weisfeiler_lehman_hash_subgraphs` function


def is_subiteration(a, b):
    """
    returns True if that each hash sequence in 'a' is a prefix for
    the corresponding sequence indexed by the same node in 'b'.
    """
    return all(b[node][: len(hashes)] == hashes for node, hashes in a.items())


def hexdigest_sizes_correct(a, digest_size):
    """
    returns True if all hex digest sizes are the expected length in a node:subgraph-hashes
    dictionary. Hex digest string length == 2 * bytes digest length since each pair of hex
    digits encodes 1 byte (https://docs.python.org/3/library/hashlib.html)
    """
    hexdigest_size = digest_size * 2
    list_digest_sizes_correct = lambda l: all(len(x) == hexdigest_size for x in l)
    return all(list_digest_sizes_correct(hashes) for hashes in a.values())


def test_empty_graph_subgraph_hash():
    """ "
    empty graphs should give empty dict subgraph hashes regardless of other params
    """
    G = nx.empty_graph()

    subgraph_hashes1 = nx.weisfeiler_lehman_subgraph_hashes(G)
    subgraph_hashes2 = nx.weisfeiler_lehman_subgraph_hashes(G, edge_attr="edge_attr")
    subgraph_hashes3 = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="edge_attr")
    subgraph_hashes4 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=2)
    subgraph_hashes5 = nx.weisfeiler_lehman_subgraph_hashes(G, digest_size=64)

    assert subgraph_hashes1 == {}
    assert subgraph_hashes2 == {}
    assert subgraph_hashes3 == {}
    assert subgraph_hashes4 == {}
    assert subgraph_hashes5 == {}


def test_directed_subgraph_hash():
    """
    A directed graph with no bi-directional edges should yield different subgraph hashes
    to the same graph taken as undirected, if all hashes don't collide.
    """
    r = 10
    for i in range(r):
        G_directed = nx.gn_graph(10 + r, seed=100 + i)
        G_undirected = nx.to_undirected(G_directed)

        directed_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G_directed)
        undirected_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G_undirected)

        assert directed_subgraph_hashes != undirected_subgraph_hashes


def test_reversed_subgraph_hash():
    """
    A directed graph with no bi-directional edges should yield different subgraph hashes
    to the same graph taken with edge directions reversed if there are no hash collisions.
    Here we test a cycle graph which is the minimal counterexample
    """
    G = nx.cycle_graph(5, create_using=nx.DiGraph)
    nx.set_node_attributes(G, {n: str(n) for n in G.nodes()}, name="label")

    G_reversed = G.reverse()

    h = nx.weisfeiler_lehman_subgraph_hashes(G, node_attr="label")
    h_reversed = nx.weisfeiler_lehman_subgraph_hashes(G_reversed, node_attr="label")

    assert h != h_reversed


def test_isomorphic_subgraph_hash():
    """
    the subgraph hashes should be invariant to node-relabeling when the output is reindexed
    by the same mapping and all hashes don't collide.
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=200 + i)
        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g1_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G1)
        g2_subgraph_hashes = nx.weisfeiler_lehman_subgraph_hashes(G2)

        assert g1_subgraph_hashes == {-1 * k: v for k, v in g2_subgraph_hashes.items()}


def test_isomorphic_edge_attr_subgraph_hash():
    """
    Isomorphic graphs with differing edge attributes should yield different subgraph
    hashes if the 'edge_attr' argument is supplied and populated in the graph, and
    all hashes don't collide.
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=300 + i)

        for a, b in G1.edges:
            G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
            G1[a][b]["edge_attr2"] = f"{a}-{b}-2"

        g1_hash_with_edge_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr1"
        )
        g1_hash_with_edge_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr2"
        )
        g1_hash_no_edge_attr = nx.weisfeiler_lehman_subgraph_hashes(G1, edge_attr=None)

        assert g1_hash_with_edge_attr1 != g1_hash_no_edge_attr
        assert g1_hash_with_edge_attr2 != g1_hash_no_edge_attr
        assert g1_hash_with_edge_attr1 != g1_hash_with_edge_attr2

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_with_edge_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, edge_attr="edge_attr1"
        )
        g2_hash_with_edge_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, edge_attr="edge_attr2"
        )

        assert g1_hash_with_edge_attr1 == {
            -1 * k: v for k, v in g2_hash_with_edge_attr1.items()
        }
        assert g1_hash_with_edge_attr2 == {
            -1 * k: v for k, v in g2_hash_with_edge_attr2.items()
        }


def test_missing_edge_attr_subgraph_hash():
    """
    If the 'edge_attr' argument is supplied but is missing from an edge in the graph,
    we should raise a KeyError
    """
    G = nx.Graph()
    G.add_edges_from([(1, 2, {"edge_attr1": "a"}), (1, 3, {})])
    pytest.raises(
        KeyError, nx.weisfeiler_lehman_subgraph_hashes, G, edge_attr="edge_attr1"
    )


def test_isomorphic_node_attr_subgraph_hash():
    """
    Isomorphic graphs with differing node attributes should yield different subgraph
    hashes if the 'node_attr' argument is supplied and populated in the graph, and
    all hashes don't collide.
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=400 + i)

        for u in G1.nodes():
            G1.nodes[u]["node_attr1"] = f"{u}-1"
            G1.nodes[u]["node_attr2"] = f"{u}-2"

        g1_hash_with_node_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, node_attr="node_attr1"
        )
        g1_hash_with_node_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, node_attr="node_attr2"
        )
        g1_hash_no_node_attr = nx.weisfeiler_lehman_subgraph_hashes(G1, node_attr=None)

        assert g1_hash_with_node_attr1 != g1_hash_no_node_attr
        assert g1_hash_with_node_attr2 != g1_hash_no_node_attr
        assert g1_hash_with_node_attr1 != g1_hash_with_node_attr2

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_with_node_attr1 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, node_attr="node_attr1"
        )
        g2_hash_with_node_attr2 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, node_attr="node_attr2"
        )

        assert g1_hash_with_node_attr1 == {
            -1 * k: v for k, v in g2_hash_with_node_attr1.items()
        }
        assert g1_hash_with_node_attr2 == {
            -1 * k: v for k, v in g2_hash_with_node_attr2.items()
        }


def test_missing_node_attr_subgraph_hash():
    """
    If the 'node_attr' argument is supplied but is missing from a node in the graph,
    we should raise a KeyError
    """
    G = nx.Graph()
    G.add_nodes_from([(1, {"node_attr1": "a"}), (2, {})])
    G.add_edges_from([(1, 2), (2, 3), (3, 1), (1, 4)])
    pytest.raises(
        KeyError, nx.weisfeiler_lehman_subgraph_hashes, G, node_attr="node_attr1"
    )


def test_isomorphic_edge_attr_and_node_attr():
    """
    Isomorphic graphs with differing node attributes should yield different subgraph
    hashes if the 'node_attr' and 'edge_attr' argument is supplied and populated in
    the graph, and all hashes don't collide
    The output should still be invariant to node-relabeling
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G1 = nx.erdos_renyi_graph(n, p * i, seed=500 + i)

        for u in G1.nodes():
            G1.nodes[u]["node_attr1"] = f"{u}-1"
            G1.nodes[u]["node_attr2"] = f"{u}-2"

        for a, b in G1.edges:
            G1[a][b]["edge_attr1"] = f"{a}-{b}-1"
            G1[a][b]["edge_attr2"] = f"{a}-{b}-2"

        g1_hash_edge1_node1 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr1", node_attr="node_attr1"
        )
        g1_hash_edge2_node2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr2", node_attr="node_attr2"
        )
        g1_hash_edge1_node2 = nx.weisfeiler_lehman_subgraph_hashes(
            G1, edge_attr="edge_attr1", node_attr="node_attr2"
        )
        g1_hash_no_attr = nx.weisfeiler_lehman_subgraph_hashes(G1)

        assert g1_hash_edge1_node1 != g1_hash_no_attr
        assert g1_hash_edge2_node2 != g1_hash_no_attr
        assert g1_hash_edge1_node1 != g1_hash_edge2_node2
        assert g1_hash_edge1_node2 != g1_hash_edge2_node2
        assert g1_hash_edge1_node2 != g1_hash_edge1_node1

        G2 = nx.relabel_nodes(G1, {u: -1 * u for u in G1.nodes()})

        g2_hash_edge1_node1 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, edge_attr="edge_attr1", node_attr="node_attr1"
        )
        g2_hash_edge2_node2 = nx.weisfeiler_lehman_subgraph_hashes(
            G2, edge_attr="edge_attr2", node_attr="node_attr2"
        )

        assert g1_hash_edge1_node1 == {
            -1 * k: v for k, v in g2_hash_edge1_node1.items()
        }
        assert g1_hash_edge2_node2 == {
            -1 * k: v for k, v in g2_hash_edge2_node2.items()
        }


def test_iteration_depth():
    """
    All nodes should have the correct number of subgraph hashes in the output when
    using degree as initial node labels
    Subsequent iteration depths for the same graph should be additive for each node
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=600 + i)

        depth3 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=3)
        depth4 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=4)
        depth5 = nx.weisfeiler_lehman_subgraph_hashes(G, iterations=5)

        assert all(len(hashes) == 3 for hashes in depth3.values())
        assert all(len(hashes) == 4 for hashes in depth4.values())
        assert all(len(hashes) == 5 for hashes in depth5.values())

        assert is_subiteration(depth3, depth4)
        assert is_subiteration(depth4, depth5)
        assert is_subiteration(depth3, depth5)


def test_iteration_depth_edge_attr():
    """
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels empty and using an edge attribute when aggregating
    neighborhoods.
    Subsequent iteration depths for the same graph should be additive for each node
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=700 + i)

        for a, b in G.edges:
            G[a][b]["edge_attr1"] = f"{a}-{b}-1"

        depth3 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", iterations=3
        )
        depth4 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", iterations=4
        )
        depth5 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", iterations=5
        )

        assert all(len(hashes) == 3 for hashes in depth3.values())
        assert all(len(hashes) == 4 for hashes in depth4.values())
        assert all(len(hashes) == 5 for hashes in depth5.values())

        assert is_subiteration(depth3, depth4)
        assert is_subiteration(depth4, depth5)
        assert is_subiteration(depth3, depth5)


def test_iteration_depth_node_attr():
    """
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels to an attribute.
    Subsequent iteration depths for the same graph should be additive for each node
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=800 + i)

        for u in G.nodes():
            G.nodes[u]["node_attr1"] = f"{u}-1"

        depth3 = nx.weisfeiler_lehman_subgraph_hashes(
            G, node_attr="node_attr1", iterations=3
        )
        depth4 = nx.weisfeiler_lehman_subgraph_hashes(
            G, node_attr="node_attr1", iterations=4
        )
        depth5 = nx.weisfeiler_lehman_subgraph_hashes(
            G, node_attr="node_attr1", iterations=5
        )

        assert all(len(hashes) == 3 for hashes in depth3.values())
        assert all(len(hashes) == 4 for hashes in depth4.values())
        assert all(len(hashes) == 5 for hashes in depth5.values())

        assert is_subiteration(depth3, depth4)
        assert is_subiteration(depth4, depth5)
        assert is_subiteration(depth3, depth5)


def test_iteration_depth_node_edge_attr():
    """
    All nodes should have the correct number of subgraph hashes in the output when
    setting initial node labels to an attribute and also using an edge attribute when
    aggregating neighborhoods.
    Subsequent iteration depths for the same graph should be additive for each node
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=900 + i)

        for u in G.nodes():
            G.nodes[u]["node_attr1"] = f"{u}-1"

        for a, b in G.edges:
            G[a][b]["edge_attr1"] = f"{a}-{b}-1"

        depth3 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=3
        )
        depth4 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=4
        )
        depth5 = nx.weisfeiler_lehman_subgraph_hashes(
            G, edge_attr="edge_attr1", node_attr="node_attr1", iterations=5
        )

        assert all(len(hashes) == 3 for hashes in depth3.values())
        assert all(len(hashes) == 4 for hashes in depth4.values())
        assert all(len(hashes) == 5 for hashes in depth5.values())

        assert is_subiteration(depth3, depth4)
        assert is_subiteration(depth4, depth5)
        assert is_subiteration(depth3, depth5)


def test_digest_size_subgraph_hash():
    """
    The hash string lengths should be as expected for a variety of graphs and
    digest sizes
    """
    n, r = 100, 10
    p = 1.0 / r
    for i in range(1, r + 1):
        G = nx.erdos_renyi_graph(n, p * i, seed=1000 + i)

        digest_size16_hashes = nx.weisfeiler_lehman_subgraph_hashes(G)
        digest_size32_hashes = nx.weisfeiler_lehman_subgraph_hashes(G, digest_size=32)

        assert digest_size16_hashes != digest_size32_hashes

        assert hexdigest_sizes_correct(digest_size16_hashes, 16)
        assert hexdigest_sizes_correct(digest_size32_hashes, 32)