scikit-learn/sklearn/utils/tests/test_shortest_path.py

96 lines
2.8 KiB
Python
Raw Normal View History

from collections import defaultdict
2011-07-28 08:33:02 +08:00
import numpy as np
2011-08-03 06:56:56 +08:00
from numpy.testing import assert_array_almost_equal
2012-03-03 02:39:43 +08:00
from sklearn.utils.graph import (graph_shortest_path,
single_source_shortest_path_length)
2011-07-28 08:33:02 +08:00
2011-10-09 21:06:05 +08:00
def floyd_warshall_slow(graph, directed=False):
2011-07-28 08:33:02 +08:00
N = graph.shape[0]
#set nonzero entries to infinity
graph[np.where(graph == 0)] = np.inf
#set diagonal to zero
graph.flat[::N + 1] = 0
if not directed:
graph = np.minimum(graph, graph.T)
2011-08-01 08:26:16 +08:00
2011-07-28 08:33:02 +08:00
for k in range(N):
for i in range(N):
for j in range(N):
graph[i, j] = min(graph[i, j], graph[i, k] + graph[k, j])
graph[np.where(np.isinf(graph))] = 0
return graph
def generate_graph(N=20):
#sparse grid of distances
rng = np.random.RandomState(0)
dist_matrix = rng.random_sample((N, N))
2011-08-01 08:26:16 +08:00
2011-07-28 08:33:02 +08:00
#make symmetric: distances are not direction-dependent
2015-08-07 04:44:14 +08:00
dist_matrix = dist_matrix + dist_matrix.T
2011-07-28 08:33:02 +08:00
#make graph sparse
i = (rng.randint(N, size=N * N // 2), rng.randint(N, size=N * N // 2))
2011-07-28 08:33:02 +08:00
dist_matrix[i] = 0
#set diagonal to zero
dist_matrix.flat[::N + 1] = 0
return dist_matrix
2011-08-01 08:26:16 +08:00
2011-07-28 08:33:02 +08:00
2011-10-09 21:06:05 +08:00
def test_floyd_warshall():
2011-07-28 08:33:02 +08:00
dist_matrix = generate_graph(20)
2011-08-01 08:26:16 +08:00
2011-07-28 08:33:02 +08:00
for directed in (True, False):
graph_FW = graph_shortest_path(dist_matrix, directed, 'FW')
2011-10-09 21:06:05 +08:00
graph_py = floyd_warshall_slow(dist_matrix.copy(), directed)
2011-07-28 08:33:02 +08:00
assert_array_almost_equal(graph_FW, graph_py)
2011-10-09 21:06:05 +08:00
def test_dijkstra():
2011-07-28 08:33:02 +08:00
dist_matrix = generate_graph(20)
for directed in (True, False):
graph_D = graph_shortest_path(dist_matrix, directed, 'D')
2011-10-09 21:06:05 +08:00
graph_py = floyd_warshall_slow(dist_matrix.copy(), directed)
2011-08-01 08:26:16 +08:00
2011-07-28 08:33:02 +08:00
assert_array_almost_equal(graph_D, graph_py)
2012-03-03 02:39:43 +08:00
def test_shortest_path():
dist_matrix = generate_graph(20)
# We compare path length and not costs (-> set distances to 0 or 1)
dist_matrix[dist_matrix != 0] = 1
2012-03-03 02:39:43 +08:00
for directed in (True, False):
if not directed:
dist_matrix = np.minimum(dist_matrix, dist_matrix.T)
2012-03-03 02:39:43 +08:00
graph_py = floyd_warshall_slow(dist_matrix.copy(), directed)
for i in range(dist_matrix.shape[0]):
# Non-reachable nodes have distance 0 in graph_py
dist_dict = defaultdict(int)
2013-05-02 21:46:25 +08:00
dist_dict.update(single_source_shortest_path_length(dist_matrix,
i))
2012-03-03 02:39:43 +08:00
for j in range(graph_py[i].shape[0]):
assert_array_almost_equal(dist_dict[j], graph_py[i, j])
2011-07-28 08:33:02 +08:00
def test_dijkstra_bug_fix():
X = np.array([[0., 0., 4.],
[1., 0., 2.],
[0., 5., 0.]])
dist_FW = graph_shortest_path(X, directed=False, method='FW')
dist_D = graph_shortest_path(X, directed=False, method='D')
assert_array_almost_equal(dist_D, dist_FW)