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
('G', 'H', 8), ('H', 'Z', 4), ('I', 'Z', 5) ]) # Figure 3.16a, pg 74 # Unfortunately, this graph is pictured without weights. Presumably the # author intended for the central path to have unit weights and the other # paths to have much larger weights. graph_fig_3_16_a = nx.Graph() graph_fig_3_16_a.add_path([i for i in 'AJKLMZIHGPNABCDEFZ']) edges_to_add = ['JC', 'KD', 'LE', 'LF', 'GD', 'EH', 'IF'] for e in edges_to_add: graph_fig_3_16_a.add_edge(e[0], e[1]) set_weights(graph_fig_3_16_a, 5.0) for src, dst in edges_on_path([i for i in 'ABCDEFZ']): graph_fig_3_16_a[src][dst]['weight'] = 1.0 # Figure 3.13a mod 1, pg 78 graph_fig_3_13_a_mod_1 = graph_fig_3_13_a.copy() graph_fig_3_13_a_mod_1['G']['H']['weight'] = 3.0 # Figure 3.13a mod 2, pg 78 graph_fig_3_13_a_mod_2 = graph_fig_3_13_a.copy() graph_fig_3_13_a_mod_2['G']['C']['weight'] = 3.0 graph_fig_3_13_a_mod_2['G']['D']['weight'] = 3.0 graph_fig_3_13_a_mod_2['H']['Z']['weight'] = 1.0 def compare_path_lists(test, one, two, g = None, total = None): '''Compare two path lists.
('E', 'F', 1), ('E', 'H', 2), ('E', 'I', 3), ('F', 'Z', 1), ('F', 'I', 3), ('G', 'H', 8), ('H', 'Z', 4), ('I', 'Z', 5)]) # Figure 3.16a, pg 74 # Unfortunately, this graph is pictured without weights. Presumably the # author intended for the central path to have unit weights and the other # paths to have much larger weights. graph_fig_3_16_a = nx.Graph() graph_fig_3_16_a.add_path([i for i in 'AJKLMZIHGPNABCDEFZ']) edges_to_add = ['JC', 'KD', 'LE', 'LF', 'GD', 'EH', 'IF'] for e in edges_to_add: graph_fig_3_16_a.add_edge(e[0], e[1]) set_weights(graph_fig_3_16_a, 5.0) for src, dst in edges_on_path([i for i in 'ABCDEFZ']): graph_fig_3_16_a[src][dst]['weight'] = 1.0 # Figure 3.13a mod 1, pg 78 graph_fig_3_13_a_mod_1 = graph_fig_3_13_a.copy() graph_fig_3_13_a_mod_1['G']['H']['weight'] = 3.0 # Figure 3.13a mod 2, pg 78 graph_fig_3_13_a_mod_2 = graph_fig_3_13_a.copy() graph_fig_3_13_a_mod_2['G']['C']['weight'] = 3.0 graph_fig_3_13_a_mod_2['G']['D']['weight'] = 3.0 graph_fig_3_13_a_mod_2['H']['Z']['weight'] = 1.0 def compare_path_lists(test, one, two, g=None, total=None): '''Compare two path lists.