算法最短路径-Dijkstra(Golang)

zhongxuan · · 2428 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

//假设有9个节点,图在代码下方,可以参考
shortTablePath存放着V0到Vx某节点的最短路径
该算法,第一次先将V0的节点连接的权值存入shortTablePath,没连接的,用MAXWEIGHT表示.

package main import ( "fmt" ) const MAXVEX int = 9 const MAXWEIGHT int = 1000 var shortTablePath = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} func main() { graph := NewGraph() var TablePathMin int //存放shortTablePath中,未遍历的最小结点的值 var Vx int //存放shortTablePath中,未遍历的最小结点的下标 var isgetPath [MAXVEX]bool //记录结点是否已经找到v0到vx的最小路径 // 获取v0这一行的权值数组 for v := 0; v < len(graph); v++ { shortTablePath[v] = graph[0][v] } shortTablePath[0] = 0 isgetPath[0] = true //遍历v1 ~ v8 for v := 1; v < len(graph); v++ { TablePathMin = MAXWEIGHT //找出shortTablePath中,未遍历的最小结点的值 for w := 0; w < len(graph); w++ { if !isgetPath[w] && shortTablePath[w] < TablePathMin { Vx = w TablePathMin = shortTablePath[w] } } isgetPath[Vx] = true for j := 0; j < len(graph); j++ { if !isgetPath[j] && TablePathMin+graph[Vx][j] < shortTablePath[j] { shortTablePath[j] = TablePathMin + graph[Vx][j] } } fmt.Println("遍历完V", v, "后:", shortTablePath) } //输出 for i := 0; i < len(shortTablePath); i++ { fmt.Println("V0到V", i, "最小路径:", shortTablePath[i]) } } func NewGraph() [MAXVEX][MAXVEX]int { var graph [MAXVEX][MAXVEX]int var v0 = [MAXVEX]int{0, 1, 5, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} var v1 = [MAXVEX]int{1, 0, 3, 7, 5, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} var v2 = [MAXVEX]int{5, 3, 0, MAXWEIGHT, 1, 7, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} var v3 = [MAXVEX]int{MAXWEIGHT, 7, MAXWEIGHT, 0, 2, MAXWEIGHT, 3, MAXWEIGHT, MAXWEIGHT} var v4 = [MAXVEX]int{MAXWEIGHT, 5, 1, 2, 0, 3, 6, 9, MAXWEIGHT} var v5 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, 7, MAXWEIGHT, 3, 0, MAXWEIGHT, 5, MAXWEIGHT} var v6 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 3, 6, MAXWEIGHT, 0, 2, 7} var v7 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 9, 5, 2, 0, 4} var v8 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 7, 4, 0} graph[0] = v0 graph[1] = v1 graph[2] = v2 graph[3] = v3 graph[4] = v4 graph[5] = v5 graph[6] = v6 graph[7] = v7 graph[8] = v8 return graph }

 

graph 图:

 

遍历完V 1 后: [0 1 4 8 6 1000 1000 1000 1000]
遍历完V 2 后: [0 1 4 8 5 11 1000 1000 1000]
遍历完V 3 后: [0 1 4 7 5 8 11 14 1000]
遍历完V 4 后: [0 1 4 7 5 8 10 14 1000]
遍历完V 5 后: [0 1 4 7 5 8 10 13 1000]
遍历完V 6 后: [0 1 4 7 5 8 10 12 17]
遍历完V 7 后: [0 1 4 7 5 8 10 12 16]
遍历完V 8 后: [0 1 4 7 5 8 10 12 16]
V0到V 0 最小路径: 0
V0到V 1 最小路径: 1
V0到V 2 最小路径: 4
V0到V 3 最小路径: 7
V0到V 4 最小路径: 5
V0到V 5 最小路径: 8
V0到V 6 最小路径: 10
V0到V 7 最小路径: 12
V0到V 8 最小路径: 16


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:zhongxuan

查看原文:算法最短路径-Dijkstra(Golang)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2428 次点击  
加入收藏 微博
被以下专栏收入,发现更多相似内容
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传