Professional Documents
Culture Documents
Source - 11.5 KB
Source - 11.5 KB
Source - 11.5 KB
5 KB
Introduction
Dijkstra's algorithm, named after its discoverer, Dutch computer scientist Edsger Dijkstra,
is a greedy algorithm that solves the single-source shortest path problem for a directed
graph with non negative edge weights. For example, if the vertices (nodes) of the graph
represent cities and edge weights represent driving distances between pairs of cities
connected by a direct road, Dijkstra's algorithm can be used to find the shortest route
between two cities. Also, this algorithm can be used for shortest path to destination in
traffic network.
We are in A node and the problem is we must go other nodes with minimum cost. L[,] is
our distances between pairs of nodes array.
Collapse
int[,] L ={
{-1, 5, -1, -1, -1, 3, -1, -1},
{ 5, -1, 2, -1, -1, -1, 3, -1},
{-1, 2, -1, 6, -1, -1, -1, 10},
{-1, -1, 6, -1, 3, -1, -1, -1},
{-1, -1, -1, 3, -1, 8, -1, 5},
{ 3, -1, -1, -1, 8, -1, 7, -1},
{-1, 3, -1, -1, -1, 7, -1, 2},
{-1, -1, 10, -1, 5, -1, 2, -1}
};
D[] shows the cost array. We will write the shortest cost in D array. C[] shows our nodes.
Pseudocode
Collapse
function Dijkstra(L[1..n, 1..n]) : array [2..n]
array D[2..n]
set C
C <- {2, 3, 4, 5, 6, …, n}
for i <- 2 to n
D[i] <- L[1,i]
repeat n - 2 times
v <- C // minimum D[v] extract to C
v <- C - {v}
for each w in C do
D[w] <- min(D[w], D[v] + L[v,w])
return D
C[]-> -1,-1,-1,-1,-1,-1,-1,-1