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

Assignment 6

The document contains two implementations of graph algorithms in C: Dijkstra's algorithm for finding the shortest path from a source vertex in a weighted graph, and the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices. The Dijkstra's algorithm implementation includes a function to find the minimum distance vertex and prints the shortest distances from the source. The Floyd-Warshall implementation initializes a distance matrix and updates it to compute the shortest paths, printing the results at the end.

Uploaded by

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

Assignment 6

The document contains two implementations of graph algorithms in C: Dijkstra's algorithm for finding the shortest path from a source vertex in a weighted graph, and the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices. The Dijkstra's algorithm implementation includes a function to find the minimum distance vertex and prints the shortest distances from the source. The Floyd-Warshall implementation initializes a distance matrix and updates it to compute the shortest paths, printing the results at the end.

Uploaded by

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

#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, min_index;

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

if (sptSet[v] == 0 && dist[v] <= min) {

min = dist[v], min_index = v;

return min_index;

// Function to print the solution

void printSolution(int dist[]) {

printf("Vertex \t Distance from Source\n");

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

printf("%d \t %d\n", i, dist[i]);

// Dijkstra's algorithm function

void dijkstra(int graph[V][V], int src) {

int dist[V]; // Output array to hold the shortest distance from src

int sptSet[V]; // sptSet[i] will be true if vertex i is included in the shortest path tree
// Initialize distances to INFINITY and sptSet[] as false

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

dist[i] = INT_MAX;

sptSet[i] = 0;

// Distance of source vertex from itself is always 0

dist[src] = 0;

// Find shortest path for all vertices

for (int count = 0; count < V - 1; count++) {

int u = minDistance(dist, sptSet); // Pick the minimum distance vertex

sptSet[u] = 1;

// Update dist[v] only if it's not in sptSet, there's an edge from u to v,

// and the total weight of path from src to v through u is smaller than dist[v]

for (int v = 0; v < V; 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];

}
}

printSolution(dist);

int main() {
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}

};

printf("Graph Representation (Adjacency Matrix):\n");

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

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

printf("%3d ", graph[i][j]);

printf("\n");

printf("\nRunning Dijkstra's Algorithm from Source Vertex 0:\n\n");

dijkstra(graph, 0); // Starting from vertex 0

return 0; }
#include <stdio.h>

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

#define INF 99999 // A large value representing infinity

// Function to print the solution matrix

void printSolution(int dist[][V]) {

printf("Shortest distances between every pair of vertices:\n");

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

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

if (dist[i][j] == INF)

printf("INF ");

else

printf("%3d ", dist[i][j]);

printf("\n");

// Function to implement the Floyd-Warshall algorithm

void floydWarshall(int graph[][V]) {

int dist[V][V];

// Initialize the solution matrix same as input graph matrix

for (int i = 0; i < V; i++)

for (int j = 0; j < V; j++)

dist[i][j] = graph[i][j];
// Compute shortest paths

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

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

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

if (dist[i][k] + dist[k][j] < dist[i][j]) {

dist[i][j] = dist[i][k] + dist[k][j];

printSolution(dist);

int main() {

int graph[V][V] = {

{0, 3, INF, 7},

{8, 0, 2, INF},

{5, INF, 0, 1},

{2, INF, INF, 0}

};

floydWarshall(graph);

return 0;

You might also like