def __nodes_order(self, graph: tg.Graph, t_alpha: int, t_omega: int):
        """
        :return: A list of tuples (node, time) sorted to be processed
        N.B. This ordering work only if all travel times are equals (as in the case of dummy nodes)
        """
        self.__list_nodes = []
        num_nodes = graph.get_n()
        nodes = np.full(num_nodes, True)

        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])
            traversal_time = 1
            if len(li) == 4:
                traversal_time = int(li[3])

            if v < num_nodes and nodes[
                    v] and t >= t_alpha and t + traversal_time <= t_omega:
                nodes[v] = False
                self.__list_nodes.append((v, t + traversal_time))

            if not graph.get_is_directed():
                if u < num_nodes and nodes[
                        u] and t >= t_alpha and t + traversal_time <= t_omega:
                    nodes[u] = False
                    self.__list_nodes.append((u, t + traversal_time))
Exemple #2
0
    def __init__(self, graph: tg.Graph, t_alpha=None, t_omega=None):
        """
        :param graph: Input Graph (sorted in non-decreasing order with respect to the edge starting times)
        :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)
        """
        self._name = None
        self._graph = graph
        self._source = None
        self._target = None
        self._eccentricity_fw = None
        self._eccentricity_bw = None
        self._node_farther_fw = None
        self._node_farther_bw = None
        self._reachables_fw = None
        self._reachables_bw = None
        self._dist_fw = None
        self._dist_bw = None
        self._diameter = None
        self._reachable_pairs = None

        if t_alpha is None:
            t_alpha, _ = graph.get_time_interval()
        if t_omega is None:
            _, t_omega = graph.get_time_interval()
        self._t_alpha, self._t_omega = t_alpha, t_omega
Exemple #3
0
    def __init__(self, graph: tg.Graph, pivots, t_alpha=None, t_omega=None):
        """
        :param graph: Temporal Graph (sorted in non-decreasing order with respect to the edge starting times)
        :param pivots: List of tuples (node, time)
        :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 t_alpha is None:
            t_alpha, _ = graph.get_time_interval()
        if t_omega is None:
            _, t_omega = graph.get_time_interval()
        self._t_alpha, self._t_omega = t_alpha, t_omega

        for p in pivots:
            if t_omega < p[1] < t_alpha:
                raise Exception(
                    "Pivot times should be between t_alpha and t_omega")

        self.__graph = graph
        self.__pivots = pivots
        self.__t_alpha = t_alpha
        self.__t_omega = t_omega

        self.__is_inA = None  # Will be a matrix [num_pivots x num_nodes] of boolean
        self.__is_inB = None  # Will be a matrix [num_pivots x num_nodes] of boolean
        self.__reachable_pairs = None
        self.__distinct_inA = None
        self.__distinct_inB = None

        self.__pivot_diameter_st = None
        self.__num_visits_st = None

        self.__pivot_diameter_ft = None
        self.__num_visits_ft = None

        self.__pivot_diameter_eat = None
        self.__num_visits_eat = None

        self.__pivot_diameter_ldt = None
        self.__num_visits_ldt = None

        self.__dist_pivot_fw = [
        ]  # List of lists of tuples (distance, nodeIndex)
        self.__dist_pivot_bw = [
        ]  # List of lists of tuples (distance, nodeIndex)
        self.__next_node_inB = [
        ]  # List of indices for locate next node in B to process relative to each landmark
        self.__next_node_inA = [
        ]  # List of indices for locate next node in A to process relative to each landmark
        self.__to_be_considered_inB = [
        ]  # List of bool, if one element is False, that node has already been considered
        self.__to_be_considered_inA = [
        ]  # List of bool, if one element is False, that node has already been considered
    def __init__(self,
                 graph: tg.Graph,
                 start_node=None,
                 t_alpha=None,
                 t_omega=None):
        """
        :param graph: Temporal Graph (sorted in non-decreasing order with respect to the edge starting times)
        :param start_node: Starting node
        :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)
        """
        self.__graph = graph
        self.__t_alpha = t_alpha
        self.__t_omega = t_omega
        self.__lb_eat = None
        self.__source_eat = None
        self.__destination_eat = None
        self.__lb_ldt = None
        self.__source_ldt = None
        self.__destinantion_ldt = None
        self.__lb_ft = None
        self.__source_ft = None
        self.__destinantion_ft = None
        self.__lb_st = None
        self.__source_st = None
        self.__destinantion_st = None

        if start_node is None:
            _, self.__r = graph.get_max_deg_out(n=1)
            self.__r = self.__r[0]
        else:
            self.__r = start_node
def sort_graph(graph: tg.Graph):
    """
    Sort input Graph and save it in 'sorted_graphs' directory.

    :return: Path of sorted graph
    """
    txt_path = graph.get_file_path()
    directory, txt_name = txt_path.rsplit('/', 1)
    directory += '/'

    output_dir = directory + 'sorted_graphs/'
    create_dir(output_dir)

    output_path = output_dir + txt_name + '.sor'
    if not check_file_exists(output_path):
        print('Loading graph ' + txt_name + '...')
        m = np.loadtxt(txt_path, dtype=int, delimiter=' ', ndmin=2)
        print('Sorting graph ' + txt_name + '...')
        m = m[m[:, 2].argsort()]
        print('Saving sorted graph...')
        np.savetxt(output_path, m, fmt='%i', delimiter=' ')
        print('Saved! \n')
    else:
        print('File ' + output_path.rsplit('/', 1)[1] + ' already exist in ' +
              output_dir + '\n')
    return output_path
    def __init__(self, graph: tg.Graph, t_alpha=None, t_omega=None):
        """
        :param graph: Temporal Graph (sorted in non-decreasing order with respect to the edge starting times)
        :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)
        """
        self.__graph = graph
        self.__diam_eat = None
        self.__diam_ldt = None
        self.__visits_eat = None
        self.__visits_ldt = None
        self.__list_nodes = []

        if t_alpha is None:
            t_alpha, _ = graph.get_time_interval()
        if t_omega is None:
            _, t_omega = graph.get_time_interval()
        self.__t_alpha, self.__t_omega = t_alpha, t_omega
Exemple #7
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
def create_outdir(graph: tg.Graph, directory_name: str, txt_out_name: str):
    """
    Create output directory

    :param graph: A given Graph
    :param directory_name: Out directory name
    :param txt_out_name: Out txt file name
    :returns:
        - Input path of txt
        - Output path of out txt
    """
    txt_path = graph.get_file_path()
    directory, txt_name = txt_path.rsplit('/', 1)
    directory += '/'

    out_directory = directory + directory_name
    if out_directory[-1] != '/':
        out_directory += '/'

    create_dir(out_directory)

    output_path = out_directory + txt_name + '-' + txt_out_name
    return txt_path, output_path
Exemple #9
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