Shortest
Shortest
AIM:
Implementation of shortest path routing algorithm using a C program
(DIJKSTRA’S).
SOFTWARE REQUIRED:
Online C Compiler
THEORY:
Let us begin our study of feasible routing algorithms with a technique that is
widely used in many forms because it is simple and easy to understand.
The idea is to build a graph of the subnet, with each node of the graph
representing a router and each arc of the graph representing a communication
line (often called a link).
To choose a route between a given pair of routers, the algorithm just finds the
shortest path between them on the graph.
The concept of the shortest path deserves some explanation. One way of
measuring path length is the number of hops.
By changing the weighting function, the algorithm would then compute the
''shortest'' path measured according to any one of a number of criteria or to a
combination of criteria.
Several algorithms for computing the shortest path between two nodes of a
graph are known.
This one is due to Dijkstra (1959). Each node is labeled (in parentheses) with its
distance from the source node along the best-known path. Initially, no paths are
known, so all nodes are labeled with infinity.
As the algorithm proceeds and paths are found, the labels may
change,reflecting better paths.
49
A label may be either tentative or permanent. Initially, all labels are tentative.
When it is discovered that a label represents the shortest possible path from the
source to that node, it is made permanent and never changed thereafter .
#include<stdio.h>
int main()
{
int n,AB,BC,CD,BE,EF,FH,AG,GH,HD,EG,P1,P2;
printf("Enter the number of nodes in subnet:");
scanf("%d",&n);
printf("Computation of shortest path connecting node A & node D is as follows\n");
printf("Enter the distance between Node A and Node B:");
scanf("%d",&AB);
printf("Enter the distance between Node B and Node C:");
scanf("%d",&BC);
printf("Enter the distance between Node C and Node D:");
scanf("%d",&CD);
printf("Enter the distance between Node B and Node E:");
scanf("%d",&BE);
printf("Enter the distance between Node E and Node F:");
scanf("%d",&EF);
50
printf("Enter the distance between Node F and Node H:");
scanf("%d",&FH);
printf("Enter the distance between Node A and Node G:");
scanf("%d",&AG);
printf("Enter the distance between Node G and Node H:");
scanf("%d",&GH);
printf("Enter the distance between Node H and Node D:");
scanf("%d",&HD);
printf("Enter the distance between Node E and Node G:");
scanf("%d",&EG);
printf("Computation of path from node A to node D is path P1 or path P2\n");
printf("Path P1:(A->B)+(B->E)+(E->G)+(G->H)+(H->D) \n");
P1=AB+BE+EG+GH+HD;
printf("Path P1= %d\n",P1);
printf("Path P2:(A->B)+(B->E)+(E->F)+(F->H)+(H->D) \n");
P2=AB+BE+EF+FH+HD;
printf("Path P2= %d\n",P2);
if(P1<P2)
printf("Path connecting node A and node D is P1=%d",P1);
else
printf("Path connecting node A and node D is P2=%d",P2);
}