0% found this document useful (0 votes)
8 views

Shortest Path for Dijktra's Algorithm

This document presents an implementation of Dijkstra's Algorithm in C for finding the shortest path in a graph represented by an adjacency matrix. It includes the code for the algorithm, functions to find the minimum distance vertex, print the solution, and the main function to test the implementation with a sample graph. The code initializes distances, processes vertices, and updates the shortest path distances accordingly.

Uploaded by

Kedar Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Shortest Path for Dijktra's Algorithm

This document presents an implementation of Dijkstra's Algorithm in C for finding the shortest path in a graph represented by an adjacency matrix. It includes the code for the algorithm, functions to find the minimum distance vertex, print the solution, and the main function to test the implementation with a sample graph. The code initializes distances, processes vertices, and updates the shortest path distances accordingly.

Uploaded by

Kedar Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 21

Name: Archit Bhandare Roll No: 66 Batch: F3

Title: Implementation of Shortest Path for Dijktra's Algorithm in C


CODE:

#include <stdio.h>
#include <limits.h>

#define V 5 // Number of vertices in the graph

// Function to find the vertex with the minimum distance value


int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, minIndex;

for (int v = 0; v < V; v++) {


if (sptSet[v] == 0 && dist[v] <= min) {
min = dist[v];
minIndex = v;
}
}
return minIndex;
}

// Function to print the constructed distance array


void printSolution(int dist[]) {
printf("Vertex \t Distance from Source\n");
for (int i = 0; i < V; i++) {
printf("%d \t\t %d\n", i, dist[i]);
}
}

// Function to implement Dijkstra's Algorithm for a graph represented using


adjacency matrix
void dijkstra(int graph[V][V], int src) {
int dist[V]; // Output array to hold the shortest distance from src to each
vertex
int sptSet[V]; // Shortest path tree set (visited vertices)

// Initialize all distances as INFINITE and sptSet[] as false


for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = 0;
}

// Distance of the source vertex from itself is always 0


dist[src] = 0;

// Find shortest path for all vertices


for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not yet
processed
int u = minDistance(dist, sptSet);

// Mark the picked vertex as processed


sptSet[u] = 1;

// Update distance value of the adjacent vertices of the picked vertex


for (int v = 0; v < V; v++) {
// Update dist[v] only if it is not in sptSet, there is an edge from u to v,
// and the total weight of the path from src to v through u is smaller
than the current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] +
graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}

// Print the constructed distance array


printSolution(dist);
}

// Main function to test the Dijkstra's algorithm


int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 10, 0, 30, 100},
{10, 0, 50, 0, 0},
{0, 50, 0, 20, 10},
{30, 0, 20, 0, 60},
{100, 0, 10, 60, 0}
};

// Starting vertex for Dijkstra's algorithm


int src = 0;
printf("Dijkstra's Shortest Path Algorithm (Starting from Vertex %d):\n", src);
dijkstra(graph, src);

return 0;
}
OUTPUT:

You might also like