0% found this document useful (0 votes)
9 views6 pages

Shortest Path

The document contains C code implementing three graph algorithms: Dijkstra's for shortest paths, Prim's for minimum spanning tree (MST), and Kruskal's for MST using edge list. Each algorithm is demonstrated with a sample graph, and the results are printed, showing distances and edges with weights. The total cost of the MST using Kruskal's algorithm is also provided.

Uploaded by

ishapandit1603
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)
9 views6 pages

Shortest Path

The document contains C code implementing three graph algorithms: Dijkstra's for shortest paths, Prim's for minimum spanning tree (MST), and Kruskal's for MST using edge list. Each algorithm is demonstrated with a sample graph, and the results are printed, showing distances and edges with weights. The total cost of the MST using Kruskal's algorithm is also provided.

Uploaded by

ishapandit1603
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/ 6

#include <stdio.

h>
#include <stdlib.h>
#include <limits.h>

#define V 5

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

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


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

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


int u = minDistance(dist, sptSet);
sptSet[u] = 1;

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

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


for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}

int minKey(int key[], int mstSet[]) {


int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == 0 && key[v] < min)
min = key[v], min_index = v;
return min_index;
}

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


int parent[V], key[V], mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] = 0;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = 1;

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


if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}

printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]);
}

struct Edge {
int src, dest, weight;
};
int find(int parent[], int i) {
if (parent[i] != i)
parent[i] = find(parent, parent[i]);
return parent[i];
}

void Union(int parent[], int rank[], int x, int y) {


int rootX = find(parent, x);
int rootY = find(parent, y);
if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else {
parent[rootY] = rootX;
rank[rootX]++;
}
}

void kruskalMST(struct Edge edges[], int E) {


int parent[V], rank[V], mstCost = 0;
for (int i = 0; i < V; i++)
parent[i] = i, rank[i] = 0;

printf("Edge \tWeight\n");
for (int i = 0, e = 0; e < V - 1 && i < E; i++) {
int u = find(parent, edges[i].src);
int v = find(parent, edges[i].dest);
if (u != v) {
printf("%d - %d \t%d\n", edges[i].src, edges[i].dest, edges[i].weight);
mstCost += edges[i].weight;
Union(parent, rank, u, v);
e++;
}
}
printf("Total MST cost: %d\n", mstCost);
}
int main() {
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};

printf("Dijkstra's Algorithm:\n");
dijkstra(graph, 0);

printf("\nPrim's MST:\n");
primMST(graph);

struct Edge edges[] = {{0, 1, 2}, {1, 2, 3}, {0, 3, 6},


{1, 3, 8}, {1, 4, 5}, {2, 4, 7},
{3, 4, 9}};
int E = sizeof(edges) / sizeof(edges[0]);

printf("\nKruskal's MST:\n");
kruskalMST(edges, E);

return 0;
}
OUTPUT
Dijkstra's Algorithm:
Vertex Distance from Source
0 0
1 2
2 5
3 6
4 7

Prim's MST:
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5

Kruskal's MST:
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
Total MST cost: 16

You might also like