pagerank.py 792 Bytes
Newer Older
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
import dgl.sparse as dglsp
import networkx as nx
import torch

N = 100
DAMP = 0.85
K = 10


def pagerank(A):
    D = A.sum(0)
    V = torch.ones(N) / N
    for _ in range(K):
        ########################################################################
        # (HIGHLIGHT) Take the advantage of DGL sparse APIs to calculate the
        # page rank.
        ########################################################################
        V = (1 - DAMP) / N + DAMP * A @ (V / D)
    return V


if __name__ == "__main__":
    g = nx.erdos_renyi_graph(N, 0.05, seed=10086)

    # Create the adjacency matrix of graph.
    edges = list(g.to_directed().edges())
    indices = torch.tensor(edges).transpose(0, 1)
    A = dglsp.spmatrix(indices, shape=(N, N))

    V = pagerank(A)
    print(V)