7

所以我看到了与此类似的问题,但并不完全是我正在寻找的问题。我需要修改 Dijkstra 算法以返回顶点 S(源)和顶点 X(目标)之间的最短路径。我想我已经知道该怎么做了,但我需要一些帮助。这是我修改的伪代码。

 1  function Dijkstra(Graph, source, destination):
 2      for each vertex v in Graph:                                // Initializations
 3          dist[v] := infinity ;                                  // Unknown distance function from 
 4                                                                 // source to v
 5          previous[v] := undefined ;                             // Previous node in optimal path
 6      end for                                                    // from source
 7      
 8      dist[source] := 0 ;                                        // Distance from source to source
 9      Q := the set of all nodes in Graph ;                       // All nodes in the graph are
10                                                                 // unoptimized - thus are in Q
11      while Q is not empty:                                      // The main loop
12          u := vertex in Q with smallest distance in dist[] ;    // Start node in first case
13          remove u from Q ;
14          if dist[u] = infinity:
15              break ;                                            // all remaining vertices are
16          end if                                                 // inaccessible from source
17          
18          for each neighbor v of u:                              // where v has not yet been 
19                                                                 // removed from Q.
20              alt := dist[u] + dist_between(u, v) ;
21              if alt < dist[v]:                                  // Relax (u,v,a)
22                  dist[v] := alt ;
23                  previous[v] := u ;
24                  decrease-key v in Q;                           // Reorder v in the Queue
25              end if
26          end for
27      end while
28  return dist[destination];

代码取自维基百科并修改:http ://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

这看起来正确吗?

4

3 回答 3

4

Dijkstra's algorithm does not need to be modified, it is an all-pairs shortest path algorithm. It seems like you are trying to find the shortest path between two specific nodes - Dijkstra handles this fine.

If you want something that's designed specifically for an unweighted, undirected graph, which is what it seems like you're doing, I would suggest doing a BFS.

于 2012-11-19T05:52:11.263 回答
4

After finding the shortest-path starting from the single SOURCE, we need begin with DESTINATION to backtrack its predecessor, in order to print the path.

Print-Path(G,s,v)
{
    if(v==s)
        print s;
    else if (pi[v]==NULL)       
        print "no path from "+s+" to "+v;
    else{
        Print-Path(G,s,pi[v])
        print v;
    }
}

codes above courtesy to Introduction to Algorithm, MIT press

于 2012-11-19T06:03:22.487 回答
2

解决这个问题的最好方法是考虑从每个可达节点返回源的路径,通常用 v 表示。当你更新每个给定节点的距离时,跟踪它在通往 v 的路径上的直接后继节点。那个节点是最短距离到v树中给定节点的父节点。当您构建了该父映射时,要找到从 v 到 w 的最短路径,以相反的顺序构建路径:w, parent[w], parent[parent[w]], ...

于 2014-05-27T20:34:54.793 回答