In [1]:
import numpy as np
from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
from scipy.sparse import csr_matrix
In [2]:
n = 100
c = n * 2
np.random.seed(1)
d = np.random.randint(0, n, c)
i = np.random.randint(0, n, c)
j = np.random.randint(0, n, c)
In [3]:
csr = csr_matrix((d, (i, j)), shape=(n, n))
a = csr.toarray()
In [4]:
print(a.shape)
In [5]:
print((a > 0).sum())
In [6]:
%%timeit
shortest_path(a)
In [7]:
%%timeit
shortest_path(csr)
In [8]:
%%timeit
shortest_path(a, method='D')
In [9]:
%%timeit
shortest_path(csr, method='D')
In [10]:
%%timeit
dijkstra(a)
In [11]:
%%timeit
dijkstra(csr)
In [12]:
%%timeit
shortest_path(a, method='FW')
In [13]:
%%timeit
shortest_path(csr, method='FW')
In [14]:
%%timeit
floyd_warshall(a)
In [15]:
%%timeit
floyd_warshall(csr)
In [16]:
%%timeit
dijkstra(a, indices=0)
In [17]:
%%timeit
dijkstra(csr, indices=0)
In [18]:
a_n = a.copy()
a_n[0, 1] = -10
csr_n = csr_matrix(a_n)
In [19]:
%%timeit
johnson(csr_n)
In [20]:
%%timeit
bellman_ford(csr_n)
In [21]:
%%timeit
floyd_warshall(csr_n)
In [22]:
%%timeit
johnson(csr_n, indices=0)