所以,我已經看到了類似的問題,這但不是之間有很大正是我要找的。我需要修改Dijkstra算法以返回頂點S(源)和頂點X(目標)之間的最短路徑。我想我已經想出了該做什麼,但我想要一些幫助。這是我修改過的僞代碼。修改Dijkstra算法得到最短路徑兩個節點
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
這是否看起來是正確的?
爲什麼你需要修改它?這正是它所做的。當您將所有邊權重設置爲1. –
這是我已經修改的代碼。所以如果它的效果很好。 – csnate
此外,由於Dijkstra中的頂點選擇是貪婪的,只要您獲得「u = destination」,就可以打破循環。 –