• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python networkx.floyd_warshall_predecessor_and_distance函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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')
开发者ID:adrianco,项目名称:networkx,代码行数:32,代码来源:test_dense.py


示例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)
开发者ID:aparamon,项目名称:networkx,代码行数:7,代码来源:test_dense.py


示例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
开发者ID:Sergiodeleon,项目名称:Hdt10,代码行数:28,代码来源:main.py


示例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)
开发者ID:aparamon,项目名称:networkx,代码行数:8,代码来源:test_dense.py


示例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)
开发者ID:aparamon,项目名称:networkx,代码行数:10,代码来源:test_dense.py


示例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)
开发者ID:alaindomissy,项目名称:cadnano2,代码行数:10,代码来源:staplegraph.py


示例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
开发者ID:aparamon,项目名称:networkx,代码行数:20,代码来源:test_dense.py


示例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)
开发者ID:aparamon,项目名称:networkx,代码行数:41,代码来源:test_dense.py


示例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)
开发者ID:ProgVal,项目名称:networkx,代码行数:17,代码来源:test_dense.py


示例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)
开发者ID:aparamon,项目名称:networkx,代码行数:5,代码来源:test_dense.py


示例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)
开发者ID:666888,项目名称:networkx,代码行数:6,代码来源:test_dense_numpy.py


示例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
开发者ID:ivancheesecake,项目名称:Bertud,代码行数:67,代码来源:BoundaryRegularizationV2.py


示例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]:
#.........这里部分代码省略.........
开发者ID:ivancheesecake,项目名称:Bertud,代码行数:101,代码来源:Building.py


示例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
开发者ID:AryaRajendran,项目名称:healthWikipedia,代码行数:31,代码来源:wikiGraph.py



注:本文中的networkx.floyd_warshall_predecessor_and_distance函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python networkx.freeze函数代码示例发布时间:2022-05-27
下一篇:
Python networkx.floyd_warshall_numpy函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap