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 for directed in (True, False): if not directed: dist_matrix = np.minimum(dist_matrix, dist_matrix.T) 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) dist_dict.update(single_source_shortest_path_length(dist_matrix, i)) for j in range(graph_py[i].shape[0]): assert_array_almost_equal(dist_dict[j], graph_py[i, j])
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 for directed in (True, False): if not directed: dist_matrix = np.minimum(dist_matrix, dist_matrix.T) 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) dist_dict.update(single_source_shortest_path_length(dist_matrix, i)) for j in range(graph_py[i].shape[0]): assert_array_almost_equal(dist_dict[j], graph_py[i, j])
# once row_val equals the row[0] entry continue with for loop indices.append(row[1]) data.append(row[2]) indptr.append(len(indices)) check = csr_matrix((data, indices, indptr), dtype=float).toarray() check2 = csr_matrix((data, indices, indptr), dtype=float) print(indices[0:5]) print(indptr[0:5]) print(data[0:5]) print(check[0:4, 0:5]) print(check.shape) print(check[6, 264306]) #6 | 264306 t1 = time.time() route = list(sorted(single_source_shortest_path_length(check2, 1).items())) t2 = time.time() total = t2 - t1 print("time", total) t1 = time.time() route = graph_shortest_path(check2, directed=False) t2 = time.time() total = t2 - t1 print("time", total) print(route)