Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views3 pages

Dijk Stra Algorithm

The document provides a C/C++ program that implements Dijkstra's algorithm to find the shortest paths from a specified source vertex in a weighted connected graph to all other vertices. It includes functions for inputting the graph's adjacency matrix and calculating the shortest distances. The program outputs the shortest path lengths from the source to each vertex in the graph.

Uploaded by

shilpa shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Dijk Stra Algorithm

The document provides a C/C++ program that implements Dijkstra's algorithm to find the shortest paths from a specified source vertex in a weighted connected graph to all other vertices. It includes functions for inputting the graph's adjacency matrix and calculating the shortest distances. The program outputs the shortest path lengths from the source to each vertex in the graph.

Uploaded by

shilpa shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

Design and implement C/C++Program to find shortest paths from a given vertex in a
weighted connected graph to other vertices using Dijkstra's algorithm.
#include<stdio.h>
void dij(int, int[20][20],int[20],int[20],int);
void main() {
int i,j,n,visited[20],source,cost[20][20],d[20];
printf("Enter no. of vertices: ");
scanf("%d",&n);
printf("Enter the cost adjacency matrix\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d",&cost[i][j]);
}
}
printf("\nEnter the source node:");
scanf("%d", &source);
dij(source, cost, visited, d, n);
for (i = 1; i <= n; i++) {
if(i != source)
printf("\nShortest path from %d to %d is %d",source, i, d[i]);
}
}
void dij(int source,int cost[20][20],int visited[20],int d[20],int n){
int i, j, min, u, w;
for(i=1;i<=n;i++){
visited[i] = 0;
d[i] =cost[source][i];
}
visited[source]=1;
d[source]=0;
for(j=2;j<=n;j++){
min = 999;
for(i=1;i<=n;i++){
if (!visited[i]) {
if(d[i]<min){
min = d[i];
u =i;
}
}
}
visited[u]=1;
for(w =1; w<=n; w++) {
if(cost[u][w]!=999 && visited[w]==0){
if (d[w] > cost[u][w] + d[u])
d[w]=cost[u][w]+d[u];
}
}
}
}

OUTPUT:

You might also like