Skip to content

random_walks() seems slow #20

@hananell

Description

@hananell

Hi
I am searching for a quick way to do random walks and saw this package that claims to be good, but when comparing it with naive python approach, the naive approach was much faster... what am I missing?

import networkx as nx
import numpy as np
import csrgraph as cg
from time import time
import random

walk_len = 20
G = nx.fast_gnp_random_graph(100, 0.3, directed=True)    # just a random graph
GG = cg.csrgraph(G)

t1 = time()

walks = GG.random_walks(walklen=walk_len)

t2 = time()

walks = np.zeros((len(G.nodes()), walk_len))
for i, node in enumerate(G.nodes()):
    cur_node = node
    for j in range(walk_len):
        neighbor = random.choice(list(G.neighbors(cur_node)))   # choose one random neighbor
        walks[i][j] = neighbor   # add it to walk
        cur_node = neighbor   # save it to cur_node to continue from it next iteration

t3 = time()

print('cg ', t2-t1)
print('naive ', t3-t2)

output:
cg 5.419240713119507
naive 0.010957479476928711

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions