求出最短路径,要过程,用Dijkstra算法。。。 100

 我来答
whalebill
推荐于2018-03-01 · TA获得超过560个赞
知道小有建树答主
回答量:317
采纳率:0%
帮助的人:166万
展开全部
从v1开始遍历
v2 = 2;
v3 = 5;
v2较小所以跳到v2
v3 = 4;

v4 = 6;
v5 = 8;
v3较小所以跳到v3
v4 = 5;
v6 = 7;
v4较小所以跳到v4
v6 = 6;
v7 = 9;
v6较小所以跳到v6
v7 = 8;

所以最后结果v1 -> v7最短路径为v1->v2->v3->v4->v6->v7,最短路径长度为8
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友1b66f0d
2016-04-21 · TA获得超过411个赞
知道小有建树答主
回答量:327
采纳率:0%
帮助的人:240万
展开全部

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
#import collections
import heapq

#==============================================================================
# class Graph:
#     def __init__(self):
#         self.edges = {}
#         
#     def neighbors(self,id):
#         return self.edges[id]
#==============================================================================

#   原型
class GraphWithWeights():
    def __init__(self):
        #super.__init__(self)
        self.edges_weights = {}
    def cost(self, from_node, to_node):
        return self.edges_weights.get(from_node).get(to_node)
    def neighbors(self,node):
        return list(self.edges_weights.get(node).keys())

class PriorityQueue:
    def __init__(self):
        self.elements = []
#   检查优先序列是否为空
    def empty(self):
        return len(self.elements) == 0
#   依据优先权重添加一个成员    
    def put(self, item, priority):
        heapq.heappush(self.elements, (priority, item))
#   取出成员    
    def get(self):
        return heapq.heappop(self.elements)[1]

#   根据图设置值,顶点,边,权重,        
target_graph = GraphWithWeights()
target_graph.edges_weights = {
    'V1':{'V2':2, 'V3':5},
    'V2':{'V1':2, 'V3':2, 'V4':4, 'V5':6},
    'V3':{'V1':5, 'V2':2, 'V4':1, 'V6':3},
    'V4':{'V2':4, 'V3':1, 'V5':4, 'V6':1,'V7':4},
    'V5':{'V2':6, 'V4':4, 'V7':1},
    'V6':{'V3':3, 'V4':1, 'V7':2},
    'V7':{'V4':4, 'V5':1, 'V6':2}
}

#   dijkstra 算法
def dijkstra_search(graph, start, goal):
    #pass
    frontier = PriorityQueue()
    frontier.put(start, 0)
    came_from = {}
    came_from[start] = None
    cost_so_far = {}
    cost_so_far[start] = 0
    
    while not frontier.empty():
        current = frontier.get()
        
        if current == goal:
            break
        for next in graph.neighbors(current):
            new_cost = cost_so_far[current] + graph.cost(current,next)
            if next not in cost_so_far or new_cost < cost_so_far[next]:
                cost_so_far[next]=new_cost
                priority = new_cost
                frontier.put(next, priority)
                came_from[next] = current
    return came_from, cost_so_far


def reconstruct_path(came_from, start, goal):
    current = goal
    path = [current]
    while current != start:
        current = came_from[current]
        path.append(current)
    path.reverse()
    return path

c, f = dijkstra_search(target_graph, 'V1', 'V7')
path=reconstruct_path(c,'V1','V7')
print("最短路径为:{}".format(path))
#==============================================================================
# 最短路径为:['V1', 'V2', 'V3', 'V4', 'V6', 'V7']
#==============================================================================


已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式