Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write the program in C language ID is your own host follow by your own port to c

ID: 3701104 • Letter: W

Question

write the program in C language

ID is your own host follow by your own port to connect to other host and port.

The main thing about this program is to create a simulated link state protocol to see who is connected to who and find the shortest path from 1 end node to another end node

messages along these routes. You must use a link-state protocol, and use Dijkstra's algorithm to compute routes. Requirements You should write one program, called router, which you will run multiple times on some combination of computers. A real router would have physical links to a few neighbors. Instead, your simulated router will exchange UDP packets with a few neighbor programs. a Your programs should not talk directly to non-neighbors:"*m just as on a real network, all communication to non- neighbors should be via neighbors. ck Interface Requirements Your program should accept arguments as follows: myhost%./router id port host! porti host2 port? .. an ( 5 The id is a number between 0 and 19 inclusive that Zo ho should be unique over all programs in a particular simulated network. The port is the number of a UDP port on which your program should send and receive packets. anutout clust

Explanation / Answer

#include<stdio.h>

#include<conio.h>

#define INFINITY 9999

#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()

{

    int G[MAX][MAX],i,j,n,u;

    printf("Enter no. of vertices:");

    scanf("%d",&n);

    printf(" Enter the adjacency matrix: ");

    for(i=0;i<n;i++)

        for(j=0;j<n;j++)

            scanf("%d",&G[i][j]);

    printf(" Enter the starting node:");

    scanf("%d",&u);

    dijkstra(G,n,u);

    return 0;

}

void dijkstra(int G[MAX][MAX],int n,int startnode)

{

    int cost[MAX][MAX],distance[MAX],pred[MAX];

    int visited[MAX],count,mindistance,nextnode,i,j;

    //pred[] stores the predecessor of each node

    //count gives the number of nodes seen so far

    //create the cost matrix

    for(i=0;i<n;i++)

        for(j=0;j<n;j++)

            if(G[i][j]==0)

                cost[i][j]=INFINITY;

            else

                cost[i][j]=G[i][j];

    //initialize pred[],distance[] and visited[]

    for(i=0;i<n;i++)

    {

        distance[i]=cost[startnode][i];

        pred[i]=startnode;

        visited[i]=0;

    }

    distance[startnode]=0;

    visited[startnode]=1;

    count=1;

    while(count<n-1)

    {

        mindistance=INFINITY;

        //nextnode gives the node at minimum distance

        for(i=0;i<n;i++)

            if(distance[i]<mindistance&&!visited[i])

            {

                mindistance=distance[i];

                nextnode=i;

            }

            //check if a better path exists through nextnode            

            visited[nextnode]=1;

            for(i=0;i<n;i++)

                if(!visited[i])

                    if(mindistance+cost[nextnode][i]<distance[i])

                    {

                        distance[i]=mindistance+cost[nextnode][i];

                        pred[i]=nextnode;

                    }

        count++;

    }

    //print the path and distance of each node

    for(i=0;i<n;i++)

        if(i!=startnode)

        {

            printf(" Distance of node%d=%d",i,distance[i]);

            printf(" Path=%d",i);

            j=i;

            do

            {

                j=pred[j];

                printf("<-%d",j);

            }while(j!=startnode);

    }