本文整理汇总了Python中networkx.floyd_warshall_predecessor_and_distance函数的典型用法代码示例。如果您正苦于以下问题:Python floyd_warshall_predecessor_and_distance函数的具体用法?Python floyd_warshall_predecessor_and_distance怎么用?Python floyd_warshall_predecessor_and_distance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了floyd_warshall_predecessor_and_distance函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_floyd_warshall_predecessor_and_distance
def test_floyd_warshall_predecessor_and_distance(self):
XG=nx.DiGraph()
XG.add_weighted_edges_from([('s','u',10) ,('s','x',5) ,
('u','v',1) ,('u','x',2) ,
('v','y',1) ,('x','u',3) ,
('x','v',5) ,('x','y',2) ,
('y','s',7) ,('y','v',6)])
path, dist =nx.floyd_warshall_predecessor_and_distance(XG)
assert_equal(dist['s']['v'],9)
assert_equal(path['s']['v'],'u')
assert_equal(dist,
{'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6},
'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4},
's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9},
'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1},
'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}})
GG=XG.to_undirected()
path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
assert_equal(dist['s']['v'],8)
assert_equal(path['s']['v'],'y')
G=nx.DiGraph() # no weights
G.add_edges_from([('s','u'), ('s','x'),
('u','v'), ('u','x'),
('v','y'), ('x','u'),
('x','v'), ('x','y'),
('y','s'), ('y','v')])
path, dist = nx.floyd_warshall_predecessor_and_distance(G)
assert_equal(dist['s']['v'],2)
assert_equal(path['s']['v'],'x')
示例2: test_weighted
def test_weighted(self):
XG3=nx.Graph()
XG3.add_weighted_edges_from([ [0,1,2],[1,2,12],[2,3,1],
[3,4,5],[4,5,1],[5,0,10] ])
path, dist = nx.floyd_warshall_predecessor_and_distance(XG3)
assert_equal(dist[0][3],15)
assert_equal(path[0][3],2)
示例3: programa
def programa():
Inicio = raw_input("Ingrese la ciudad de partida: ")
Final = raw_input("Ingrese la ciudad de llegada: ")
FinalAlt = Final
predecesor, distance = nx.floyd_warshall_predecessor_and_distance(g)
distancias = predecesor[Inicio].keys()
n=0
recorrido = []
recorrido.append(Final)
while n < len(distancias) :
if distancias[n] == FinalAlt:
recorrido.append(predecesor[Inicio][distancias[n]])
if predecesor[Inicio][distancias[n]] != Inicio:
FinalAlt = predecesor[Inicio][distancias[n]]
n=-1
n=n+1
print
print "Disancia: "
print distance[Inicio][Final]
print "Camino mas corto:"
n = len(recorrido) - 1
while n >= 0:
print recorrido[n]
n=n-1
print "CENTRO DEL GRAFO"
print
print FinalAlt
示例4: test_weighted2
def test_weighted2(self):
XG4=nx.Graph()
XG4.add_weighted_edges_from([ [0,1,2],[1,2,2],[2,3,1],
[3,4,1],[4,5,1],[5,6,1],
[6,7,1],[7,0,1] ])
path, dist = nx.floyd_warshall_predecessor_and_distance(XG4)
assert_equal(dist[0][2],4)
assert_equal(path[0][2],1)
示例5: test_weight_parameter
def test_weight_parameter(self):
XG4 = nx.Graph()
XG4.add_edges_from([ (0, 1, {'heavy': 2}), (1, 2, {'heavy': 2}),
(2, 3, {'heavy': 1}), (3, 4, {'heavy': 1}),
(4, 5, {'heavy': 1}), (5, 6, {'heavy': 1}),
(6, 7, {'heavy': 1}), (7, 0, {'heavy': 1}) ])
path, dist = nx.floyd_warshall_predecessor_and_distance(XG4,
weight='heavy')
assert_equal(dist[0][2], 4)
assert_equal(path[0][2], 1)
示例6: floydWarshall
def floydWarshall(self):
"""
This finds all-pairs of shortest path lengths using Floyd's algorithm
and sets self.min_path_dict to [predecessor_dict, distance_dict] where
the two dictionaries 2D dictionaries keyed on node index.
predecessor is the ordered list of visited nodes when going from node
A to node B along a shortest path.
"""
# self.min_path_dict = nx.floyd_warshall(self.G)
self.min_path_dict = nx.floyd_warshall_predecessor_and_distance(self.G)
示例7: test_zero_distance
def test_zero_distance(self):
XG=nx.DiGraph()
XG.add_weighted_edges_from([('s','u',10) ,('s','x',5) ,
('u','v',1) ,('u','x',2) ,
('v','y',1) ,('x','u',3) ,
('x','v',5) ,('x','y',2) ,
('y','s',7) ,('y','v',6)])
path, dist =nx.floyd_warshall_predecessor_and_distance(XG)
for u in XG:
assert_equal(dist[u][u], 0)
GG=XG.to_undirected()
# make sure we get lower weight
# to_undirected might choose either edge with weight 2 or weight 3
GG['u']['x']['weight']=2
path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
for u in GG:
dist[u][u] = 0
示例8: test_floyd_warshall_predecessor_and_distance
def test_floyd_warshall_predecessor_and_distance(self):
XG=nx.DiGraph()
XG.add_weighted_edges_from([('s','u',10) ,('s','x',5) ,
('u','v',1) ,('u','x',2) ,
('v','y',1) ,('x','u',3) ,
('x','v',5) ,('x','y',2) ,
('y','s',7) ,('y','v',6)])
path, dist =nx.floyd_warshall_predecessor_and_distance(XG)
assert_equal(dist['s']['v'],9)
assert_equal(path['s']['v'],'u')
assert_equal(dist,
{'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6},
'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4},
's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9},
'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1},
'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}})
GG=XG.to_undirected()
# make sure we get lower weight
# to_undirected might choose either edge with weight 2 or weight 3
GG['u']['x']['weight']=2
path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
assert_equal(dist['s']['v'],8)
# skip this test, could be alternate path s-u-v
# assert_equal(path['s']['v'],'y')
G=nx.DiGraph() # no weights
G.add_edges_from([('s','u'), ('s','x'),
('u','v'), ('u','x'),
('v','y'), ('x','u'),
('x','v'), ('x','y'),
('y','s'), ('y','v')])
path, dist = nx.floyd_warshall_predecessor_and_distance(G)
assert_equal(dist['s']['v'],2)
# skip this test, could be alternate path s-u-v
# assert_equal(path['s']['v'],'x')
# alternate interface
dist = nx.floyd_warshall(G)
assert_equal(dist['s']['v'],2)
示例9: test_reconstruct_path
def test_reconstruct_path(self):
XG = nx.DiGraph()
XG.add_weighted_edges_from([('s', 'u', 10), ('s', 'x', 5),
('u', 'v', 1), ('u', 'x', 2),
('v', 'y', 1), ('x', 'u', 3),
('x', 'v', 5), ('x', 'y', 2),
('y', 's', 7), ('y', 'v', 6)])
predecessors, _ = nx.floyd_warshall_predecessor_and_distance(XG)
path = nx.reconstruct_path('s', 'v', predecessors)
assert_equal(path, ['s', 'x', 'u', 'v'])
path = nx.reconstruct_path('s', 's', predecessors)
assert_equal(path, [])
# this part raises the keyError
nx.reconstruct_path('1', '2', predecessors)
示例10: test_cycle
def test_cycle(self):
path, dist = nx.floyd_warshall_predecessor_and_distance(nx.cycle_graph(7))
assert_equal(dist[0][3],3)
assert_equal(path[0][3],2)
assert_equal(dist[0][4],3)
示例11: test_directed_cycle_numpy
def test_directed_cycle_numpy(self):
G = nx.DiGraph()
G.add_cycle([0,1,2,3])
pred,dist = nx.floyd_warshall_predecessor_and_distance(G)
D = nx.utils.dict_to_numpy_array(dist)
assert_equal(nx.floyd_warshall_numpy(G),D)
示例12: regularizeBoundary
#.........这里部分代码省略.........
semi_perimeter = (side_1 + side_2 + base) / 2.0
temp = semi_perimeter * (semi_perimeter - side_1) * (semi_perimeter - side_2) * (semi_perimeter - base)
if temp < 0:
temp = 0
area = math.sqrt(temp)
height = (2.0 * area) / base
Ax = ordered_points[idx][0]
Ay = ordered_points[idx][1]
Bx = ordered_points[idx2][0]
By = ordered_points[idx2][1]
Cx = ordered_points[idx3][0]
Cy = ordered_points[idx3][1]
position = np.sign((Bx-Ax)*(Cy-Ay) - (By-Ay)*(Cx-Ax))
total_distance += height * position
average_distance += height
counter += 1
idx3 = (idx3 + 1) % len_ordered_points
average_distance /= counter
# if average_distance <= MIN_E_DISTANCE and total_distance >= -MIN_E_DISTANCE and total_distance < 30:
if average_distance <= MIN_E_DISTANCE:
G.add_edge(idx,idx2,weight=base)
G.add_edge(idx,str(idx2),weight=base)
E.add_edge(idx,idx2,weight=average_distance)
E.add_edge(idx,str(idx2),weight=average_distance)
fail_counter = 0
else:
fail_counter += 1
idx2 = (idx2 + 1) % len_ordered_points
F = nx.floyd_warshall_predecessor_and_distance(G)
best_route = []
count = 0
for target in xrange(0,len_ordered_points):
pointer = str(target)
route = []
route.append(pointer)
while pointer != target:
pointer = F[0][target][pointer]
route.append(pointer)
if len(route) == 7:
count += 1
if len(best_route) == 0 or len(best_route) > len(route):
best_route = copy.deepcopy(route)
#print best_route
#######################################CODE 4/29/16
#######################################CODE 4/29/16
NEIGHBOR_TRESHOLD = 6
valid_nodes = []
best_route = best_route[::-1]
best_route.pop()
for point in best_route:
point = int(point)
valid_nodes.append([point])
for i in xrange(1, (NEIGHBOR_TRESHOLD / 2) + 1):
valid_nodes[-1].append((point + i) % len_ordered_points)
valid_nodes[-1].append((point - i) % len_ordered_points)
#print "VALID", valid_nodes
示例13: approximate_line_segments
def approximate_line_segments(self, TRAINING_MIN_AREA = 60, TRAINING_MAX_AREA = 20576, MAX_FAIL_COUNTER = 20, NEIGHBOR_TRESHOLD = 6):
obj_area = len(self.FOOTPRINT_initial[self.FOOTPRINT_initial != 0])
if obj_area < TRAINING_MIN_AREA:
obj_area = TRAINING_MIN_AREA
elif obj_area > TRAINING_MAX_AREA:
obj_area = TRAINING_MAX_AREA
lin_area = [60,86,113,137,140,146,152,155,158,176,183,187,191,194,198,202,209,221,231,240,247,254,265,280,289,290,293,294,299,307,317,332,344,369,386,398,415,424,446,256,472,485,517,532,571,590,611,634,671,689,713,722,755,781,796,893,938,970,1004,1050,1050,1107,1114,1153,1164,1226,1518,1598,1762,1830,1875,1985,2045,2212,2574,2829,2895,3038,3610,4299,5088,5261,5312,5961,10616,12586,20576]
lin_min_e = [0.4,0.4,0.4,0.4,0.6,0.4,0.425,0.5,0.425,0.4,0.475,0.5,0.475,0.475,0.5,0.6,0.625,0.7,0.6,0.6,0.6,0.725,0.725,0.7,0.7,0.7,0.6,0.6,0.7,0.7,0.75,0.75,0.75,0.775,0.76,0.75,0.8,0.775,0.7,0.725,0.725,0.8,0.83,0.805,0.82,0.8,0.85,0.85,0.9,0.87,0.9,0.9,0.9,0.92,1.0,1.0,1.0,0.95,1.0,0.9,0.95,1.0,1.0,1.0,1.0,1.0,0.9,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0]
FXN_min_e = interp1d(lin_area, lin_min_e)
MIN_E_DISTANCE = FXN_min_e(obj_area)
G=nx.DiGraph()
E=nx.DiGraph()
len_ordered_points = len(self.POINTS_ordered)
for idx in xrange(0,len_ordered_points):
next_idx = (idx+1) % len_ordered_points
dist = math.hypot(self.POINTS_ordered[idx][0] - self.POINTS_ordered[next_idx][0], self.POINTS_ordered[idx][1] - self.POINTS_ordered[next_idx][1])
G.add_edge(idx,next_idx,weight=dist)
G.add_edge(idx,str(next_idx),weight=dist)
E.add_edge(idx,next_idx,weight=0)
E.add_edge(idx,str(next_idx),weight=0)
idx2 = (idx + 2) % len_ordered_points
fail_counter = 0
while((idx2 + 1) % len_ordered_points != idx and fail_counter < MAX_FAIL_COUNTER): #?
counter = 0
average_distance = 0.0
base = math.hypot(self.POINTS_ordered[idx][0] - self.POINTS_ordered[idx2][0], self.POINTS_ordered[idx][1] - self.POINTS_ordered[idx2][1])
idx3 = (idx + 1) % len_ordered_points
while(idx2 != idx3): #?
side_1 = math.hypot(self.POINTS_ordered[idx][0] - self.POINTS_ordered[idx3][0], self.POINTS_ordered[idx][1] - self.POINTS_ordered[idx3][1])
side_2 = math.hypot(self.POINTS_ordered[idx2][0] - self.POINTS_ordered[idx3][0], self.POINTS_ordered[idx2][1] - self.POINTS_ordered[idx3][1])
semi_perimeter = (side_1 + side_2 + base) / 2.0
temp = semi_perimeter * (semi_perimeter - side_1) * (semi_perimeter - side_2) * (semi_perimeter - base)
if temp < 0:
temp = 0
area = math.sqrt(temp)
height = (2.0 * area) / base
average_distance += height
counter += 1
idx3 = (idx3 + 1) % len_ordered_points
average_distance /= counter
if average_distance <= MIN_E_DISTANCE:
G.add_edge(idx,idx2,weight=base)
G.add_edge(idx,str(idx2),weight=base)
E.add_edge(idx,idx2,weight=average_distance)
E.add_edge(idx,str(idx2),weight=average_distance)
fail_counter = 0
else:
fail_counter += 1
idx2 = (idx2 + 1) % len_ordered_points
F = nx.floyd_warshall_predecessor_and_distance(G)
best_route = []
for target in xrange(0,len_ordered_points):
pointer = str(target)
route = []
route.append(pointer)
while pointer != target:
pointer = F[0][target][pointer]
route.append(pointer)
if len(best_route) == 0 or len(best_route) > len(route):
best_route = copy.deepcopy(route)
valid_nodes = []
best_route = best_route[::-1]
best_route.pop()
for point in best_route:
point = int(point)
valid_nodes.append([point])
for i in xrange(1, (NEIGHBOR_TRESHOLD / 2) + 1):
valid_nodes[-1].append((point + i) % len_ordered_points)
valid_nodes[-1].append((point - i) % len_ordered_points)
len_valid_nodes = len(valid_nodes)
simplified_E=nx.DiGraph()
for node_idx in xrange(0, len_valid_nodes):
next_node = (node_idx + 1) % len_valid_nodes
for point in valid_nodes[node_idx]:
for adj_point in valid_nodes[next_node]:
#.........这里部分代码省略.........
示例14: open
G=nx.Graph()
print "Reading category file"
with open(categoryLinksFile, 'rb') as f:
reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_ALL, quotechar ='"', escapechar='\\', doublequote=False)
for row in reader:
G.add_node(row[0])
G.add_node(row[1])
G.add_edge(row[0], row[1])
#G.add_edge(row[1], row[0])
print "Floyd-Warshall"
#path = nx.all_pairs_dijkstra_path(G)
#path = nx.all_pairs_shortest_path_length(G)
#path = nx.floyd_warshall(G)
path = nx.floyd_warshall_predecessor_and_distance(G)
#print G.nodes()
#print path
print "Generating categories for each page"
with open(pageFile, 'rb') as f:
reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_ALL, quotechar ='"', escapechar='\\', doublequote=False)
for (page, cat) in reader:
if cat not in path:
#print "Page: ", row[0], " has category ", row[1], " which is not in paths map"
continue