def get_shortest_paths(graph, source, target): shortest_paths = [x for x in gt.all_shortest_paths(graph, source, target)] return { 'user_id': graph.vp.user_id[target], 'num_shortest_paths': len(shortest_paths), 'len_shortest_path': len(shortest_paths[0]) if len(shortest_paths) > 0 else 0 }
def refine_cost_array(self, g, cost_array, num=100): final_cost_array = [] for data in cost_array[:num]: v1 = data[0] v2 = data[1] paths = list( gt.all_shortest_paths(g, v1, v2, weights=g.edge_properties['weight'])) paths = sorted(paths, key=lambda x: len(list(x))) path = paths[0] djk_path = [int(x) for x in path] djk_pos = [g.vertex_properties['x'][v] for v in djk_path] djk_path_length = len(path) djk_cost = self.cost_of_path(g, path) djk_efficiency = float(feff(djk_path_length / 2.0)) djk_final_cost = float(djk_cost / djk_efficiency) d = [ djk_path, djk_pos, djk_path_length, djk_cost, djk_efficiency, djk_final_cost ] final_cost_array.append(np.concatenate((data, d))) # data_copy = data[:] # data_copy = data_copy + d # final_cost_array.append(data_copy) final_cost_array = np.array(final_cost_array) args = final_cost_array.T[-1].argsort() final_cost_array = final_cost_array[args] return final_cost_array
def shortest_paths_from_source(graph, source): if source is None: return uid_to_shortest_paths = {} for v in graph.vertices(): shortest_paths = gt.all_shortest_paths(graph, source, v) uid_to_shortest_paths[graph.vp.user_id[v]] = [x for x in shortest_paths] return uid_to_shortest_paths
def shortest_paths_to_target(graph, target): if target is None: return uid_to_shortest_paths = {} for v in graph.vertices(): shortest_paths = gt.all_shortest_paths(graph, v, target) uid_to_shortest_paths[graph.vp.user_id[v]] = [x for x in shortest_paths] return uid_to_shortest_paths
def all_shortest_paths(self, source, target): v_source = self.vd[source] v_target = self.vd[target] path_iterator = gt.all_shortest_paths(self.G, v_source, v_target) return path_iterator