dongsu7049 2013-11-11 07:22
浏览 95
已采纳

开始,Dijkstra:打印出路径,而不仅仅是计算最短距离

Go, Dijkstra : print out the path, not just calculate the shortest distance.

http://play.golang.org/p/A2jnzKcbWD

I was able to find the shortest distance using Dijkstra algorithm, maybe not. The code can be found here.

But it would be useless if I can't print out the path. With a lot of pointers going on, I can't figure out how to print out the final path that takes the least amount of weights.

In short, how do I not only find the shortest distance, but also print out the shortest path on this given code?

The link is here:

http://play.golang.org/p/A2jnzKcbWD

And the snippet of the code is below:

const MAXWEIGHT = 1000000

type MinDistanceFromSource map[*Vertex]int

func (G *Graph) Dijks(StartSource, TargetSource *Vertex) MinDistanceFromSource {
  D := make(MinDistanceFromSource)
  for _, vertex := range G.VertexArray {
    D[vertex] = MAXWEIGHT
  }
  D[StartSource] = 0

  for edge := range StartSource.GetAdEdg() {
    D[edge.Destination] = edge.Weight
  }
  CalculateD(StartSource, TargetSource, D)
  return D
}

func CalculateD(StartSource, TargetSource *Vertex, D MinDistanceFromSource) {
  for edge := range StartSource.GetAdEdg() {
    if D[edge.Destination] > D[edge.Source]+edge.Weight {
      D[edge.Destination] = D[edge.Source] + edge.Weight
    } else if D[edge.Destination] < D[edge.Source]+edge.Weight {
      continue
    }
    CalculateD(edge.Destination, TargetSource, D)
  }
}

I did something with array to see what is being updated.

http://play.golang.org/p/bRXYjnIGxy

This gives ms

   [A->D D->E E->F F->T B->E E->D E->F F->T]
  • 写回答

2条回答 默认 最新

  • doulu4534 2013-11-11 07:24
    关注

    When you adjust the new path distance here

       if D[edge.Destination] > D[edge.Source]+edge.Weight {
          D[edge.Destination] = D[edge.Source] + edge.Weight
    

    Set some array element (say, P for "parent") to point that you have come to Destination from Source.

    P[edge.Destination] = edge.Source
    

    After the algorithm ends, in this array each vertex will have its predecessor on the path leading from the starting vertex.

    PS. OK, not with arrays and indices ...

    Add a new field Prev to the Vertex:

    type Vertex struct {
        Id      string
        Visited bool
        AdjEdge []*Edge
        Prev *Vertex
    }
    

    When adjusting distance:

    if D[edge.Destination] > D[edge.Source]+edge.Weight {
        D[edge.Destination] = D[edge.Source] + edge.Weight
        edge.Destination.Prev = edge.Source
    

    And when you display the results:

    for vertex1, distance1 := range distmap1 {
        fmt.Println(vertex1.Id, "=", distance1)
        if vertex1.Prev != nil {
            fmt.Println (vertex1.Id, " -> ", vertex1.Prev.Id)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题