Program 6 Dijkstra Algorithm
Program 6 Dijkstra Algorithm
Step 1 :
Create cost matrix C[ ][ ] from adjacency matrix adj[ ][ ].
C[i][j] is the cost of going from vertex i to vertex j.
If there is no edge between vertices i and j then C[i][j] is infinity.
Step 4:
1. Create the distance matrix, by storing the cost of vertices from vertex no. 0 to n-
1 from the source vertex 0.
for(i=1;i<n;i++)
distance[i]=cost[0][i];
2. Initially, distance of source vertex is taken as 0. i.e. distance[0]=0;
Step 5 : for(i=1;i<n;i++)
1. Choose a vertex w, such that distance[w] is minimum and visited[w] is 0. Mark
visited[w] as
2. Recalculate the shortest distance of remaining vertices from the source.
3. Only, the vertices not marked as 1 in array visited[ ] should be considered for
recalculation of distance. i.e. for each vertex v
if(visited[v]==0)
distance[v]=min(distance[v],distance[w]+cost[w][v])
C Program:
#include<stdio.h>
#define INFINITY 9999
#define MAX 10
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
while(count<n-1)
{
mindistance=INFINITY;
2. What is routing?
Routing is the process of selecting a path for traffic in a network or between or across
multiple networks.
6. What is packetizing?
The process of encapsulating the data received from upper layers of the network(also
called as payload) in a network layer packet at the source and decapsulating the payload
from the network layer packet at the destination is known as packetizing.
Connectionless service is used in the network system to transfer data from one end to
another end without creating any connection. So it does not require establishing a
connection before sending the data from the sender to the receiver. It is not a reliable
network service because it does not guarantee the transfer of data packets to the receiver,
and data packets can be received in any order to the receiver. Therefore we can say that
the data packet does not follow a defined path. In connectionless service, the transmitted
data packet is not received by the receiver due to network congestion, and the data may be
lost.