def test_all_pairs_shortest_path_length(self): ans = dict(nx.shortest_path_length(self.cycle)) assert ans[0] == {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1} assert ans == dict(nx.all_pairs_shortest_path_length(self.cycle)) ans = dict(nx.shortest_path_length(self.grid)) assert ans[1][16] == 6 # now with weights ans = dict(nx.shortest_path_length(self.cycle, weight="weight")) assert ans[0] == {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1} assert ans == dict(nx.all_pairs_dijkstra_path_length(self.cycle)) ans = dict(nx.shortest_path_length(self.grid, weight="weight")) assert ans[1][16] == 6 # weights and method specified ans = dict( nx.shortest_path_length(self.cycle, weight="weight", method="dijkstra")) assert ans[0] == {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1} assert ans == dict(nx.all_pairs_dijkstra_path_length(self.cycle)) ans = dict( nx.shortest_path_length(self.cycle, weight="weight", method="bellman-ford")) assert ans[0] == {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1} assert ans == dict(nx.all_pairs_bellman_ford_path_length(self.cycle))
def test_all_pairs_shortest_path_length(self): ans = dict(nx.shortest_path_length(self.cycle)) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_shortest_path_length(self.cycle))) ans = dict(nx.shortest_path_length(self.grid)) assert_equal(ans[1][16], 6) # now with weights ans = dict(nx.shortest_path_length(self.cycle, weight='weight')) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_dijkstra_path_length(self.cycle))) ans = dict(nx.shortest_path_length(self.grid, weight='weight')) assert_equal(ans[1][16], 6) # weights and method specified ans = dict( nx.shortest_path_length(self.cycle, weight='weight', method='dijkstra')) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_dijkstra_path_length(self.cycle))) ans = dict( nx.shortest_path_length(self.cycle, weight='weight', method='bellman-ford')) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_bellman_ford_path_length(self.cycle)))
def netDiameter(map): G = gStruct(map) diameter = 0 # 22网络直径-T fordPath = dict(nx.all_pairs_bellman_ford_path_length(G)) # print(fordPath) for i in fordPath.keys(): for j in fordPath[i]: if diameter < fordPath[i][j]: diameter = fordPath[i][j] return diameter
def calc_shortest_path(Ga): G = Ga.copy() update_weight(G) f1 = open("../data/length.file", "wb") f2 = open("../data/path.file", "wb") def dict2df(data): return pd.DataFrame.from_dict(data, orient='index') length = dict(nx.all_pairs_bellman_ford_path_length(G)) path = dict(nx.all_pairs_bellman_ford_path(G)) pickle.dump(length, f1) pickle.dump(path, f2)
def Calculate_travel_distances(self): self.distances = {} self.BigM_dis = 1000000 # depend on locations MaxX*MaxY self.shortest_paths = list(nx.all_pairs_shortest_path_length(self.G)) for i, j in it.permutations(self.G.nodes, 2): if (i, j) in self.G.edges or (j, i) in self.G.edges: pos1 = self.G.node[i]['location'] pos2 = self.G.node[j]['location'] dis = get_distance(pos1, pos2) self.G.edges[min([i, j]), max([i, j])]['Travel_time'] = dis Dis = nx.all_pairs_bellman_ford_path_length(self.G, weight='Travel_time') self.distances = dict([((i, a[0]), a[1]) for i, k in Dis for a in k.items()])
def test_all_pairs_shortest_path_length(self): ans = dict(nx.shortest_path_length(self.cycle)) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_shortest_path_length(self.cycle))) ans = dict(nx.shortest_path_length(self.grid)) assert_equal(ans[1][16], 6) # now with weights ans = dict(nx.shortest_path_length(self.cycle, weight='weight')) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_dijkstra_path_length(self.cycle))) ans = dict(nx.shortest_path_length(self.grid, weight='weight')) assert_equal(ans[1][16], 6) # weights and method specified ans = dict(nx.shortest_path_length(self.cycle, weight='weight', method='dijkstra')) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_dijkstra_path_length(self.cycle))) ans = dict(nx.shortest_path_length(self.cycle, weight='weight', method='bellman-ford')) assert_equal(ans[0], {0: 0, 1: 1, 2: 2, 3: 3, 4: 3, 5: 2, 6: 1}) assert_equal(ans, dict(nx.all_pairs_bellman_ford_path_length(self.cycle)))
def shortest_path_length(G, source=None, target=None, weight=None, method='dijkstra'): """Compute shortest path lengths in the graph. Parameters ---------- G : NetworkX graph source : node, optional Starting node for path. If not specified, compute shortest path lengths using all nodes as source nodes. target : node, optional Ending node for path. If not specified, compute shortest path lengths using all nodes as target nodes. weight : None or string, optional (default = None) If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1. method : string, optional (default = 'dijkstra') The algorithm to use to compute the path length. Supported options: 'dijkstra', 'bellman-ford'. Other inputs produce a ValueError. If `weight` is None, unweighted graph methods are used, and this suggestion is ignored. Returns ------- length: int or iterator If the source and target are both specified, return the length of the shortest path from the source to the target. If only the source is specified, return a dict keyed by target to the shortest path length from the source to that target. If only the target is specified, return a dict keyed by source to the shortest path length from that source to the target. If neither the source nor target are specified, return an iterator over (source, dictionary) where dictionary is keyed by target to shortest path length from source to that target. Raises ------ NodeNotFound If `source` is not in `G`. NetworkXNoPath If no path exists between source and target. ValueError If `method` is not among the supported options. Examples -------- >>> G = nx.path_graph(5) >>> nx.shortest_path_length(G, source=0, target=4) 4 >>> p = nx.shortest_path_length(G, source=0) # target not specified >>> p[4] 4 >>> p = nx.shortest_path_length(G, target=4) # source not specified >>> p[0] 4 >>> p = dict(nx.shortest_path_length(G)) # source,target not specified >>> p[0][4] 4 Notes ----- The length of the path is always 1 less than the number of nodes involved in the path since the length measures the number of edges followed. For digraphs this returns the shortest directed path length. To find path lengths in the reverse direction use G.reverse(copy=False) first to flip the edge orientation. See Also -------- all_pairs_shortest_path_length() all_pairs_dijkstra_path_length() all_pairs_bellman_ford_path_length() single_source_shortest_path_length() single_source_dijkstra_path_length() single_source_bellman_ford_path_length() """ if method not in ('dijkstra', 'bellman-ford'): # so we don't need to check in each branch later raise ValueError('method not supported: {}'.format(method)) method = 'unweighted' if weight is None else method if source is None: if target is None: # Find paths between all pairs. if method == 'unweighted': paths = nx.all_pairs_shortest_path_length(G) elif method == 'dijkstra': paths = nx.all_pairs_dijkstra_path_length(G, weight=weight) else: # method == 'bellman-ford': paths = nx.all_pairs_bellman_ford_path_length(G, weight=weight) else: # Find paths from all nodes co-accessible to the target. with nx.utils.reversed(G): if method == 'unweighted': # We need to exhaust the iterator as Graph needs # to be reversed. path_length = nx.single_source_shortest_path_length paths = path_length(G, target) elif method == 'dijkstra': path_length = nx.single_source_dijkstra_path_length paths = path_length(G, target, weight=weight) else: # method == 'bellman-ford': path_length = nx.single_source_bellman_ford_path_length paths = path_length(G, target, weight=weight) else: if target is None: # Find paths to all nodes accessible from the source. if method == 'unweighted': paths = nx.single_source_shortest_path_length(G, source) elif method == 'dijkstra': path_length = nx.single_source_dijkstra_path_length paths = path_length(G, source, weight=weight) else: # method == 'bellman-ford': path_length = nx.single_source_bellman_ford_path_length paths = path_length(G, source, weight=weight) else: # Find shortest source-target path. if method == 'unweighted': p = nx.bidirectional_shortest_path(G, source, target) paths = len(p) - 1 elif method == 'dijkstra': paths = nx.dijkstra_path_length(G, source, target, weight) else: # method == 'bellman-ford': paths = nx.bellman_ford_path_length(G, source, target, weight) return paths
edgeLen = (math.sqrt((e.source.y - e.junction.y)**2 + (e.source.x - e.junction.x)**2) / dotSize) if edgeLen > maximumEdgeLen: maximumEdgeLen = edgeLen if edgeLen < minimumEdgeLen and edgeLen != 0: minimumEdgeLen = edgeLen totalEdgeLength += edgeLen edgeCount = len(emanatedEdges) averageEdgeLen = totalEdgeLength / edgeCount #CALCULATE SPANNING RATIO endTime = time.time() emSpanningRatio = 0 if calcSpanningRatio: shortestPaths = dict(nx.all_pairs_bellman_ford_path_length(emGraph)) for p1 in points: p1.degree = len(p1.connectedRotations) if p1.degree > maximumDegree: maximumDegree = p1.degree if p1.degree < minimumDegree: minimumDegree = p1.degree averageDegree += p1.degree if calcSpanningRatio: for p2 in points: if p1 != p2: dist = math.sqrt((p1.y - p2.y)**2 + (p1.x - p2.x)**2) if dist != 0: dilation = shortestPaths[(p1.x, p1.y)][(p2.x, p2.y)] / dist
(e.source.x - e.junction.x)**2) / dotSize) if edgeLen > maximumEdgeLen: maximumEdgeLen = edgeLen if edgeLen < minimumEdgeLen and edgeLen != 0: minimumEdgeLen = edgeLen totalEdgeLength += edgeLen edgeCount = len(emanatedEdges) averageEdgeLen = totalEdgeLength / edgeCount #CALCULATE SPANNING RATIO endTime = time.time() emSpanningRatio = 0 if calcSpanningRatio: shortestPaths = dict( nx.all_pairs_bellman_ford_path_length(emGraph)) for p1 in points: p1.degree = len(p1.connectedRotations) if p1.degree > maximumDegree: maximumDegree = p1.degree if p1.degree < minimumDegree: minimumDegree = p1.degree averageDegree += p1.degree if calcSpanningRatio: for p2 in points: if p1 != p2: dist = math.sqrt((p1.y - p2.y)**2 + (p1.x - p2.x)**2) dilation = shortestPaths[(p1.x, p1.y)][(p2.x, p2.y)] / dist if emSpanningRatio < dilation:
G.edges(data=True) nx.draw_networkx(G, pos=None, with_labels=True, font_weight='bold') plt.savefig("Results/inputGraph.png") EPSILON = 1e-7 # to prevent divided by zero weight = None edge_list = None method = "OTD" verbose = False print(nx.is_weighted(G)) print(nx.is_negatively_weighted(G)) #print(nx.negative_edge_cycle(G)) length = dict(nx.all_pairs_dijkstra_path_length(G, weight='weight')) hop_distance = dict(nx.all_pairs_bellman_ford_path_length(G, weight='weight')) #print(length) if not edge_list: edge_list = G.edges() print(edge_list) args = [(G, source, target, length, hop_distance, verbose, method) for source, target in edge_list] args scalar = 0 file1 = open("Results/Ricci_Curvature.txt", "w") file1.close() if method == 'OTD': #optimal transport distance for arg in args: