Simulate and implement Dijkstra algorithm for shortest path routing in Computer Network
Aim:-
Simulate and implement Dijkstra algorithm for shortest path routing.
Program:-
1. #include<stdio.h>
2. #include<stdlib.h>
3. #define INFINITY 999
4. #define NUM_NODES 5
5. int findmin(int cost[]);
6. int main(){
7. int net[NUM_NODES][NUM_NODES]={
8. {0,5,2,3,999},
9. {5,0,4,999,3},
10. {2,4,0,999,4},
11. {3,999,999,0,999},
12. {999,3,4,999,0}
13. };
14. printf("Dijkstra's Algorithm for shortest path routing\n");
15. printf("------------------------------------------------");
16. int root,n;
17. char c;
18. int p_list[NUM_NODES]={-1,-1,-1,-1,-1};
19. int t_list[NUM_NODES]={INFINITY,INFINITY,INFINITY,INFINITY,INFINITY};
20. int pred[NUM_NODES]={0};
21. printf("\nEnter the root node:");
22. scanf("%c",&c);
23. root=c-65;
24. t_list[root]=net[root][root];
25. pred[root]=-1;
26. while((n=findmin(t_list))!=-1){
27. printf("THe %c node has cost =%d\n",65+n,t_list[n]);
28. p_list[n]=t_list[n];
29. t_list[n]=INFINITY;
30. int i;
31. for(i=0;i<NUM_NODES;i++){
32. if(net[n][i]<INFINITY && n!=i && p_list[i]==-1){
33. int min_cost=p_list[n]+net[n][i];
34. if(min_cost<INFINITY &&(t_list[i]==-1||t_list[i]>min_cost)){
35. t_list[i]=min_cost;
36. pred[i]=n;
37. }
38. }
39. }
40.
41. }
42. int i;
43. printf("Routing table of %c \n",(65+root));
44. printf("To\t Cost\t Next\n");
45. for(i=0;i<NUM_NODES;i++){
46. char to=65+i;
47. char next=((pred[i]!=-1 && pred[i]!=root)?65+pred[i]:'-');
48. printf("%c\t%d\t%c\n",to,p_list[i],next);
49. }
50. system("pause");
51. return 0;
52. }
53. int findmin(int list[]){
54. int i,min=INFINITY,min_node=-1;
55. for(i=0;i<NUM_NODES;i++){
56. if(list[i]<min){
57. min=list[i];
58. min_node=i;
59. }
60. }
61. return min_node;
62. }
Outpute:-
Enter the root node:1
Routing table of 1
To Cost Next
A -1 A
B -1 A
C -1 A
D -1 A
E -1 A
*****Thank You*****
Comments
Post a Comment