Sei sulla pagina 1di 8

Title : Dijkstra's algorithm

Aim : Write a program to implement Dijkstra's algorithm using c language

Theory :

It is conceived by Dutch computer scientist Edsger Dijkstra in 1959, is a graph search


algorithm that solves the single-source shortest path problem for a graph with nonnegative edge
path costs, producing a shortest path tree. This algorithm is often used in routing. An equivalent
algorithm was developed by Edward F. Moore in 1957.

For a given source vertex (node) in the graph, the algorithm finds the path with lowest
cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for
finding costs of shortest paths from a single vertex to a single destination vertex by stopping the
algorithm once the shortest path to the destination vertex has been determined. For example, if
the vertices of the graph represent cities and edge path costs represent driving distances between
pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest
route between one city and all other cities. As a result, the shortest path first is widely used in
network routing protocols, most notably IS-IS and OSPF (Open Shortest Path First).

Suppose you want to find the shortest path between two intersections on a map, a starting point
and a destination. To accomplish this, you could highlight the streets (tracing the streets with a
marker) in a certain order, until you have a route highlighted from the starting point to the
destination. The order is conceptually simple: at each iteration, create a set of intersections
consisting of every unmarked intersection that is directly connected to a marked intersection, this
will be your set of considered intersections. From that set of considered intersections, find the
closest intersection to the destination (this is the "greedy" part, as described above) and highlight
it and mark that street to that intersection, draw an arrow with the direction, then repeat. In each
stage mark just one new intersection. When you get to the destination, follow the arrows
backwards. There will be only one path back against the arrows, the shortest one.

For a given source vertex (node) in the graph, the algorithm finds the path with lowest cost (i.e. the
shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest
paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path
to the destination vertex has been determined. For example, if the vertices of the graph represent cities
and edge path costs represent driving distances between pairs of cities connected by a direct road,
Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a
result, the shortest path first is widely used in network routing protocols, most notably IS-IS and OSPF
(Open Shortest Path First).
Conclusion :
After observing the above program , we see that there are different nodes interconnected to each other ,
we are finding the shortest path , hence from the o/p we observe that by assigning the weights between
two nodes , we can easily calculate the shortest path by using dijkstras algorithm . From above data the
shortest path is path 4 which is between node F and B.

Algorithm :

Let the node we are starting be called an initial node. Let a distance of a node Y be the distance from the
initial node to it. Dijkstra's algorithm will assign some initial distance values and will try to improve them
step-by-step.
1. Assign to every node a distance value. Set it to zero for our initial node and to infinity for all other
nodes.
2. Mark all nodes as unvisited. Set initial node as current.
3. For current node, consider all its unvisited neighbours and calculate their distance (from the initial
node). For example, if current node (A) has distance of 6, and an edge connecting it with another
node (B) is 2, the distance to B through A will be 6+2=8. If this distance is less than the previously
recorded distance (infinity in the beginning, zero for the initial node), overwrite the distance.
4. When we are done considering all neighbours of the current node, mark it as visited. A visited
node will not be checked ever again; its distance recorded now is final and minimal.
5. Set the unvisited node with the smallest distance (from the initial node) as the next "current node"
and continue from step 3.

Description

Suppose you want to find the shortest path between two intersections on a map, a starting point and a
destination. To accomplish this, you could highlight the streets (tracing the streets with a marker) in a
certain order, until you have a route highlighted from the starting point to the destination. The order is
conceptually simple: at each iteration, create a set of intersections consisting of every unmarked
intersection that is directly connected to a marked intersection, this will be your set of considered
intersections. From that set of considered intersections, find the closest intersection to the destination
(this is the "greedy" part, as described above) and highlight it and mark that street to that intersection,
draw an arrow with the direction, then repeat. In each stage mark just one new intersection. When you get
to the destination, follow the arrows backwards. There will be only one path back against the arrows, the
shortest one.

Program :
#include<stdio.h>

#include<conio.h>
#include<process.h>

#include<string.h>

#include<math.h>

#define IN 99

#define N 6

int dijkstra(int cost[][N], int source, int target);

void main()

int cost[N][N],i,j,w,ch,co;

int source, target,x,y;

clrscr();

printf("\tShortest Path Algorithm(DIJKSTRA's ALGORITHM\n\n");

for(i=1;i< N;i++)

for(j=1;j< N;j++)

cost[i][j] = IN;

for(x=1;x< N;x++)

for(y=x+1;y< N;y++)

printf("Enter the weight of the path between node %d and %d: ",x,y);

scanf("%d",&w);
cost [x][y] = cost[y][x] = w;

printf("\n");

printf("\nEnter The Source:");

scanf("%d", &source);

printf("\nEnter The target");

scanf("%d", &target);

co = dijsktra(cost,source,target);

printf("\nShortest Path: %d",co);

getch();

int dijsktra(int cost[][N],int source,int target)

int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;

char path[N];

for(i=1;i< N;i++)

dist[i] = IN;

prev[i] = -1;

}
start = source;

selected[start]=1;

dist[start] = 0;

while(selected[target] ==0)

min = IN;

m = 0;

for(i=1;i< N;i++)

d = dist[start] +cost[start][i];

if(d< dist[i]&&selected[i]==0)

dist[i] = d;

prev[i] = start;

if(min>dist[i] && selected[i]==0)

min = dist[i];

m = i;

}
start = m;

selected[start] = 1;

start = target;

j = 0;

while(start != -1)

path[j++] = start+65;

start = prev[start];

path[j]='\0';

strrev(path);

printf("%s", path);

return dist[target];

}
o/p :

Potrebbero piacerti anche