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

Exp 6

The document outlines a program that implements Dijkstra's algorithm to find the shortest paths from a given vertex in a weighted connected graph. It describes the steps involved in the algorithm, including initializing distances and updating paths, and provides a C code implementation. The code defines a graph, initializes it, and executes the Dijkstra function to calculate and print the shortest distances to other vertices.

Uploaded by

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

Exp 6

The document outlines a program that implements Dijkstra's algorithm to find the shortest paths from a given vertex in a weighted connected graph. It describes the steps involved in the algorithm, including initializing distances and updating paths, and provides a C code implementation. The code defines a graph, initializes it, and executes the Dijkstra function to calculate and print the shortest distances to other vertices.

Uploaded by

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

From a given vertex in a weighted connected graph, develop a program to find the shortestpaths

to other vertices using Dijkstra’s algorithm.

Aim:

To find a shortest path to other vertex in a weighted connected graph using Dijkstra’salgorithm.

Algorithm:

Step1:Create a set short Path to store vertices that come in the way of the shortest path tree.

Step2:Initialize all distance values as INFINITE and assign distance values as 0 for source vertex so
that it is picked first.

Step3:Loop until all vertices of the graph are in the short Path.

#include <stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10

void Dijkstra(int Graph[MAX][MAX], int n, int start);

void Dijkstra(int Graph[MAX][MAX], int n, int start) {


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

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


for (j = 0; j < n; j++)
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];

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


distance[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}

distance[start] = 0;
visited[start] = 1;
count = 1;

while (count < n - 1) {


mindistance = INFINITY;

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


if (distance[i] < mindistance && !visited[i]) {
mindistance = distance[i];
nextnode = i;
}

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++;
}

// Printing the distance


for (i = 0; i < n; i++)
if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
}
int main() {
int Graph[MAX][MAX]={{0,4,0,0,0,0,0,8,0},
{4,0,8,0,0,0,0,11,0 },
{0,8,0,7, 0,4,0,0,2 },
{0,0,7,0,9,14,0,0,0 },
{0,0,0,9,0,10,0,0,0 },
{0,0,4,14,10,0,2,0,0},
{0,0,0,0, 0,2,0,1,6 },
{8,11,0,0,0,0,1,0,7 },
{0,0,2, 0,0,0,6, 7,0} }, i, j, n, u;
n = 7;
u = 0;
clrscr();
Dijkstra(Graph, n, u);
getch();
return 0;
}

You might also like