Shortest Path Problem

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Shortest Path Problem: Dijkstra's

Algorithm

dsdws, 13 Aug 2007 CPOL


3.56 (53 votes
)
Rate this: vote 1vote 2vote 3vote 4vote 5

Using Dijkstra's Algorithm for finding shortest path problem


Download source - 11.5 KB

Introduction
Dijkstra's algorithm, named after its discoverer, Dutch computer
scientist Edsger Dijkstra, is a greedy algorithm that solves the singlesource 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.

Using the Code


I will explain this algorithm over an example.

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.
Hide Copy Code
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
Hide Copy Code
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

It is shown below how the algorithm steps are worked:

D[]-> -1, 5,-1,-1,-1, 3,-1,-1

D[]-> -1, 5,-1,-1,11, 3,10,-1

C[]-> -1, 1, 2, 3, 4, 5, 6, 7

C[]-> -1, 1, 2, 3, 4,-1, 6, 7

D[]-> -1, 5, 7,-1,11, 3, 8,-1

D[]-> -1, 5, 7,13,11, 3, 8,17

C[]-> -1,-1, 2, 3, 4,-1, 6, 7

C[]-> -1,-1,-1, 3, 4,-1, 6, 7

D[]-> -1, 5, 7,13,11, 3, 8,10

D[]-> -1, 5, 7,13,11, 3, 8,10

C[]-> -1,-1,-1, 3, 4,-1,-1, 7

C[]-> -1,-1,-1, 3, 4,-1,-1,-1

D[]-> -1, 5, 7,13,11, 3, 8, 8

C[]-> -1,-1,-1,-1,-1,-1,-1,-1

Using the Code


Hide Shrink
class Dijkstra
{
private int rank = 0;
private int[,] L;
private int[] C;
public int[] D;
private int trank = 0;
public Dijkstra(int paramRank,int [,]paramArray)
{
L = new int[paramRank, paramRank];
C = new int[paramRank];
D = new int[paramRank];
rank = paramRank;
for (int i = 0; i < rank; i++)
{
for (int j = 0; j < rank; j++) {
L[i, j] = paramArray[i, j];
}
}
for (int i
{
C[i] =
}
C[0] = -1;
for (int i
D[i] =

= 0; i < rank; i++)


i;
= 1; i < rank; i++)
L[0, i];

}
public void DijkstraSolving()
{
int minValue = Int32.MaxValue;
int minNode = 0;
for (int i = 0; i < rank; i++)
{
if (C[i] == -1)
continue;
if (D[i] > 0 && D[i] < minValue)
{
minValue = D[i];
minNode = i;
}
}
C[minNode] = -1;
for (int i = 0; i < rank; i++)
{
if (L[minNode, i] < 0)
continue;
if (D[i] < 0) {
D[i] = minValue + L[minNode, i];
continue;

Copy Code

}
if ((D[minNode] + L[minNode, i]) < D[i])
D[i] = minValue+ L[minNode, i];
}
}
public void Run()
{
for (trank = 1; trank >rank; trank++)
{
DijkstraSolving();
Console.WriteLine("iteration" + trank);
for (int i = 0; i < rank; i++)
Console.Write(D[i] + " ");
Console.WriteLine("");
for (int i = 0; i < rank; i++)
Console.Write(C[i] + " ");
Console.WriteLine("");
}
}
}

For bug reports and suggestions, feel free to contact me


at [email protected].

#include <stdio.h>
#include <conio.h>
#define INF 9999
void main( )
{
int arr[4][4] ;
int cost[4][4] = {
7,
7,
0,
4,

5,
0,
3,
0,

0,
0,
0,
1,

0,
2,
0,
0

} ;
int i, j, k, n = 4 ;
clrscr( ) ;
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0; j < n ; j++ )
{
if ( cost[i][j] == 0 )
arr[i][j] = INF ;
else
arr[i][j] = cost[i][j] ;
}
}
printf ( "Adjacency matrix of cost of edges:\n" ) ;
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0; j < n ; j++ )
printf ( "%d\t", arr[i][j] ) ;
printf ( "\n" ) ;
}
for ( k = 0 ; k < n ; k++ )
{
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
if ( arr[i][j] > arr[i][k] + arr[k][j] )
arr[i][j] = arr[i][k] + arr[k][j];
}
}
}
printf ( "\nAdjacency matrix of lowest cost between the vertices:\n" ) ;
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0; j < n ; j++ )
printf ( "%d\t", arr[i][j] ) ;
printf ( "\n" ) ;
}

getch( ) ;
}

#include<stdio.h>
#include<conio.h>
void main()
{
int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;
clrscr();
printf("enter the cost matrix\n");
for(i=1;i<=5;i++)
for(j=1;j<=5;j++)
scanf("%d",&a[i][j]);
printf("enter number of paths\n");
scanf("%d",&p);
printf("enter possible paths\n");
for(i=1;i<=p;i++)
for(j=1;j<=5;j++)
scanf("%d",&path[i][j]);
for(i=1;i<=p;i++)
{
t[i]=0;
stp=st;
for(j=1;j<=5;j++)
{
edp=path[i][j+1];
t[i]=t[i]+a[stp][edp];
if(edp==ed)
break;
else
stp=edp;
}
}
min=t[st];index=st;
for(i=1;i<=p;i++)
{
if(min>t[i])
{
min=t[i];
index=i;
}
}
printf("minimum cost %d",min);
printf("\n minimum cost path ");
for(i=1;i<=5;i++)
{
printf("--> %d",path[index][i]);
if(path[index][i]==ed)
break;
}

getch();
}

OUTPUT:
enter the cost matrix :
01420
00023
00030
00005
00000
enter number of paths : 4
enter possible paths :
12450
12500
14500
13450
minimum cost : 4
minimum cost path :
1>2>5

You might also like