Advertisement
Bewin

DVR

Apr 9th, 2025
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include<stdio.h>
  2. struct node
  3. {
  4.     unsigned dist[20];
  5.     unsigned from[20];
  6. }rt[10];
  7. int main()
  8. {
  9.     int costmat[20][20];
  10.     int nodes,i,j,k,count=0;
  11.     printf("\nEnter the number of nodes : ");
  12.     scanf("%d",&nodes);//Enter the nodes
  13.     printf("\nEnter the cost matrix :\n");
  14.     for(i=0;i<nodes;i++)
  15.     {
  16.         for(j=0;j<nodes;j++)
  17.         {
  18.             scanf("%d",&costmat[i][j]);
  19.             costmat[i][i]=0;
  20.             rt[i].dist[j]=costmat[i][j];//initialise the distance equal to cost matrix
  21.             rt[i].from[j]=j;
  22.         }
  23.     }
  24.         do
  25.         {
  26.             count=0;
  27.             for(i=0;i<nodes;i++)
  28.                 for(j=0;j<nodes;j++)
  29.                     for(k=0;k<nodes;k++)
  30.                         if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
  31.                         {//We calculate the minimum distance
  32.                              rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
  33.                                  rt[i].from[j]=k;
  34.                              count++;
  35.                     }
  36.         }while(count!=0);
  37.         for(i=0;i<nodes;i++)
  38.         {
  39.             printf("\n\n Routing Table For Router %d\n",i+1);
  40.             for(j=0;j<nodes;j++)
  41.             {
  42.                 printf("\t\nnode %d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]);
  43.             }
  44.         }
  45.     printf("\n\n");
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement