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

import dgl
import networkx as nx
import numpy as np
import scipy.sparse as sp
import torch as th
10
import utils as U
GaiYu0's avatar
GaiYu0 committed
11

Gan Quan's avatar
Gan Quan committed
12
13
import itertools

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

Gan Quan's avatar
Gan Quan committed
16
17
18
def toset(x):
    return set(x.tolist())

19
def test_bfs_nodes(n=1000):
GaiYu0's avatar
GaiYu0 committed
20
21
22
23
24
25
26
27
28
    g = dgl.DGLGraph()
    a = sp.random(n, n, 10 / n, data_rvs=lambda n: np.ones(n))
    g.from_scipy_sparse_matrix(a)
    g_nx = g.to_networkx()

    src = random.choice(range(n))

    edges = nx.bfs_edges(g_nx, src)
    layers_nx = [set([src])]
Gan Quan's avatar
Gan Quan committed
29
    edges_nx = []
GaiYu0's avatar
GaiYu0 committed
30
    frontier = set()
Gan Quan's avatar
Gan Quan committed
31
    edge_frontier = set()
GaiYu0's avatar
GaiYu0 committed
32
33
34
    for u, v in edges:
        if u in layers_nx[-1]:
            frontier.add(v)
Gan Quan's avatar
Gan Quan committed
35
            edge_frontier.add(g.edge_id(u, v))
GaiYu0's avatar
GaiYu0 committed
36
37
        else:
            layers_nx.append(frontier)
Gan Quan's avatar
Gan Quan committed
38
            edges_nx.append(edge_frontier)
GaiYu0's avatar
GaiYu0 committed
39
            frontier = set([v])
Gan Quan's avatar
Gan Quan committed
40
            edge_frontier = set([g.edge_id(u, v)])
GaiYu0's avatar
GaiYu0 committed
41
    layers_nx.append(frontier)
Gan Quan's avatar
Gan Quan committed
42
    edges_nx.append(edge_frontier)
GaiYu0's avatar
GaiYu0 committed
43

44
45
    layers_dgl = dgl.bfs_nodes_generator(g, src)

GaiYu0's avatar
GaiYu0 committed
46
47
48
    assert len(layers_dgl) == len(layers_nx)
    assert all(toset(x) == y for x, y in zip(layers_dgl, layers_nx))

Gan Quan's avatar
Gan Quan committed
49
50
51
52
53
    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))

54
def test_topological_nodes(n=1000):
GaiYu0's avatar
GaiYu0 committed
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
    g = dgl.DGLGraph()
    a = sp.random(n, n, 10 / n, data_rvs=lambda n: np.ones(n))
    b = sp.tril(a, -1).tocoo()
    g.from_scipy_sparse_matrix(b)

    layers_dgl = dgl.topological_nodes_generator(g)

    adjmat = g.adjacency_matrix()
    def tensor_topo_traverse():
        n = g.number_of_nodes()
        mask = th.ones((n, 1))
        degree = th.spmm(adjmat, mask)
        while th.sum(mask) != 0.:
            v = (degree == 0.).float()
            v = v * mask
            mask = mask - v
            frontier = th.squeeze(th.squeeze(v).nonzero(), 1)
            yield frontier
            degree -= th.spmm(adjmat, v)

    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']
def test_dfs_labeled_edges(n=1000, example=False):
    dgl_g = dgl.DGLGraph()
Gan Quan's avatar
Gan Quan committed
83
84
    dgl_g.add_nodes(6)
    dgl_g.add_edges([0, 1, 0, 3, 3], [1, 2, 2, 4, 5])
GaiYu0's avatar
GaiYu0 committed
85
    dgl_edges, dgl_labels = dgl.dfs_labeled_edges_generator(
Gan Quan's avatar
Gan Quan committed
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
            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
116
117
118
119
120

if __name__ == '__main__':
    test_bfs_nodes()
    test_topological_nodes()
    test_dfs_labeled_edges()