Algorithm Shortest Path-dijkstra (Golang)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Assuming there are 9 nodes, the diagram below the code, you can refer to
Shorttablepath stores the shortest path to a node of V0 to VX
The algorithm, the first time the V0 node connected to the weight of the Shorttablepath, is not connected, expressed in maxweight.

Package Mainimport ("FMT") const Maxvex int = 9const Maxweight int = 1000var Shorttablepath = [Maxvex]int{maxweight, Maxwei GHT, Maxweight, Maxweight, Maxweight, Maxweight, Maxweight, Maxweight, Maxweight}func Main () {graph: = Newgraph () var Table pathmin int//Store Shorttablepath, the value of the smallest node not traversed var Vx int//store Shorttablepath, subscript var isgetpath for the smallest node not traversed [M Axvex]bool//Record node has found V0 to VX minimum path//Get V0 This line of weights array for V: = 0; V < len (graph); v++ {Shorttablepath[v] = graph[0][v]}shorttablepath[0] = 0isgetpath[0] = true//Traverse v1 ~ v8for V: = 1; V < len (graph); v++ {tablepathmin = maxweight//Find out Shorttablepath, the value of the smallest node not traversed for w: = 0; w < Len (graph); w++ {if!isgetpath[w] && SHORTTABLEPATH[W] < tablepathmin {Vx = Wtablepathmin = Shorttablepath[w]}}isgetpath[vx] = truefor J: = 0; J < Len (graph); J + + {if!isgetpath[j] && Tablepathmin+graph[vx][j] < Shorttablepath[j] {Shorttablepath[j] = tablepathmin + GRA Ph[vx][j]}}fmt. Println ("Traverse V", V, "after:", Shorttablepath)}//output forI: = 0; I < Len (Shorttablepath); i++ {fmt. Println ("V0 to V", I, "min path:", Shorttablepath[i])}}func newgraph () [Maxvex][maxvex]int {var graph [Maxvex][maxvex]intvar 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, MAXWE Ight}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}va R V6 = [Maxvex]int{maxweight, Maxweight, Maxweight, 3, 6, Maxweight, 0, 2, 7}var v7 = [Maxvex]int{maxweight, Maxweight, MA Xweight, Maxweight, 9, 5, 2, 0, 4}var v8 = [Maxvex]int{maxweight, Maxweight, Maxweight, Maxweight, Maxweight, MAXWEIGHT, 7 , 4, 0}graph[0] = v0graph[1] = v1graph[2] = v2graph[3] = v3graph[4] = v4graph[5] = v5graph[6] = v6graph[7] = v7graph[8] = V8return Graph}

Graph graph:

After traversing v 1: [0 1 4 8 6 1000 1000 1000 1000]
After traversing v 2: [0 1 4 8 5 11 1000 1000 1000]
After traversing v 3: [0 1 4 7 5 8 11 14 1000]
After traversing v 4: [0 1 4 7 5 8 10 14 1000]
After traversing v 5: [0 1 4 7 5 8 10 13 1000]
After traversing v 6: [0 1 4 7 5 8 10 12 17]
After traversing v 7: [0 1 4 7 5 8 10 12 16]
After traversing v 8: [0 1 4 7 5 8 10 12 16]
V0 to v 0 min. path: 0
V0 to v 1 min. path: 1
V0 to v 2 min. path: 4
V0 to v 3 min. path: 7
V0 to v 4 min. path: 5
V0 to v 5 min. path: 8
V0 to v 6 min. path: 10
V0 to v 7 min. path: 12
V0 to v 8 min. path: 16

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.