test_traversal.py 4.2 KB
Newer Older
GaiYu0's avatar
GaiYu0 committed
1
2
3
import random
import sys
import time
4
import unittest
GaiYu0's avatar
GaiYu0 committed
5
6
7
8
9

import dgl
import networkx as nx
import numpy as np
import scipy.sparse as sp
10
import backend as F
GaiYu0's avatar
GaiYu0 committed
11

Gan Quan's avatar
Gan Quan committed
12
import itertools
13
from utils import parametrize_dtype
Gan Quan's avatar
Gan Quan committed
14

GaiYu0's avatar
GaiYu0 committed
15
16
np.random.seed(42)

Gan Quan's avatar
Gan Quan committed
17
def toset(x):
VoVAllen's avatar
VoVAllen committed
18
    # F.zerocopy_to_numpy may return a int
19
    return set(F.zerocopy_to_numpy(x).tolist())
Gan Quan's avatar
Gan Quan committed
20

21
@unittest.skipIf(F._default_context_str == 'gpu', reason="GPU not implemented")
22
@parametrize_dtype
23
def test_bfs(idtype, n=100):
Minjie Wang's avatar
Minjie Wang committed
24
25
26
27
28
29
30
31
32
    def _bfs_nx(g_nx, src):
        edges = nx.bfs_edges(g_nx, src)
        layers_nx = [set([src])]
        edges_nx = []
        frontier = set()
        edge_frontier = set()
        for u, v in edges:
            if u in layers_nx[-1]:
                frontier.add(v)
33
                edge_frontier.add(g.edge_ids(u, v))
Minjie Wang's avatar
Minjie Wang committed
34
35
36
37
            else:
                layers_nx.append(frontier)
                edges_nx.append(edge_frontier)
                frontier = set([v])
38
                edge_frontier = set([g.edge_ids(u, v)])
39
40
41
42
        # avoids empty successors
        if len(frontier) > 0 and len(edge_frontier) > 0:
            layers_nx.append(frontier)
            edges_nx.append(edge_frontier)
Minjie Wang's avatar
Minjie Wang committed
43
        return layers_nx, edges_nx
GaiYu0's avatar
GaiYu0 committed
44

45
    a = sp.random(n, n, 3 / n, data_rvs=lambda n: np.ones(n))
46
    g = dgl.graph(a).astype(idtype)
47

Minjie Wang's avatar
Minjie Wang committed
48
    g_nx = g.to_networkx()
GaiYu0's avatar
GaiYu0 committed
49
    src = random.choice(range(n))
Minjie Wang's avatar
Minjie Wang committed
50
    layers_nx, _ = _bfs_nx(g_nx, src)
51
    layers_dgl = dgl.bfs_nodes_generator(g, src)
GaiYu0's avatar
GaiYu0 committed
52
53
54
    assert len(layers_dgl) == len(layers_nx)
    assert all(toset(x) == y for x, y in zip(layers_dgl, layers_nx))

Minjie Wang's avatar
Minjie Wang committed
55
    g_nx = nx.random_tree(n, seed=42)
56
    g = dgl.graph(g_nx).astype(idtype)
Minjie Wang's avatar
Minjie Wang committed
57
58
    src = 0
    _, edges_nx = _bfs_nx(g_nx, src)
Gan Quan's avatar
Gan Quan committed
59
60
61
62
    edges_dgl = dgl.bfs_edges_generator(g, src)
    assert len(edges_dgl) == len(edges_nx)
    assert all(toset(x) == y for x, y in zip(edges_dgl, edges_nx))

63
@unittest.skipIf(F._default_context_str == 'gpu', reason="GPU not implemented")
64
@parametrize_dtype
65
def test_topological_nodes(idtype, n=100):
66
    a = sp.random(n, n, 3 / n, data_rvs=lambda n: np.ones(n))
GaiYu0's avatar
GaiYu0 committed
67
    b = sp.tril(a, -1).tocoo()
68
    g = dgl.graph(b).astype(idtype)
GaiYu0's avatar
GaiYu0 committed
69
70
71
72
73
74

    layers_dgl = dgl.topological_nodes_generator(g)

    adjmat = g.adjacency_matrix()
    def tensor_topo_traverse():
        n = g.number_of_nodes()
75
        mask = F.copy_to(F.ones((n, 1)), F.cpu())
76
77
78
        degree = F.spmm(adjmat, mask)
        while F.reduce_sum(mask) != 0.:
            v = F.astype((degree == 0.), F.float32)
GaiYu0's avatar
GaiYu0 committed
79
80
            v = v * mask
            mask = mask - v
81
            frontier = F.copy_to(F.nonzero_1d(F.squeeze(v, 1)), F.cpu())
GaiYu0's avatar
GaiYu0 committed
82
            yield frontier
83
            degree -= F.spmm(adjmat, v)
GaiYu0's avatar
GaiYu0 committed
84
85
86
87
88
89
90

    layers_spmv = list(tensor_topo_traverse())

    assert len(layers_dgl) == len(layers_spmv)
    assert all(toset(x) == toset(y) for x, y in zip(layers_dgl, layers_spmv))

DFS_LABEL_NAMES = ['forward', 'reverse', 'nontree']
91
@unittest.skipIf(F._default_context_str == 'gpu', reason="GPU not implemented")
92
@parametrize_dtype
93
94
def test_dfs_labeled_edges(idtype, example=False):
    dgl_g = dgl.DGLGraph().astype(idtype)
Gan Quan's avatar
Gan Quan committed
95
96
    dgl_g.add_nodes(6)
    dgl_g.add_edges([0, 1, 0, 3, 3], [1, 2, 2, 4, 5])
GaiYu0's avatar
GaiYu0 committed
97
    dgl_edges, dgl_labels = dgl.dfs_labeled_edges_generator(
Gan Quan's avatar
Gan Quan committed
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
            dgl_g, [0, 3], has_reverse_edge=True, has_nontree_edge=True)
    dgl_edges = [toset(t) for t in dgl_edges]
    dgl_labels = [toset(t) for t in dgl_labels]
    g1_solutions = [
            # edges           labels
            [[0, 1, 1, 0, 2], [0, 0, 1, 1, 2]],
            [[2, 2, 0, 1, 0], [0, 1, 0, 2, 1]],
    ]
    g2_solutions = [
            # edges        labels
            [[3, 3, 4, 4], [0, 1, 0, 1]],
            [[4, 4, 3, 3], [0, 1, 0, 1]],
    ]

    def combine_frontiers(sol):
        es, ls = zip(*sol)
        es = [set(i for i in t if i is not None)
              for t in itertools.zip_longest(*es)]
        ls = [set(i for i in t if i is not None)
              for t in itertools.zip_longest(*ls)]
        return es, ls

    for sol_set in itertools.product(g1_solutions, g2_solutions):
        es, ls = combine_frontiers(sol_set)
        if es == dgl_edges and ls == dgl_labels:
            break
    else:
        assert False

GaiYu0's avatar
GaiYu0 committed
127
if __name__ == '__main__':
128
129
130
    test_bfs(idtype='int32')
    test_topological_nodes(idtype='int32')
    test_dfs_labeled_edges(idtype='int32')