Exemplo n.º 1
0
    def __best_path(self, graph: tg.Graph, source_node: int, t_alpha: int,
                    t_omega: int):
        """
        Compute: Numpy array of duration of the best paths from or to source_node depending on input graph (if it is a
        transformed link stream it compute bw stats) , within time interval, eccentricity (fw or bw) of source_node,
        destination (or source) node in longest path, number of nodes reachables from (or reaching) source_node

        :param source_node: Node to start from
        :param t_alpha: Lower bound time interval (if None it is the minimum strarting time in the graph)
        :param t_omega: Upper bound time interval (if None it is the minimum arrival time in the graph)
        """

        list_of_queues = []
        for elem in range(graph.get_num_nodes()):
            list_of_queues.append([])

        dist_fw = np.full((graph.get_num_nodes(), ), np.inf)
        dist_fw[source_node] = 0

        for line in graph.graph_reader():
            if not line.strip():  # check if line is blank
                break
            li = line.split()
            if len(li) > 3:
                raise Exception(
                    'Line ' + line +
                    ' not have correct number of fields: This FT algorithm work only with '
                    'unweighted graph, transform it using dummies nodes before!'
                )
            u = int(li[0])
            v = int(li[1])
            t = int(li[2])

            if t >= t_alpha and t + 1 <= t_omega:
                dist_fw = self.__compute_ft(u=u,
                                            v=v,
                                            t=t,
                                            source_node=source_node,
                                            list_of_queues=list_of_queues,
                                            distances=dist_fw)
                if not graph.get_is_directed():
                    dist_fw = self.__compute_ft(u=v,
                                                v=u,
                                                t=t,
                                                source_node=source_node,
                                                list_of_queues=list_of_queues,
                                                distances=dist_fw)
            elif t >= t_omega:
                break

        # To handle dummy nodes: discard them from the distances vector
        if graph.get_latest_node() is not None:
            dist_fw = dist_fw[:graph.get_latest_node()]

        node_farther, eccentricity, reachables = self._compute_ecc_reach(
            distances=dist_fw)
        return dist_fw, node_farther, eccentricity, reachables
Exemplo n.º 2
0
    def __best_path(self, graph: tg.Graph, source_node: int, t_alpha: int,
                    t_omega: int):
        """
        Compute: Numpy array of travel time of best paths from or to source_node depending on input graph
        (if it is a transformed link stream it compute bw stats), within time interval, eccentricity (fw or bw) of
        source_node, destination (or source) node in longest path, number of nodes reachables from (or reaching)
        source_node

        :param graph: Input Graph
        :param source_node: Node to start from
        :param t_alpha: Lower bound time interval (if None it is the minimum strarting time in the graph)
        :param t_omega: Upper bound time interval (if None it is the minimum arrival time in the graph)
        """

        if graph.get_latest_node() is not None:
            raise Exception(
                'Shortest path does not work with dummy nodes, give me in input a weighted graph!'
            )

        list_of_sorted_lists = []
        for elem in range(graph.get_num_nodes()):
            list_of_sorted_lists.append([])

        dist_fw = np.full((graph.get_num_nodes(), ), np.inf)
        dist_fw[source_node] = 0

        for line in graph.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])

            if len(li) == 4:
                traversal_time = int(li[3])
            else:
                traversal_time = 1

            if t >= t_alpha and t + traversal_time <= t_omega:
                dist_fw = self.__compute_st(
                    u=u,
                    v=v,
                    t=t,
                    traversal_time=traversal_time,
                    source_node=source_node,
                    list_of_sorted_lists=list_of_sorted_lists,
                    distances=dist_fw)
                if not self._graph.get_is_directed():
                    dist_fw = self.__compute_st(
                        u=v,
                        v=u,
                        t=t,
                        traversal_time=traversal_time,
                        source_node=source_node,
                        list_of_sorted_lists=list_of_sorted_lists,
                        distances=dist_fw)
            elif t >= t_omega:
                break
        node_farther, eccentricity, reachables = self._compute_ecc_reach(
            distances=dist_fw)
        return dist_fw, node_farther, eccentricity, reachables