def ssbp(self, source_node: int): """ Compute: Numpy array of the latest departure times from source_node to every nodes v in V within time interval, eccentricity fw of source_node, destination node in longest path, number of nodes reachables from source_node :param source_node: Node to start from :returns: - Distances forward - Node farther, starting from source_node - Eccentricity forward - Number of reachables nodes, starting from source_node """ super().ssbp(source_node=source_node) graph_t_path = tg_utils.transform_graph(graph=self._graph) graph_t = tg.Graph(file_path=graph_t_path, is_directed=self._graph.get_is_directed(), latest_node=self._graph.get_latest_node()) graph_tr_path = tg_utils.reverse_edges_sort(graph=graph_t) graph_tr = tg.Graph(file_path=graph_tr_path, is_directed=graph_t.get_is_directed(), latest_node=graph_t.get_latest_node()) back_bfs = bwbfs.TemporalBackwardBFS(graph=graph_tr, target_node=source_node, t_alpha=-self._t_omega, t_omega=-self._t_alpha) back_bfs.bfs() self._dist_fw = -(back_bfs.get_eat()) self._eccentricity_fw = -(back_bfs.get_eccentricity_eat()) self._node_farther_fw = back_bfs.get_idx_farther_eat() self._reachables_fw = back_bfs.get_reachables() self._dist_fw = self._t_omega - self._dist_fw self._eccentricity_fw = self._t_omega - self._eccentricity_fw return self._dist_fw, self._node_farther_fw, self._eccentricity_fw, self._reachables_fw
def __nodes_order_ldt(self): graph_t_path = tg_utils.transform_graph(graph=self.__graph) graph_t = tg.Graph(file_path=graph_t_path, is_directed=self.__graph.get_is_directed(), latest_node=self.__graph.get_latest_node()) graph_tr_path = tg_utils.reverse_edges_sort(graph=graph_t) graph_tr = tg.Graph(file_path=graph_tr_path, is_directed=graph_t.get_is_directed(), latest_node=graph_t.get_latest_node()) self.__nodes_order(graph=graph_tr, t_alpha=-self.__t_omega, t_omega=-self.__t_alpha)
def stbp(self, target_node: int): """ Compute: Numpy array of the earliest arrival times to target_node from every nodes v in V within time interval, eccentricity bw of target_node, destination node in longest path, number of nodes reaching target_node :param target_node: Target node :returns: - Distances backward - Node farther, arriving to target_node - Eccentricity forward - Number of reachables nodes, arriving to target_node """ super().stbp(target_node=target_node) graph_r_path = tg_utils.reverse_edges_sort(graph=self._graph) graph_r = tg.Graph(file_path=graph_r_path, is_directed=self._graph.get_is_directed(), latest_node=self._graph.get_latest_node()) back_bfs = bwbfs.TemporalBackwardBFS(graph=graph_r, target_node=target_node, t_alpha=self._t_alpha, t_omega=self._t_omega) back_bfs.bfs() self._dist_bw = back_bfs.get_eat() self._eccentricity_bw = back_bfs.get_eccentricity_eat() self._node_farther_bw = back_bfs.get_idx_farther_eat() self._reachables_bw = back_bfs.get_reachables() self._dist_bw = self._dist_bw - self._t_alpha self._eccentricity_bw = self._eccentricity_bw - self._t_alpha return self._dist_bw, self._node_farther_bw, self._eccentricity_bw, self._reachables_bw
def stbp(self, target_node: int): """ Compute: Numpy array of travel time of best paths to target_node from every nodes v in V within time interval, eccentricity bw of target_node, starting node in longest path, number of nodes reaching target_node :param target_node: Target node :returns: - Distances backward - Node farther, arriving to target_node - Eccentricity forward - Number of reachables nodes, arriving to target_node """ super().stbp(target_node=target_node) if self._graph.get_latest_node() is not None: raise Exception( 'Shortest path does not work with dummy nodes, give me in input a weighted graph!' ) graph_t_path = tg_utils.transform_graph(graph=self._graph) graph_t = tg.Graph(file_path=graph_t_path, is_directed=self._graph.get_is_directed(), latest_node=self._graph.get_latest_node()) self._dist_bw, self._node_farther_bw, self._eccentricity_bw, self._reachables_bw = \ self.__best_path(graph=graph_t, source_node=target_node, t_alpha=-self._t_omega, t_omega=-self._t_alpha) return self._dist_bw, self._node_farther_bw, self._eccentricity_bw, self._reachables_bw
def __nodes_order_eat(self): graph_r_path = tg_utils.reverse_edges_sort(graph=self.__graph) graph_r = tg.Graph(file_path=graph_r_path, is_directed=self.__graph.get_is_directed(), latest_node=self.__graph.get_latest_node()) self.__nodes_order(graph=graph_r, t_alpha=self.__t_alpha, t_omega=self.__t_omega)
def stbp(self, target_node: int): """ Compute: Numpy array of the latest departure times to target_node from every nodes v in V within time interval, eccentricity bw of target_node, destination node in longest path, number of nodes reaching target_node :param target_node: Target node :returns: - Distances backward - Node farther, arriving to target_node - Eccentricity forward - Number of reachables nodes, arriving to target_node """ super().stbp(target_node=target_node) graph_r_path = tg_utils.reverse_edges_sort(graph=self._graph) graph_r = tg.Graph(file_path=graph_r_path, is_directed=self._graph.get_is_directed(), latest_node=self._graph.get_latest_node()) self._dist_bw = np.full((graph_r.get_num_nodes(), ), np.NINF) self._dist_bw[target_node] = self._t_omega for line in graph_r.graph_reader(): if not line.strip(): # check if line is blank break li = line.split() u = int(li[0]) v = int(li[1]) t = int(li[2]) traversal_time = 1 if len(li) == 4: traversal_time = int(li[3]) if t >= self._t_alpha: if t + traversal_time <= self._dist_bw[v]: if t > self._dist_bw[u]: self._dist_bw[u] = t elif not graph_r.get_is_directed( ) and t + traversal_time <= self._dist_bw[u]: if t > self._dist_bw[v]: self._dist_bw[v] = t else: break # To handle dummy nodes: discard them from the distances vector if graph_r.get_latest_node() is not None: self._dist_bw = self._dist_bw[:graph_r.get_latest_node()] self._dist_bw = self._t_omega - self._dist_bw # np.NINF become np.inf (consistent with other distances) self._node_farther_bw, self._eccentricity_bw, self._reachables_bw = self._compute_ecc_reach( distances=self._dist_bw) return self._dist_bw, self._node_farther_bw, self._eccentricity_bw, self._reachables_bw
raise Exception('In input file, incorrect line: {}'.format(row)) g_path = row[0] dummy_node = None is_directed = True if len(row) > 1 and int(row[1]) >= 0: dummy_node = int(row[1]) if len(row) > 2: is_directed = False print('\nDISTANCE: ' + args.EAT_LDT_FT_ST + '\n', flush=True) graph_name = g_path.rsplit('/', 1)[1] print(args.EAT_LDT_FT_ST + ' Graph ' + graph_name, flush=True) print(args.EAT_LDT_FT_ST + ' Graph ' + graph_name + ' Dummy node: ' + str(dummy_node), flush=True) print(args.EAT_LDT_FT_ST + ' Graph ' + graph_name + ' is_directed: ' + str(is_directed), flush=True) g = tg.Graph(file_path=g_path, is_directed=is_directed, latest_node=dummy_node) a = round(math.log(g.get_n(), 2)) num_2sweep = [1, a, 2*a, 4*a] num_visits = ["4", "4*log(n)", "8*log(n)", "16*log(n)"] print(args.EAT_LDT_FT_ST + ' Graph ' + graph_name + ' Num of BFS: ' + str(num_2sweep), flush=True) if ((4 * a) * 4) >= g.get_n(): print('GRAPH ' + graph_name + ' SKIPPED: It is too small, 4*log(n) > n', flush=True) continue start_nodes = random.sample(range(g.get_n()), (4 * a) * 4) print('2SWEEP RESULTS: ', flush=True)