def test_shortest_path_BF_on_digraph_with_negwcycle(self): #test negative weight cycle detection gr = generate_fixture_digraph_neg_weight_cycle() try: shortest_path_bellman_ford(gr, 1) except NegativeWeightCycleError: pass else: self.fail()
def GetFarthestNode(self, gr, node): """node is start node""" # Remember: weights are negative distance = minmax.shortest_path_bellman_ford(gr, node)[1] # Find the farthest node, which is end of track min_key = None for key, value in distance.iteritems(): if min_key is None or value < distance[min_key]: min_key = key return min_key
def constructGPrime(G,V_i,delta,m): print "Constructing G'..." # PRE-COMPUTE shortest paths shortest_paths = {} for v in G: shortest_paths[v] = shortest_path_bellman_ford(G,v) #print 'shortest_paths: %s' % shortest_paths # NODES G_p = graph() G_p.DIRECTED = True for p_i in V_i: for v in V_i[p_i]: G_p.add_node("%s.%s" % (p_i,v), attrs=[('weight',V_i[p_i][v])]) G_p.add_node(0,attrs=[('weight',0)]) #source G_p.add_node(-1,attrs=[('weight',0)]) #sink print "NODES: %s" % G_p.nodes() # EDGES for i in V_i: for j in V_i[i]: v_ij = "%s.%s" % (i,j) shortest_path_j = shortest_paths[j][-1] #print 'shortest_path_%s: %s' % (j,str(shortest_path_j)) d = 0 while d <= m + 1: for l in shortest_path_j: v_idl = "%s.%s" % (i+d,l) if v_ij == v_idl: continue d_p = shortest_path_j[l] if d_p <= m + 1: try: G_p.add_edge((v_ij,v_idl), attrs=[('weight', max(d,d_p)*delta)]) except: pass d += 1 try: G_p.add_edge((0, v_ij), attrs=[('weight', (i-1)*delta)]) # from source G_p.add_edge((v_ij, -1), attrs=[('weight', (len(V_i)-i)*delta)]) # to sink except: pass print "EDGES: %s" % G_p.edges() print "... Done." return G_p
def ComputeLongestPath(self, gr, parent_node): parent_node = self.FindParentNode(gr) farthest_node = self.GetFarthestNode(gr, parent_node) gr = self.NegateGraph(gr) st, distance = minmax.shortest_path_bellman_ford(gr, parent_node) gr = self.NegateGraph(gr) max_distance = distance[farthest_node] # Then swim back to the parent node. Record the path. node_list = [farthest_node] i = 0 while parent_node not in node_list: node_list.append(st[node_list[-1]]) i += 1 if i > 10000: raise ValueError() # Grab doublets node_list2 = [] for node1 in node_list: for node2 in gr.nodes(): z1, x1, Q1 = node1 z2, x2, Q2 = node2 dx = math.fabs(x1 - x2) if z1 == z2 and math.fabs(dx - bar_width) < distance_threshold: node_list2.append(node2) for node in node_list + node_list2: if node: gr.del_node(node) node_list = [x for x in node_list if x is not None] return node_list, max_distance, gr
def test_shortest_path_BF_on_digraph(self): #testing correctness on the fixture gr = generate_fixture_digraph() pre,dist = shortest_path_bellman_ford(gr, 1) assert pre == {1: None, 2: 3, 3: 4, 4: 1, 5: 2} \ and dist == {1: 0, 2: 2, 3: 4, 4: 7, 5: -2}
def test_shortest_path_BF_on_empty_digraph(self): pre, dist = shortest_path_bellman_ford(digraph(), 1) assert pre == {1:None} and dist == {1:0}
def test_shortest_path_BF_on_unconnected_graph(self): gr = generate_fixture_digraph_unconnected() pre,dist = shortest_path_bellman_ford(gr, 100) assert pre == {200: 100, 100: None} and \ dist == {200: 2, 100: 0}
def test_shortest_path_BF_on_unconnected_graph(self): gr = generate_fixture_digraph_unconnected() pre, dist = shortest_path_bellman_ford(gr, 100) assert pre == {200: 100, 100: None} and \ dist == {200: 2, 100: 0}
def test_shortest_path_BF_on_digraph(self): #testing correctness on the fixture gr = generate_fixture_digraph() pre, dist = shortest_path_bellman_ford(gr, 1) assert pre == {1: None, 2: 3, 3: 4, 4: 1, 5: 2} \ and dist == {1: 0, 2: 2, 3: 4, 4: 7, 5: -2}
def test_shortest_path_BF_on_empty_digraph(self): pre, dist = shortest_path_bellman_ford(digraph(), 1) assert pre == {1: None} and dist == {1: 0}
return metroGraph nodeFile = "nodes.txt" if __name__ == '__main__': nodFl = open(nodeFile) subGraph = BuildMetroGraph(nodFl) dists = {}; prevs = {}; minTreeFl = open("minDstTree.txt", "wb") for node in subGraph.nodes(): prev,dist = shortest_path_bellman_ford(subGraph, node) prevs[node] = prev dists[node] = dist for tnode in prev.keys(): if tnode == node: continue; minTreeFl.write("\"%s\",\"%s\",\"%s\",%f\n" % (node, tnode, prev[tnode], dist[tnode])) if len(sys.argv) >= 3: src = sys.argv[1] dst = sys.argv[2] print "Distance %s --> %s : %s" % (src, dst, dists[dst][src]) prevNode = src prev2Node = prevs[dst][prevNode]; while prev2Node != dst: print "%s --> %s" % (prevNode, prev2Node);
print "Breadth first search" print "Spanning tree" print bfs print "level-based ordering" print bfsord print "\n" print "Accessibility" access= accessibility(gr) print access print "Mutual accessibility" maccess= mutual_accessibility(gr) print maccess print "\n" print "Traversal" trav= traversal(gr, 'A', 'pre') for t in trav: print t print "Transitive Edges" tredges= transitive_edges(gr) print tredges print "\n" print "shortest_path_bellman_ford" short= shortest_path_bellman_ford(gr, 'A') print short print "shortest_path" shrt= shortest_path(gr, 'A') print shrt