You are on page 1of 4

DESIGN AND SIMULATION LAB1

M.VINOD KUMAR (11EC64Q01) DOS: 26/08/11

PROBLEM STATEMENT: In a given network the shortest path between any two nodes
in the network is to be found using DIJKSTRAS ALOGRITM. The cost of the shortest path and the path traced is to be displayed as output.

DESIGN: The Dijkstras algorithm computes the shortest path between any two nodes in a
given network when all the edges have non-negative costs. The procedure for finding the shortest path is described below. 1. Dijkstras algorithm starts from a source node and in each iteration add another node to the shortest path. 2. Initially no paths are known, so all nodes are labeled with infinity except source node. 3. The node with least cost is selected from source is selected as the next working node, and it is made permanent. 4. The iterations are continued till the destination node is reached. 5. The cost of the destination node is displayed as the shortest path.

IMPLEMENTATION:

Get the no of nodes and distance between them in the network. Ask the user to enter the source and destination node. Initially source node status made as permanent and its distance as zero and working node also made itself. Next loop is executed for no of nodes times, in each iteration a node with least distance is selected from source and it is made permanent. Finally the distance from source to destination and path traversed is displayed.

RESULTS: For the network shown below the shortest path between the source node 1
and destination node 6 is found.
2

4 2
1

2 1
6

3
3

2 3
5

CODE:
#include <stdio.h> #define INFINITY 9999 #define MAX 100 int main(void) { int i,j,N,B,node1,node2,dist,src,dest,worknode,min,minnode,x; int distmat[MAX][MAX]; struct nodefmt { char status; int srcdist; int prevnode; } ; struct nodefmt node[MAX]; for(i=0;i<MAX;i++) { node[i].status='T'; node[i].srcdist=INFINITY; node[i].prevnode=MAX+1; for(j=0;j<MAX;j++) { distmat[i][j]=INFINITY; } } printf("\nHow many nodes are there?"); scanf("%d",&N); printf("\nNumber of Branches ?"); scanf("%d",&B); printf("\nEnter the distance between the nodes "); for(i=0;i<B;i++) { printf("\n( Node1 Node2 Distance) for branch %d\t" , i+1); scanf("%d%d%d",&node1,&node2,&dist); distmat[node1-1][node2-1]=dist; distmat[node2-1][node1-1]=dist; } printf("\nEnter source node and destination node\t"); scanf("%d%d",&src,&dest); node[src-1].status='P'; node[src-1].srcdist=0; node[src-1].prevnode=src-1; worknode=src-1;

for(j=0;j<N;j++) { for(i=0;i<N;i++) { if(distmat[i][worknode]!=INFINITY && i!=worknode && node[i].status=='T' ) { if(node[i].srcdist>(node[worknode].srcdist+distmat[i] [worknode])) { node[i].srcdist=node[worknode].srcdist+distmat[i][worknode]; node[i].prevnode=worknode; } } } min=INFINITY; for(i=0;i<N;i++) { if(node[i].status=='T') if(node[i].srcdist<min) { min=node[i].srcdist; minnode=i; } } node[minnode].status='P'; worknode=minnode; } j=dest-1; printf("%d",dest); printf("\nThe Shortest Path Is \n"); printf("%d",dest); for(i=0;j!=src-1;i++) { printf(" - %d",((node[j].prevnode)+1)); j=node[j].prevnode; } printf("\n\nShortest Distance is %d",node[dest-1].srcdist); getchar(); return 0;

OUTPUT:
How many nodes are there? 6 Number of Branches? 8 Enter the distance between the nodes Node1 Node2 Distance for branch 1 1 3 3 Node1 Node2 Distance for branch 2 1 2 4 Node1 Node2 Distance for branch 3 3 5 3 Node1 Node2 Distance for branch 4 2 4 4 Node1 Node2 Distance for branch 5 4 6 2 Node1 Node2 Distance for branch 6 1 4 2 Node1 Node2 Distance for branch 7 5 6 2 Node1 Node2 Distance for branch 8 2 5 1 Enter source node and destination node 1 6 The Shortest Path Is 6 - 4 1 .Shortest Distance is 4 How many nodes are there? 6 Number of Branches? 8 Enter the distance between the nodes Node1 Node2 Distance for branch 1 1 3 3 Node1 Node2 Distance for branch 2 1 2 4 Node1 Node2 Distance for branch 3 3 5 3 Node1 Node2 Distance for branch 4 2 4 -10 Node1 Node2 Distance for branch 5 4 6 2 Node1 Node2 Distance for branch 6 1 4 2 Node1 Node2 Distance for branch 7 5 6 2 Node1 Node2 Distance for branch 8 2 5 1 Enter source node and destination node 1 6 The Shortest Path Is 6-4-1 Shortest Distance is 4 In this case shortest path should be 1-2-4-6 as there is a negative cost of -10.This is the disadvantage of Dijkstras algorithm

You might also like