def dijkstra(self, start_in, stop_in, collision_ind=None): if collision_ind is not None: self.connection_matrix[collision_ind, :] = 0 self.connection_matrix[:, collision_ind] = 0 if start_in < 0: start = start_in + np.shape(self.vertices)[0] else: start = start_in if stop_in < 0: stop = stop_in + np.shape(self.vertices)[0] else: stop = stop_in # dist_matrix, predecessors = dijkstra(self.connection_matrix, indices=start, # directed=False, return_predecessors=True) length = 0 dist, predecessors = dijkstra(self.connection_matrix, indices=start, directed=False, return_predecessors=True) self.shortest_path = [] i = stop while i != start: if i == -9999: raise RuntimeError('No feasible path') self.shortest_path.append(i) length += dist[i] i = predecessors[i] self.shortest_path.append(start) self.shortest_path.reverse() logger.debug('path length={}'.format(length)) return self.shortest_path
def dijkstras(A, is_directed): lengths = A.copy() lengths = lengths.astype(float) lengths[lengths == 0] = np.inf lengths = 1/lengths lengths[lengths == 0] = np.inf return dijkstra(lengths, is_directed)
def targets(data, dataset: str, min_dist=2, max_dist=3): try: with open(f"Structures/bad_ex_{dataset}.json", "r") as f: return json.load(f) except FileNotFoundError: emap = IMap() r_t = defaultdict(set) h_r = defaultdict(set) for h, r, t in data: emap.put(h) emap.put(t) r_t[r].add(emap[t]) h_r[emap[h]].add(r) g = lil_matrix((len(emap), len(emap))) for h, r, t in data: g[emap[h], emap[t]] = 1 g = g.tocsr() ts = [] for i in trange(len(emap), desc="Bad examples", ncols=140): rel_inds = set() for r in h_r[i]: rel_inds |= r_t[r] dists = dijkstra( g, directed=False, unweighted=True, indices=i, return_predecessors=False, limit=max_dist ) dists_inds = set(np.asarray((min_dist <= dists) & (dists <= max_dist)).nonzero()[0].tolist()) ts.append(list(dists_inds ^ rel_inds)) with open(f"Structures/bad_ex_{dataset}.json", "w") as f: json.dump(ts, f) return ts
def sample_trajectory(M, n_states): # Samples trajectories from random nodes # in our domain (M) G, W = M.get_graph_inv() N = G.shape[0] if N >= n_states: rand_ind = np.random.permutation(N) else: rand_ind = np.tile(np.random.permutation(N), (1, 10)) init_states = rand_ind[0:n_states].flatten() goal_s = M.map_ind_to_state(M.targetx, M.targety) states = [] states_xy = [] states_one_hot = [] # Get optimal path from graph g_dense = W g_masked = np.ma.masked_values(g_dense, 0) g_sparse = csr_matrix(g_dense) d, pred = dijkstra(g_sparse, indices=goal_s, return_predecessors=True) for i in range(n_states): path = trace_path(pred, goal_s, init_states[i]) path = np.flip(path, 0) states.append(path) for state in states: L = len(state) r, c = M.get_coords(state) row_m = np.zeros((L, M.n_row)) col_m = np.zeros((L, M.n_col)) for i in range(L): row_m[i, r[i]] = 1 col_m[i, c[i]] = 1 states_one_hot.append(np.hstack((row_m, col_m))) states_xy.append(np.hstack((r, c))) return states_xy, states_one_hot
def main(): N = 10000 s = int(sys.argv[1]) t = int(sys.argv[2]) graph = np.random.randint(1, 10, size=(N, N)) graph = (graph + graph.T) / 2 graph = graph.astype('int') graph = csr_matrix(graph) start_time = time.time() dijkstra(csgraph=graph, directed=False, indices=s, return_predecessors=True)[0][t] end_time = time.time() print('Elasped time (sec.): ', end_time - start_time)
def compute_row_stats(i, n, adj_mat_original, hyp_dist_row, weighted, verbose=False): # the real distances in the graph true_dist_row = csg.dijkstra(adj_mat_original, indices=[i], unweighted=(not weighted), directed=False).squeeze() # true_dist_row = csg.dijkstra(adj_mat_original, indices=[i], unweighted=True, directed=True).squeeze() # print(f"{i}: {true_dist_row}") # row MAP neighbors = adj_mat_original.todense()[i].A1 # print(f"row {i}: ", neighbors) # print("shape", neighbors.shape) row_map = dis.map_row(neighbors, hyp_dist_row, n, i) # distortions: worst cases (contraction, expansion) and average dc, de, avg, _ = dis.distortion_row(true_dist_row, hyp_dist_row, n, i) # dc, de, avg = 0.0, 0.0, 0.0 # print out stats for this row if verbose: print( f"Row {i}, MAP = {curr_map}, distortion = {avg}, d_c = {dc}, d_e = {de}" ) return (row_map, avg, dc, de)
def shortest_path(self, directed=None, weighted=None, method='auto', return_predecessors=False, limit=np.inf, indices=None): '''Mirrors the scipy.sparse.csgraph function of the same name.''' d = directed if directed is not None else self.is_directed() w = weighted if weighted is not None else self.is_weighted() adj = self.matrix('dense', 'csr', 'csc') if not ss.issparse(adj): adj = np.ascontiguousarray(adj) # dispatch based on presence of limit and/or indices if np.isinf(limit) and indices is None: overwrite = not (hasattr(self, '_adj') and self._adj is adj) return ssc.shortest_path(adj, method=method, directed=d, return_predecessors=return_predecessors, unweighted=(not w), overwrite=overwrite) return ssc.dijkstra(adj, directed=d, indices=indices, return_predecessors=return_predecessors, unweighted=(not w), limit=limit)
def extract_durations_with_dijkstra(attention_map: np.array) -> np.array: """ Extracts durations from the attention matrix by finding the shortest monotonic path from top left to bottom right. """ attn_max = np.max(attention_map) path_probs = attn_max - attention_map adj_matrix = to_adj_matrix(path_probs) dist_matrix, predecessors = dijkstra(csgraph=adj_matrix, directed=True, indices=0, return_predecessors=True) path = [] pr_index = predecessors[-1] while pr_index != 0: path.append(pr_index) pr_index = predecessors[pr_index] path.reverse() # append first and last node path = [0] + path + [dist_matrix.size - 1] cols = path_probs.shape[1] mel_text = {} durations = np.zeros(attention_map.shape[1], dtype=np.int32) # collect indices (mel, text) along the path for node_index in path: i, j = from_node_index(node_index, cols) mel_text[i] = j for j in mel_text.values(): durations[j] += 1 return durations
def search_path_dijkstra(img, adjacency, src, dst): # We chose two arbitrary points, which we know are connected source = to_index(img, *src) target = to_index(img, *dst) # Compute the shortest path between the source and all other points in the image _, predecessors = dijkstra(adjacency, directed=True, indices=[source], unweighted=False, return_predecessors=True) # Constructs the path between source and target pixel_index = target pixels_path = [] if math.isinf(_[0][target]): return {'dist': math.inf} print('source: ' + str(_[0][source]**(1.0 / 16.0))) print('target: ' + str(_[0][target]**(1.0 / 16.0))) i = 0 #pixels_path.append(target) while pixel_index != source: i += 1 pixels_path.append(pixel_index) pixel_index = predecessors[0, pixel_index] print(str(i) + '.: ' + str(_[0][pixel_index]**(1.0 / 16.0))) pixels_path.append(source) return {'dist': _[0][target]**(1.0 / 16.0), 'path': pixels_path}
def get_graph_distance_matrix(data, num_neighbors, mode="connectivity", metric="correlation"): """ Compute graph distance matrices on data """ assert (mode in [ "connectivity", "distance" ]), "Norm argument has to be either one of 'connectivity', or 'distance'. " if mode == "connectivity": include_self = True else: include_self = False graph_data = kneighbors_graph(data, num_neighbors, mode=mode, metric=metric, include_self=include_self) shortestPath_data = dijkstra(csgraph=csr_matrix(graph_data), directed=False, return_predecessors=False) shortestPath_max = np.nanmax( shortestPath_data[shortestPath_data != np.inf]) shortestPath_data[shortestPath_data > shortestPath_max] = shortestPath_max shortestPath_data = shortestPath_data / shortestPath_data.max() return shortestPath_data
def generate(self): """ Генерация графа сетки. :return: """ indent = np.array((configs.BOUNDARY_INDENT, configs.BOUNDARY_INDENT)) self.points = np.random.uniform(self.lower_left - indent, self.upper_right + indent, size=(configs.GRID_SIZE, 2)) self.points = np.vstack((self.terminal_points, self.points)) self.kd_tree = KDTree(self.points) distances, indices = self.kd_tree.query(self.points, k=configs.N_GRID_NEIGHBOURS) # self.connectivity_matrix = np.zeros((self.points.shape[0], self.points.shape[0])) # for ind, neighbours, dists in zip(range(self.points.shape[0]), indices, distances): # self.connectivity_matrix[ind, neighbours] = dists # # self.connectivity_matrix = np.max(np.dstack((self.connectivity_matrix, self.connectivity_matrix.T)), axis=2) tri = Delaunay(self.points) indices, indptr = tri.vertex_neighbor_vertices self.connectivity_matrix = np.zeros( (self.points.shape[0], self.points.shape[0])) for ind in range(len(self.points)): neighbours = indptr[indices[ind]:indices[ind + 1]] vectors = self.points[ind].reshape( 1, -1).T - self.points[neighbours].T distances = np.linalg.norm(vectors, axis=0) self.connectivity_matrix[ind, neighbours] = distances self.distance_matrix, self.predecessors = dijkstra( self.connectivity_matrix, return_predecessors=True)
def solve3(N, M, abt): def dijkstra(V, G, s): INF = 1 << 30 dist = [INF] * (V + 1) dist[s] = 0 p_queue = [] p_queue.append((dist[s], s)) while p_queue: min_dist, u = heapq.heappop(p_queue) if dist[u] < min_dist: continue for v, c in G[u][1:]: alt = dist[u] + c if alt < dist[v]: dist[v] = alt heapq.heappush(p_queue, (alt, v)) return dist G = [[(-1, -1)] for _ in range(N + 1)] for i in range(M): a, b, t = abt[i] G[a].append((b, t)) G[b].append((a, t)) min_time = 1 << 30 for s in range(1, N + 1): time = max(dijkstra(N, G, s)[1:]) min_time = min(min_time, time) return min_time
def __init__(self, strn, prod): self.w, self.h = strn.shape self.vertices = list(itertools.product(range(self.w), range(self.h))) self.dist = self._get_dist_graph(strn, prod) self.path, self.route = dijkstra(self.dist, False, return_predecessors=True)
def compute_node_distance_to_soma(segskel, nodelbls, binary=False): soma_ids = np.nonzero(nodelbls == 0) graph = segskel.csgraph_binary if binary else segskel.csgraph dists = csgraph.dijkstra(graph, directed=False, indices=soma_ids) return np.min(np.min(dists, axis=0), axis=0)
def solve2(N, G): def dijkstra(V, G, s): INF = 1 << 30 dist = [INF] * V dist[s] = 0 p_queue = [] p_queue.append((dist[s], s)) while p_queue: min_dist, u = heapq.heappop(p_queue) if dist[u] < min_dist: continue for v, c in G[u]: alt = dist[u] + c if alt < dist[v]: dist[v] = alt heapq.heappush(p_queue, (alt, v)) return dist min_time = 1 << 30 for s in range(N): dist_matrix = dijkstra(N, G, s) time = np.max(dist_matrix) if time < min_time: min_time = time return min_time
def find_path(graph, start_cnode): import numpy as np from scipy import sparse from scipy.sparse import csgraph cnodes = graph["nodes"] moves = {tuple(key): value for key, value in zip(graph["edges"], graph["moves"])} mask = np.array([is_solved_node(cnode) for cnode in cnodes], bool) edges = np.array(graph["edges"]) data = np.ones(len(edges)) m = sparse.coo_matrix((data, (edges[:, 0], edges[:, 1]))) start_idx = cnodes.index(start_cnode) start = time.clock() dist, predecessors = csgraph.dijkstra(m, True, indices=[start_idx], return_predecessors=True) print time.clock() - start dist = dist[0] predecessors = predecessors[0] dist[~mask] = np.inf end_idx = np.argmin(dist) path = [] p = end_idx while p != start_idx: path.append(p) p = predecessors[p] path.append(start_idx) path = path[::-1] print len(path) return for n1, n2 in zip(path[:-1], path[1:]): print moves[n1, n2] print cnode_str(cnodes[n2])
def dijkstra(self, graph_dense, s_i, s_j, t_i, t_j): """ Performs a graph search for source and target graph_dense : dense graph retpresentation of the costmap s_i, s_j : source coordinate on the costmap t_i, t_j : target coordinate on the costmap """ source_id = self.graph_id(s_i, s_j) target_id = self.graph_id(t_i, t_j) graph_sparse = csgraph.csgraph_from_dense(graph_dense) dist_matrix, predecessors = csgraph.dijkstra( graph_sparse, directed=not self.average_cost, return_predecessors=True, indices=source_id, limit=np.inf) path = [] path.append((t_i, t_j)) while True: target_id_bkp = target_id target_id = predecessors[target_id] t_i, t_j = self.costmap_id(target_id) s_i, s_j = self.costmap_id(target_id_bkp) path.append((t_i, t_j)) if source_id == target_id: break return path
def isograph(self, min_weight=None): '''Remove short-circuit edges using the Isograph algorithm. min_weight : float, optional Minimum weight of edges to consider removing. Defaults to max(MST). From "Isograph: Neighbourhood Graph Construction Based On Geodesic Distance For Semi-Supervised Learning" by Ghazvininejad et al., 2011. Note: This uses the non-iterative algorithm which removes edges rather than reweighting them. ''' W = self.matrix('dense') # get candidate edges: all edges - MST edges tree = self.minimum_spanning_subtree() candidates = np.argwhere((W - tree.matrix('dense')) > 0) cand_weights = W[candidates[:,0], candidates[:,1]] # order by increasing edge weight order = np.argsort(cand_weights) cand_weights = cand_weights[order] # disregard edges shorter than a threshold if min_weight is None: min_weight = tree.edge_weights().max() idx = np.searchsorted(cand_weights, min_weight) cand_weights = cand_weights[idx:] candidates = candidates[order[idx:]] # check each candidate edge to_remove = np.zeros_like(cand_weights, dtype=bool) for i, (u,v) in enumerate(candidates): W_uv = np.where(W < cand_weights[i], W, 0) len_uv = ssc.dijkstra(W_uv, indices=u, unweighted=True, limit=2)[v] if len_uv > 2: to_remove[i] = True ii, jj = candidates[to_remove].T return self.remove_edges(ii, jj, copy=True)
def find_minimum_pairing(graph, n, odd): dist_matrix = np.full((len(odd), len(odd)), np.inf) prevs = [[] for _ in range(n)] for s in range(len(odd)): dist, prev = dijkstra(csgraph=graph, directed=False, \ indices=odd[s], return_predecessors=True) prevs[odd[s]] = prev for d in range(len(odd)): if s != d: dist_matrix[s][d] = dist[odd[d]] row_ind, col_ind = linear_sum_assignment(dist_matrix) set_pairs = set() for i in range(len(row_ind)): u, v = odd[row_ind[i]], odd[col_ind[i]] if (u > v): u, v = v, u set_pairs.add((u, v)) result = [] for s, d in set_pairs: path = [] get_path(prevs[s], d, path) result.append((s, d, dist_matrix[odd.index(s)][odd.index(d)], path)) return result
def find_shortest_paths(graph, start_node): # using dijkstra """ Uses Dijkstra algorithm using Fibonacci Heaps to find the shortest path from a start point to all other points. :param graph: The N x N array of non-negative distances representing the input graph. :type graph: array, matrix, or sparse matrix, 2 dimensions :param start_node: The start point to compute the shortest paths :type start_node: array_like or int :return: The matrix of distances between graph nodes. dist_matrix has shape (n_indices, n_nodes) and dist_matrix[i, j] gives the shortest distance from point i to point j along the graph. :rtype: dist_matrix: 2D numpy array :return: Returned only if return_predecessors == True. The matrix of predecessors, which can be used to reconstruct the shortest paths. Row i of the predecessor matrix contains information on the shortest paths from point i: each entry predecessors[i, j] gives the index of the previous node in the path from point i to point j. If no path exists between point i and j, then predecessors[i, j] = -9999 :rtype: predecessors: 2D numpy array """ [dist_matrix, predecessors] = dijkstra(csgraph=graph, directed=True, indices=start_node, return_predecessors=True) return dist_matrix, predecessors
def find_minimum_pairing(M, n, out_u, in_u, cor_table): OUT = [] for o, oN in out_u: for _ in range(oN): OUT.append(cor_table.index(o)) IN = [] for i, iN in in_u: for _ in range(iN): IN.append(cor_table.index(i)) dist_matrix = np.full((len(OUT), len(IN)), np.inf) prevs = [[] for _ in range(n)] for s in range(len(OUT)): dist, prev = dijkstra(csgraph=M, directed=True,\ indices=OUT[s], return_predecessors=True) prevs[OUT[s]] = prev for d in range(len(IN)): dist_matrix[s][d] = dist[IN[d]] row_ind, col_ind = linear_sum_assignment(dist_matrix) result = [] for i in range(len(row_ind)): u, v = OUT[row_ind[i]], IN[col_ind[i]] path = [] get_path(prevs[u], u, v, path, cor_table) result.append((cor_table[u], cor_table[v], dist_matrix[OUT.index(u)][IN.index(v)], path)) return result
def get_distance_matrix(graph, limit=3): """Get Distance Matrix from a Graph. Parameters: graph: array, matrix or sparse matrix, 2 dimensions (N, N) Graph representation of the connectivity. See `scipy doc <https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.dijkstra.html#scipy.sparse.csgraph.dijkstra>`_ for reference. limit: integer Maximum number of steps to analyze. For most molecular information, three should be enough. Returns: return: scipy.sparse.csr_matrix, shape (N, N) A scipy.sparse.csr_matrix. All elements that are not connected within *limit* steps are set to zero. This is a potential memory bottleneck, as csgraph.dijkstra produces a dense output matrix. Here we replace all np.inf values with 0 and transform back to csr_matrix. Why not dok_matrix like the connectivity-matrix? Because row-picking is most likely and this is super fast with csr. """ mat = csgraph.dijkstra(graph, directed=False, limit=limit) mat[mat == np.inf] = 0 return sp.csr_matrix(mat, dtype=np.int8)
def stitch(self): """Main Stitch Func""" self.validate() print(self._edge_matrix) parents = csgraph.dijkstra(self._edge_matrix, directed=False, indices=self.center, return_predecessors=True)[1] print('Parent matrix:\n', parents) next_H = self.calculate_relative_homographies(parents) Hs = self.calculate_total_homographies(parents, next_H) all_new_corners = self.calculate_new_corners(Hs) base_shift, base_size = np.array( self.calculate_bounds(all_new_corners)) order = self.calculate_draw_order(parents) canvas = np.zeros((base_size[1], base_size[0], 4), dtype=np.uint8) for j in order: image = self._images[j] new_corners = all_new_corners[j] H = Hs[j] shift, size = np.array(fitting_rectangle(*new_corners)) dest_shift = shift - base_shift print('Post Transform of', image.name, 'is', *size) T = np.array([[1, 0, -shift[0]], [0, 1, -shift[1]], [0, 0, 1]]) Ht = T.dot(H) print('Translated homography:\n', Ht) new_image = cv2.warpPerspective( image.image, Ht, tuple(size), flags=cv2.INTER_LINEAR, ) im_pst(canvas, new_image, dest_shift) return canvas
def compute_dist_matrix(self, compute_predecessors=False): # create a distance_matrix dist_matrix from self.cs_graph by using dijkstra shortest path algorithm # dist_matrix is fully populated of size n x n # the entry dist_matrix[vertex_a, vertex_b] contains the shortest distance on cs_graph between vertex_a and vertex_b if self.args: self.args.logger.info("Computing {}x{} dist_matrix ...".format(self.n, self.n)) start = timer() if self.cs_graph is None: raise Exception("No CS_Graph available!") if compute_predecessors: self.dist_matrix, self.predecessors = dijkstra(self.cs_graph, directed=False, return_predecessors=True) else: self.dist_matrix = dijkstra(self.cs_graph, directed=False, return_predecessors=False) end = timer() if self.args: self.args.logger.info("\t done after {} secs".format(end-start))
def plot_test_distances(dataset: str): limit = 10 train, valid, test = load_dataset(dataset) # map entities to an id emap = IMap() for h, _, t in train: emap.put(h) emap.put(t) # build the kg kg = lil_matrix((len(emap), len(emap)), dtype=np.uint16) for h, _, t in train: kg[emap[h], emap[t]] = 1 kg = kg.tocsr() test.sort(key=lambda hrt: hrt[0]) distances = [] _h = None shortest = None for h, _, t in tqdm(test, desc="Distances"): if _h != h: shortest = dijkstra(kg, limit=limit, indices=emap[h], return_predecessors=False) _h = h distances.append(shortest[emap[t]]) distances = np.array(distances) distances[distances > limit] = limit + 1 plt.hist(distances, bins=range(0, limit + 2)) plt.axvline(distances.mean(), color="red", linestyle="dashed") plt.axvline(np.median(distances), color="black") plt.title(f"Distances of test triples in training graph in {dataset}") plt.xlabel("distance") plt.ylabel("# of nodes") plt.show()
def findingShortestPath(self,start,end): self.start = self.word_list.searchsorted('start') self.end = self.word_list.searchsorted('end') self.distances, self.predessors = dijkstra(self.graph,indices=self.start,return_predecessors=True) return distances[self.end]
def run(surface: CorticalMesh, roi: np.ndarray): """ Finds the shortest route to the ROI for every vertex :param surface: cortical surface :param roi: region of interest :return: """ graph = surface.graph_point_point('distance', dtype='float') roi_dist, roi_idx = sparse_min(graph[roi, :], 0) stack1 = sparse.vstack((roi_dist[~roi], graph[~roi, :][:, ~roi])).tocsr() full_mat = sparse.hstack((np.append(0, roi_dist[~roi])[:, None], stack1)) dist_matrix, predecessors = csgraph.dijkstra(full_mat, indices=0, directed=False, return_predecessors=True) full_dist_matrix = np.zeros(surface.nvertices) full_dist_matrix[~roi] = dist_matrix[1:] original_predecessors = np.zeros(predecessors.shape) original_idx = np.arange(surface.nvertices) original_predecessors[predecessors != 0] = original_idx[~roi][predecessors[predecessors != 0] - 1] original_predecessors[predecessors == 0] = original_idx[roi][roi_idx[predecessors[predecessors == 0] - 1]] full_predecessors = -np.ones(surface.nvertices, dtype='i4') full_predecessors[~roi] = original_predecessors[1:] closest_vertex = full_predecessors.copy() closest_vertex[roi] = original_idx[roi] for _ in range(surface.nvertices): replace = (~roi[closest_vertex]) & (closest_vertex != -1) if not replace.any(): break closest_vertex[replace] = full_predecessors[closest_vertex[replace]] return full_dist_matrix, full_predecessors, closest_vertex
def compute_path(sparse_graph, startpoint, endpoint): """ Task 6/7: Uses dijkstra-algorithm to calculate the cheapest path from startPoint to endPoint. The dijkstra-func gives a distance vector and a predecessor vector. The predecessor vector shows the point we came from. :param sparse_graph: Sparse graph of possible travels :param startpoint: Start point of the travel :param endpoint: Where the travel should end :return path, costpath: list of indices for the cheapest path and the cost of the travel (float) """ # Use dijkstra to get predecessor/cost vector. # sparse_graph is a parameter with all possible travels between points # indices sets stating city # return predecessors to True because we want calculate the cheapest path # "Directed" not necessary in this case, if false it finds the shortest path where the algorithm can # progress from point i to j along sparse_graph[i, j] or sparse_graph[j, i] cost_vector, predecessors = cs.dijkstra(sparse_graph, indices=startpoint, return_predecessors=True, directed=False) path = [endpoint ] # Adds end point to path-list, would not be appended in loop cost_path = cost_vector[endpoint] node = endpoint while node != startpoint: # Loops through predecessor vector "backwards" to find the cheapest path until node = predecessors[node] # we arrive at the start city path.append(node) # append previous visited node to path return list(reversed(path)), cost_path
def fill_path_cache(self, cities): cities = list(cities) n = self.vxs.shape[0] g = spa.dok_matrix((n + 2, n + 2)) edge = self.extend_area(self.edge, 5) for u in range(n): for v in self.adj_vxs[u]: if not (edge[u] and edge[v]): g[u, v] = self.edge_weight(u, v) if edge[u]: d = self.vxs[u, 0] - self.vxs[u, 1] if d < -0.5: g[n, u] = 1e-12 if d > 0.5: g[u, n + 1] = 1e-12 g = g.tocsc() tocities = cities + [n + 1] fromcities = cities + [n] dists, preds = csg.dijkstra(g, indices=fromcities, return_predecessors=True) for b in tocities: for i, a in enumerate(fromcities): if a == b: continue p = [b] while p[0] != a: p.insert(0, preds[i, p[0]]) p = [x for x in p if x < n] d = dists[i, b] self.path_cache['topleft' if a == n else a, 'bottomright' if b == n + 1 else b] = p, d
def fill_path_cache(self, cities): cities = list(cities) n = self.vxs.shape[0] g = spa.dok_matrix((n+2, n+2)) edge = self.extend_area(self.edge, 5) for u in xrange(n): for v in self.adj_vxs[u]: if not (edge[u] and edge[v]): g[u, v] = self.edge_weight(u, v) if edge[u]: d = self.vxs[u,0] - self.vxs[u,1] if d < -0.5: g[n, u] = 1e-12 if d > 0.5: g[u, n+1] = 1e-12 g = g.tocsc() tocities = cities + [n+1] fromcities = cities + [n] dists, preds = csg.dijkstra(g, indices=fromcities, return_predecessors=True) for b in tocities: for i, a in enumerate(fromcities): if a == b: continue p = [b] while p[0] != a: p.insert(0, preds[i, p[0]]) p = [x for x in p if x < n] d = dists[i, b] self.path_cache['topleft' if a == n else a, 'bottomright' if b == n+1 else b] = p, d
def calculate_distance(bus, bus1, bus2, X, V, ex=0.3, ev=0.3): """ Calculate distance matrix with Dijkstra's algorithm. """ ta = clock() dist = np.zeros((len(bus), len(bus))) # branch distances ind1 = mult_ind(bus1, bus) ind2 = mult_ind(bus2, bus) # Handle limitation in djikstra # Now minimum distance is 1 except for zero distances which are set to 1e-6 temp = np.abs(X)**ex * V**ev temp /= min(temp[temp > 0]) temp[temp == 0] = 1e-6 dist[ind1, ind2] = dist[ind2, ind1] = temp d = dijkstra(dist, directed=False) # shortest path between all bus-pairs # quick fix -- TO REVIEW! max_val = np.max(d) if np.isinf(max_val): max_val = np.unique(d)[-2] d = d.astype( np.float32) / max_val * 100 # normalise so system diameter = 100 units t = clock() - ta return d, t
def test_sk_csgraph(simple_skeleton): sk = simple_skeleton graph = sk.csgraph gdist = csgraph.dijkstra(graph, indices=[6]) assert np.all(gdist[0] == np.array([ 4., 3., 2., np.inf, np.inf, 1., 0.])) ugdist = csgraph.dijkstra(sk.csgraph_undirected, indices=[6]) assert np.all(ugdist[0] == np.array([4., 3., 2., 3., 4., 1., 0.])) bg = sk.csgraph_binary.toarray() assert np.all(np.unique(bg) == [0,1]) assert np.array_equal(bg, (sk.csgraph>0).toarray()) ubg = sk.csgraph_binary_undirected.toarray() assert np.all(np.unique(ubg) == [0,1]) assert np.array_equal(ubg, (sk.csgraph_undirected>0).toarray())
def checker(path_to_input, path_to_output): true_output = {} user_output = {} with open(path_to_input, 'r') as f: start, finish = map(int, f.readline().split()) start -= 1 finish -= 1 n, m, t = map(int, f.readline().split()) input_path = [[0 for _ in range(n)] for _ in range(n)] for _ in range(m): i, j = map(int, f.readline().split()) i -= 1 j -= 1 input_path[i][j] = 1 with open(path_to_output, 'r') as f: output_len = int(f.readline()) user_output['n'] = output_len matrix = csr_matrix(input_path) true_out, aa = dijkstra(csgraph=matrix, directed=True, return_predecessors=True) true_output['n'] = 0 if true_out[start, finish] == float('inf') else 1 return user_output, true_output, true_output['n'] == user_output['n']
def _compute_connection_costs_to_bus(n, busmap, costs, config, connection_costs_per_link=None, buses=None): if connection_costs_per_link is None: connection_costs_per_link = _prepare_connection_costs_per_link( n, costs, config) if buses is None: buses = busmap.index[busmap.index != busmap.values] connection_costs_to_bus = pd.DataFrame(index=buses) for tech in connection_costs_per_link: adj = n.adjacency_matrix(weights=pd.concat( dict(Link=connection_costs_per_link[tech].reindex(n.links.index), Line=pd.Series(0., n.lines.index)))) costs_between_buses = dijkstra( adj, directed=False, indices=n.buses.index.get_indexer(buses)) connection_costs_to_bus[tech] = costs_between_buses[ np.arange(len(buses)), n.buses.index.get_indexer(busmap.loc[buses])] return connection_costs_to_bus
def extract_manifold_distances_knn(D, knn=[3,4,5,7,10], add_mst=None): ''' Return the distances along a k nearest neighbour graph for the given distances D (Using dijkstra). It also returns the knn graph itself. This is a generator function and will return an iterator for each k given in knn. Optionally you can add the edges from an additional graph (usually mst), in order to ensure full connectedness. Give the graph you want to add as add_mst=mst. returns iterator for each k: iter([distances along knn, knn]) ''' # K Nearest Neighbours distances idxs = np.argsort(D) r = range(D.shape[0]) for k in knn: idx = idxs[:, :k] _distances = sparse.csc_matrix(D.shape) for neighbours in idx.T: _distances[r, neighbours] = D[r, neighbours] if add_mst is not None: for i,j,v in zip(*sparse.find(add_mst)): if _distances[i,j] == 0: _distances[i,j] = v nearest_neighbour_distances = dijkstra(_distances, directed=False) yield nearest_neighbour_distances, _distances
def get_reachable_cells(self, i, j, speed): "Returns all the cells reachable from (i, j) and the predecessor matrix" origin = self.from_coord_to_cell_number(i, j) dist, predecessor = dijkstra(self.dist_mat, indices=origin, return_predecessors=True) # Only take those where dist is reachable dist = np.argwhere(dist <= speed).flatten() # And convert it to a list of grid coordinates dist = map(self.from_cell_number_to_coord, dist) return dist, predecessor
def breadth_first(Matrix, neighbours, s0, t0): predecessors, dist = cs.dijkstra(Matrix, return_predecessors=True, indices=s0) res = np.array([]) t = t0 while t != s0 and not (np.isnan(t)): res = np.append(res, t) t = predecessors.item(t) if not (np.isnan(t)): res = np.append(res, s0) return np.flipud(res), res.size > 1
def extract_manifold_distances_mst(D): """ Return the distances along a minimal spanning tree for the given distances D (Using dijkstra). It also returns the mst itself. returns [distances along mst, mst] """ # First do the minimal spanning tree distances mst = minimum_spanning_tree(D) return dijkstra(mst, directed=False, return_predecessors=False), mst
def distanceMatrix(G, synNodes): """ Given a nx graph, produce an all to all distance dict via scipy sparse matrix black magic. Also, you get in 'id2index' the the mapping from a node id to the index in matrix scaledDistance. """ dmat = {} nodeList = tuple(G.nodes()) synNodes = set(synNodes) synIndices = tuple(i for i, node in enumerate(nodeList) if node in synNodes) dmat = dijkstra(nx.to_scipy_sparse_matrix(G, nodeList), directed=False, indices=synIndices) return dmat, {node: i for i, node in enumerate(nodeList)}
def shortest_path_subtree(self, start_idx, directed=True): '''Returns a subgraph containing only the shortest paths from start_idx to every other vertex. ''' adj = self.matrix() _, pred = ssc.dijkstra(adj, directed=directed, indices=start_idx, return_predecessors=True) adj = ssc.reconstruct_path(adj, pred, directed=directed) if not directed: adj = adj + adj.T return self.__class__.from_adj_matrix(adj)
def shortest_path(matrix_sparse, start_node): """ Computes the shortest path from start node to all other nodes :param matrix_sparse: A csr matrix of a sparse graph :param start_node: The given start node :return: Returns a distance matrix and a predecessor matrix for the given start node """ return csgraph.dijkstra( matrix_sparse, indices=start_node, directed=False, return_predecessors=True)
def fit(self,X, num, method='dijkstra'): # Construct k-neigh. graph knn = KNN(num).fit(X) #Find shortest path if method == 'dijkstra': result = dijkstra(knn) else: result = shortest_path(knn, method=method) #Multidimensional scaling #Can be used Kernel PCA model = KernelPCA(n_components=num) return model.fit_transform(result)
def neighborhood_subgraph(self, start_idx, radius=1, weighted=True, directed=True, return_mask=False): '''Returns a subgraph containing only vertices within a given geodesic radius of start_idx.''' adj = self.matrix(dense=True, csr=True, csc=True) dist = ssc.dijkstra(adj, directed=directed, indices=start_idx, unweighted=(not weighted), limit=radius) mask = np.isfinite(dist) sub_adj = adj[mask][:,mask] g = self.__class__.from_adj_matrix(sub_adj) if return_mask: return g, mask return g
def test_dijkstra_indices_min_only(directed, SP_ans, indices): SP_ans = np.array(SP_ans) indices = np.array(indices, dtype=np.int64) min_ind_ans = indices[np.argmin(SP_ans[indices, :], axis=0)] min_d_ans = np.zeros(SP_ans.shape[0], SP_ans.dtype) for k in range(SP_ans.shape[0]): min_d_ans[k] = SP_ans[min_ind_ans[k], k] min_ind_ans[np.isinf(min_d_ans)] = -9999 SP, pred, sources = dijkstra(directed_G, directed=directed, indices=indices, min_only=True, return_predecessors=True) assert_array_almost_equal(SP, min_d_ans) assert_array_equal(min_ind_ans, sources) SP = dijkstra(directed_G, directed=directed, indices=indices, min_only=True, return_predecessors=False) assert_array_almost_equal(SP, min_d_ans)
def flesh_out(X, W, embed_dim, CC_labels, dist_mult=2.0, angle_thresh=0.2, min_shortcircuit=4, max_degree=5, verbose=False): '''Given a connected graph adj matrix (W), add edges to flesh it out.''' W = W.astype(bool) assert np.all(W == W.T), 'graph given to flesh_out must be symmetric' D = pairwise_distances(X, metric='sqeuclidean') # compute average edge lengths for each point avg_edge_length = np.empty(X.shape[0]) for i,nbr_mask in enumerate(W): avg_edge_length[i] = D[i,nbr_mask].mean() # candidate edges must satisfy edge length for at least one end point dist_thresh = dist_mult * avg_edge_length dist_mask = (D < dist_thresh) | (D < dist_thresh[:,None]) # candidate edges must connect points >= min_shortcircuit hops away hops_mask = np.isinf(dijkstra(W, unweighted=True, limit=min_shortcircuit-1)) # candidate edges must not already be connected, or in the same initial CC CC_mask = CC_labels != CC_labels[:,None] candidate_edges = ~W & dist_mask & hops_mask & CC_mask candidate_points, = np.where(np.any(candidate_edges, axis=0)) if verbose: # pragma: no cover print('before F:', candidate_edges.sum(), 'potentials') # calc subspaces subspaces, _ = cluster_subspaces(X, embed_dim, CC_labels.max()+1, CC_labels) # upper triangular avoids p,q <-> q,p repeats ii,jj = np.where(np.triu(candidate_edges)) # Get angles edge_dirs = X[ii] - X[jj] ssi = subspaces[CC_labels[ii]] ssj = subspaces[CC_labels[jj]] F = edge_cluster_angle(edge_dirs, ssi, ssj) mask = F < angle_thresh edge_ii = ii[mask] edge_jj = jj[mask] if verbose: # pragma: no cover print('got', len(edge_ii), 'potential edges') # Prevent any one node from getting a really high degree degree = W.sum(axis=0) sorted_edges = np.hstack((edge_ii[:,None], edge_jj[:,None]) )[np.argsort(F[mask])] for e in sorted_edges: if degree[e].max() < max_degree: W[e[0],e[1]] = True W[e[1],e[0]] = True degree[e] += 1 return Graph.from_adj_matrix(W.astype(int))
def distances_in_structure(self): """ Return the structure distances, where each edge along the graph has a distance of one, such that the distance just means the number of hops to make in order to get from one point to another. This can be very helpful in doing structure analysis and clustering of the topslam embedded data points. returns hops, the pairwise number of hops between points along the tree. """ if getattr(self, '_struct_distances', None) is None: self._struct_distances = dijkstra(self.graph, directed=False, unweighted=True, return_predecessors=False) return self._struct_distances
def _compute_paths(self): """ Run Dijkstra's algorithm over the adjacency matrix for the paths produced by the simulation. :return: The distance matrix based on adjacency matrix self._adj_mat :rtype: np.array """ distance_mat = sp.dijkstra(self._adj_mat, directed=True) # assert not np.any(np.isinf(distance_mat)) if np.any(np.isinf(distance_mat)): logger.debug("Found inf distance!!") return distance_mat
def main(): if len(sys.argv) < 3: print 'usage: %s from to' % __file__ return sn,gn = sys.argv[1:] # 疎行列を作成 wgt,lf,lt,row,col = zip(*storage.connection()) nnode = storage.nnode() + 1 #print nnode graph = coo_matrix((wgt, (row, col)), shape=(nnode, nnode)) # 疎グラフを作成 n_components, labels = connected_components(graph, directed=False) s,g=map(storage.station_name2seq, [sn, gn]) print 'from:',s, print storage.station_by_cd(storage.seq2station_cd(s))['station_name'] print 'to:',g, print storage.station_by_cd(storage.seq2station_cd(g))['station_name'] # ダイクストラ法で最短経路を求める dist_matrix, predecessors = dijkstra( #dist_matrix, predecessors = floyd_warshall( graph , indices=[s] # 対象のノードから開始するもののみ , return_predecessors=True ) c = g path = [] while c != s: c = predecessors[0][c] if c == -9999: break path.append(c) path = list(reversed(path)) + [g] for i,(a,b) in enumerate(zip(path, path[1:] + path[-1:])): l1 = storage.line_cd_by_seq(a) l2 = storage.line_cd_by_seq(b) if i == 0: print storage.line_cd2line_name(l1) print ' ', storage.station_by_cd(storage.seq2station_cd(a))['station_name'] if l1 != l2: print storage.line_cd2line_name(l1),'>', print storage.line_cd2line_name(l2)
def calculate(self, journeysByZone): zoneCount = len(journeysByZone) import numpy self.time_matrix = numpy.zeros(shape=(zoneCount,zoneCount)) for i, (zone, journeys) in enumerate(journeysByZone.items()): connections = makeConnections(zone, journeys) for (otherZone, outbound), connection in connections.items(): secs = connection.avgDuration.seconds j = self.zoneIndices[otherZone] if outbound: self.time_matrix[i][j] = secs if self.time_matrix[j][i] == 0: self.time_matrix[j][i] = secs from scipy.sparse.csgraph import dijkstra self.shortest_paths, self.predecessors = dijkstra(self.time_matrix, return_predecessors=True)
def _find_cycle_inducers(adj, potential_edges, length_thresh, directed=False): # remove edges that induce large cycles path_dist = ssc.dijkstra(adj, directed=directed, return_predecessors=False, unweighted=True) remove_ii, remove_jj = [], [] for i,j in potential_edges: if length_thresh < path_dist[i,j] < np.inf: remove_ii.append(i) remove_jj.append(j) else: # keeping this edge: update path lengths tmp = (path_dist[:,i] + 1)[:,None] + path_dist[j,:] ii, jj = np.nonzero(tmp < path_dist) new_lengths = tmp[ii, jj] path_dist[ii,jj] = new_lengths if not directed: path_dist[jj,ii] = new_lengths return remove_ii, remove_jj
def nodes_to_graph(self, meters=600): """Convert nodes and links to graph""" data = self.nodes.links.link_length node_ids = self.nodes.links.node_ids row = node_ids.from_node col = node_ids.to_node N = len(self.nodes) mat = csc_matrix((data, (row, col)), shape=(N, N)) source_nodes = self.routes.source_nodes dist_matrix = dijkstra(mat, directed=True, return_predecessors=False, indices=self.routes.source_nodes, #limit=meters, ) dist_vector = dist_matrix.min(axis=0) self.set_link_distance(dist_vector) dist_vector[dist_vector > meters] = np.NINF self.get_max_nodes(dist_vector)
def test_shortest_path_min_only_random(n): np.random.seed(1234) data = scipy.sparse.rand(n, n, density=0.5, format='lil', random_state=42, dtype=np.float) data.setdiag(np.zeros(n, dtype=np.bool)) # choose some random vertices v = np.arange(n) np.random.shuffle(v) indices = v[:int(n*.1)] ds, pred, sources = dijkstra(data, directed=False, indices=indices, min_only=True, return_predecessors=True) for k in range(n): p = pred[k] s = sources[k] while(p != -9999): assert(sources[p] == s) p = pred[p]
def _get_social_distances(self, uidx, t, recommended_repos): G_F_t = self.G_F.at_t(t) G_I_t = self.G_I.at_t(t) uidx_to_others_distances = dijkstra(G_F_t, indices=uidx, unweighted=True, directed=False) uidx_to_others_distances[uidx] = np.inf #for later cleaning social_distances = np.array([ imin(uidx_to_others_distances[G_I_t[r].tocoo().col]) for r in recommended_repos ]) insc_recommendations = np.logical_not(np.isposinf(social_distances)) self.number_insc_per_user[uidx] += insc_recommendations.sum() insc_users = np.logical_not(np.isposinf(uidx_to_others_distances)) for i in xrange(len(social_distances)): if social_distances[i] == np.inf: if np.min(uidx_to_others_distances) == np.inf: social_distances[i] = self.NU-1 else: social_distances[i] = np.max(uidx_to_others_distances[insc_users]) return social_distances
def find_path(start_node): import numpy as np from scipy import sparse from scipy.sparse import csgraph from collections import Counter counts = Counter(start_node) blocks = ("A" * (counts["A"] // 4) + "B" * (counts["B"] // 2) + "C" * (counts["C"] // 2) + "D" * counts["D"]) if blocks not in graph_cache: graph = load_graph(blocks) graph_cache[blocks] = graph else: graph = graph_cache[blocks] cnodes = graph["nodes"] mask = np.array([is_solved_node(cnode) for cnode in cnodes], bool) edges = np.array(graph["edges"]) data = np.ones(len(edges)) m = sparse.coo_matrix((data, (edges[:, 0], edges[:, 1]))) start_idx = cnodes.index(start_node) dist, predecessors = csgraph.dijkstra(m, True, indices=[start_idx], return_predecessors=True) dist = dist[0] predecessors = predecessors[0] dist[~mask] = np.inf end_idx = np.argmin(dist) path = [] p = end_idx while p != start_idx: path.append(p) p = predecessors[p] path.append(start_idx) path = path[::-1] for n1, n2 in zip(path[:-1], path[1:]): yield cnodes[n2]
def isomap(W, num_vecs): G = -0.5 * dijkstra(W, directed=False) ** 2 embedder = KernelPCA(n_components=num_vecs, kernel='precomputed') return embedder.fit_transform(G)
def check(limit, result): SP = dijkstra(undirected_G, directed=False, limit=limit) assert_array_almost_equal(SP, result)
def similarities_to_landmarks(self, min_affinity=0, affinity_to_cost=None, distance_measure='least_cost', beta=0.01, distance_transformation=None, distance_to_similarity=None): ''' Compute the affinities from landmarks to all nodes, and from all nodes to landmarks Store it in self.L2all (horizontal: n_landmarks x n_nodes) and self.all2L (vertical: n_nodes x n_landmarks) N.B. : To avoid confusion, cost and affinities are properties of edges, while distances and similarities are properties of pair of nodes that are not necessarily directly connected. Parameters ---------- min_affinity: float (default = 1e-3) Affinities below this value will not be considered as possible steps. They will be associated with an infinite cost, will have an infinite distance to all landmark, and a 0 similarity to all node. affinity_to_cost: function handle (default = None) Function that defines the transformation of affinities into costs. It must accept a numpy vector of floats (the affinities) and return a vector of the same size (the costs) if None, cost are defined as 1 / affinities. distance_transformation: function handle (default = None) Function that would be applied to the results the dijkstra distances computation. self.distances_L2all = distance_transformation(dijkstra_results). If distance_transformation is None, self.distances_L2all = dijkstra_results. The same goes for self.distances_all2L. distance_transformation=np.log can be a good choice distance_to_similarity: function handle (default = None) Function that defines the transformation of distances into similarities. if None, similarities are defined as (max_distance - distance) / max_distance. ''' def finite_max(a): ''' Returns the maximum of an array, excluding infinte values ''' return np.amax(a[np.isfinite(a)]) A = self.G.A.copy() # make a copy of the affinity matrix # Transformation of affinities to cost impossible_steps = A.data < min_affinity if affinity_to_cost == None: # default affinity to cost transformation A.data[:] = 1. / A.data else: # user specified affinity to cost transformation A.data[:] = affinity_to_cost(A.data) A.data[impossible_steps] = np.inf # From landmarks to all points ############################## if self.verbose: print("Compute similarities from landmarks to all nodes...") # Shortest-path computation with Dijkstra self.distances_L2all = dijkstra(A, indices=self.landmarks) # Optional distance transformation if distance_transformation != None: self.distances_L2all = distance_transformation(self.distances_L2all) # Distance to similarity transformation if distance_to_similarity == None: # default distance to similarity transformation self.similarities_L2all = (finite_max(self.distances_L2all) - self.distances_L2all)/finite_max(self.distances_L2all) self.similarities_L2all[self.distances_L2all == np.inf] = 0 else: # user specified distance to similarity transformation self.similarities_L2all = distance_to_similarity(self.distances_L2all) # From all points to landmarks ############################## if self.verbose: print("Compute similarities from all nodes to landmarks...") if distance_measure == 'least_cost': # Shortest-path computation with Dijkstra: self.distances_all2L = np.transpose(dijkstra(A.transpose(), indices=self.landmarks)) elif distance_measure == 'free_energy': self.distances_all2L = self.free_energy_distances_to(destinations=self.landmarks, beta=beta) # Optional distance transformation if distance_transformation != None: self.distances_all2L = distance_transformation(self.distances_all2L) # Distance to similarity transformation if distance_to_similarity == None: # default distance to similarity transformation self.similarities_all2L = (finite_max(self.distances_all2L) - self.distances_all2L)/finite_max(self.distances_all2L) self.similarities_all2L[self.distances_all2L == np.inf] = 0 else: # user specified distance to similarity transformation self.similarities_all2L = distance_to_similarity(self.distances_all2L)
from scipy.sparse import csr_matrix from scipy.sparse.csgraph import dijkstra import numpy as np # 入力を取る N, M, T = map(int, input().split()) A = np.array([int(x) for x in input().split()]) # sparse 行列作成 in_, out, weight = [], [], [] for _ in range(M): s, t, w = map(int, input().split()) in_.append(s - 1) out.append(t - 1) weight.append(w) graph = csr_matrix((weight, (in_, out)), shape=(N, N), dtype=np.int64) inv_graph = csr_matrix((weight, (out, in_)), shape=(N, N), dtype=np.int64) # dijkstra 法で最短経路を計算 dists = dijkstra(graph, indices=0) inv_dists = dijkstra(inv_graph, indices=0) # 答えを計算 print(int(np.max(A * (T - (dists + inv_dists)))))
# connected_graphs = list(nx.connected_component_subgraphs(dmG)) # for i, g in enumerate(connected_graphs): # nx.write_gml(g, '../results/graphs/%d.gml'%i) # dm = dm.fillna(1) # dm = dm.iloc[:500, :500] otu_table = table.T otu_table = otu_table.loc[:, dm.index] dm = dm.fillna(np.inf) cosine = dm cosine = cosine.reindex_axis(sorted(otu_table.columns), axis=0) cosine = cosine.reindex_axis(sorted(otu_table.columns), axis=1) shortest = dijkstra(dm.values) shortest = pd.DataFrame(shortest, columns=dm.index, index=dm.columns) shortest = shortest.reindex_axis(sorted(otu_table.columns), axis=0) shortest = shortest.reindex_axis(sorted(otu_table.columns), axis=1) # otu_table = table.T otu_table = otu_table.reindex_axis(sorted(otu_table.columns), axis=1) # Uses an idea similar to simrank graph_dm = (otu_table>0).dot(cosine).dot((otu_table>0).T) graph_dm.to_csv('../results/simrank.txt', '\t') # Uses Aitchison distance # samples = ['CF31_A', u'CF31_B', u'CF141_A', u'CF141_B', u'Tuni', u'Bry'] dm = cosine.values dm[dm==np.inf]=0 mat = otu_table.values