Dijkstra’s algorithm for single sources shortest path:
1.
Algorithm Shortest Paths (v, cost,
dist, n)
2.
// dis [j], 1<= j <= n, is set to the length of the
shortest
3.
// path from vertex v to vertex j
in a digraph G with n
4.
// vertices. dist [v] is set to
zero. G is represented by its
5.
// cost adjacency matrix cost [1:n,
1:n]
6.
{
7.
for (int i=1; i<=n; i++)
8.
{ // Initialize S
9.
S [i] = false;
10. dist
[i] = cost [v][i];
11. }
12. S
[v] = true;
13. dist
[v] = 0.0; // Put v in S.
14. for
(num = 2; num < n; num ++)
15. {
16. //
determine n-1 paths from v.
17. choose
u from among those vertices not
18. in
S such that dist [u] is minimum;
19. S[u]
= true; // Put u in S.
20. for
(w = 1; w <= n; w++)
21. // Update distances.
22. if
((S[w] = false) && (dist[w] > dist[u] + cost[u][w])) then dist[w] =
dist[u] + cost[u][w];
23. }
24. }
Tnx admin
ReplyDeleteWelcome
Delete