Esempio n. 1
0
 def test_interlacing_edges(self):
     """Test interlacing edge function for correctness."""
     a = [i for i in "ABCDEFZ"]
     b = [i for i in "AGCBIZ"]
     a2 = [1, 2, 3]
     a3 = [1, 2]
     tuples = [(a, b, [("C", "B")]), (a2, a2, [(1, 2), (2, 3)]), (a2, a3, [(1, 2)])]
     for x, y, result in tuples:
         self.assertEqual(interlacing_edges(x, y), result)
Esempio n. 2
0
 def test_interlacing_edges(self):
     '''Test interlacing edge function for correctness.'''
     a = [i for i in 'ABCDEFZ']
     b = [i for i in 'AGCBIZ']
     a2 = [1, 2, 3]
     a3 = [1, 2]
     tuples = [(a, b, [('C', 'B')]), (a2, a2, [(1, 2), (2, 3)]),
               (a2, a3, [(1, 2)])]
     for x, y, result in tuples:
         self.assertEqual(interlacing_edges(x, y), result)
Esempio n. 3
0
def grouped_shortest_pair(g, shortest_path, shortest_path_2):
    '''Find shortest pair of paths of two interlacing original paths.

    Last step of the optimal edge-disjoint and vertex-disjoint shortest-pair
    algorithms.

    @param g: original NetworkX Graph or DiGraph
    @param shortest_path: list of path nodes
    @param shortest_path_2: list of path nodes
    @return path1: first non-interlacing shortest path
    @return path2: second non-interlacing shortest path
    '''
    #shortest_path = int(shortest_path)
    #shortest_path2 = int(shortest_path2)
    src = shortest_path[0]
    dst = shortest_path[-1]
    assert src == shortest_path_2[0]
    assert dst == shortest_path_2[-1]

    g3 = nx.Graph()
    g3.add_path(shortest_path)
    g3.add_path(shortest_path_2)
    # copy edges on path:
    for a, b in edges_on_path(shortest_path):
        g3[a][b]['weight'] = g[a][b]['weight']
    g3.add_path(shortest_path_2)
    for a, b in edges_on_path(shortest_path_2):
        g3[a][b]['weight'] = g[a][b]['weight']
    for a, b in interlacing_edges(shortest_path, shortest_path_2):
        g3.remove_edge(a, b)

    # Find a path through graph and remove edges used.
    path1 = BFS(g3, src, dst)
    for a, b in edges_on_path(path1):
        g3.remove_edge(a, b)
    path2 = BFS(g3, src, dst)
    for a, b in edges_on_path(path2):
        g3.remove_edge(a, b)
    assert g3.number_of_edges() == 0
    return path1, path2
Esempio n. 4
0
def grouped_shortest_pair(g, shortest_path, shortest_path_2):
    '''Find shortest pair of paths of two interlacing original paths.

    Last step of the optimal edge-disjoint and vertex-disjoint shortest-pair
    algorithms.

    @param g: original NetworkX Graph or DiGraph
    @param shortest_path: list of path nodes
    @param shortest_path_2: list of path nodes
    @return path1: first non-interlacing shortest path
    @return path2: second non-interlacing shortest path
    '''
    #shortest_path = int(shortest_path)
    #shortest_path2 = int(shortest_path2)
    src = shortest_path[0]
    dst = shortest_path[-1]
    assert src == shortest_path_2[0]
    assert dst == shortest_path_2[-1]

    g3 = nx.Graph()
    g3.add_path(shortest_path)
    g3.add_path(shortest_path_2)
    # copy edges on path:
    for a, b in edges_on_path(shortest_path):
        g3[a][b]['weight'] = g[a][b]['weight']
    g3.add_path(shortest_path_2)
    for a, b in edges_on_path(shortest_path_2):
        g3[a][b]['weight'] = g[a][b]['weight']
    for a, b in interlacing_edges(shortest_path, shortest_path_2):
        g3.remove_edge(a, b)

    # Find a path through graph and remove edges used.
    path1 = BFS(g3, src, dst)
    for a, b in edges_on_path(path1):
        g3.remove_edge(a, b)
    path2 = BFS(g3, src, dst)
    for a, b in edges_on_path(path2):
        g3.remove_edge(a, b)
    assert g3.number_of_edges() == 0
    return path1, path2