0% found this document useful (0 votes)
15 views5 pages

PRIMS and Floyd

Uploaded by

Abhay Verma
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)
15 views5 pages

PRIMS and Floyd

Uploaded by

Abhay Verma
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/ 5

PRIMS

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

// Function to find the vertex with the minimum key value that is not yet
included in the MST
int minKey(int key[], bool mstSet[], int V) {
int minIndex = 0;
int min = INT_MAX;

// Iterate over all vertices to find the one with the minimum key
value
for (int i = 0; i < V; i++) {
if (!mstSet[i] && key[i] < min) {
min = key[i];
minIndex = i;
}
}
return minIndex;
}

// Function to print the constructed MST


void printMST(int parent[], int graph[][12], int V) {
char c = 'A';

// Print the MST edges


for (int i = 1; i < V; i++) {
printf("%c > %c == %d\n", c + parent[i], c + i,
graph[i][parent[i]]);
}
}

// Function to implement Prim's Algorithm for finding MST


void prim(int graph[][12], int V) {
int parent[V]; // Array to store the constructed MST
int key[V]; // Array to store the minimum weight edge for each
vertex
bool mstSet[V]; // Array to keep track of vertices included in MST

// Initialize all key values as INFINITY and mstSet as false


for (int i = 0; i < V; i++) {
mstSet[i] = false;
key[i] = INT_MAX;
}

// First node is always the root of the MST


parent[0] = -1;
key[0] = 0;

// Construct the MST with V-1 edges


for (int count = 0; count < V - 1; count++) {
// Get the vertex with the minimum key value that is not yet
included in MST
int u = minKey(key, mstSet, V);
mstSet[u] = true;

// Update key values and parent index of adjacent vertices


for (int v = 0; v < V; v++) {
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}
}

// Print the constructed MST


printMST(parent, graph, V);
}

int main() {
// Define the graph as an adjacency matrix
int graph[12][12] = {
{0, 3, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
{3, 0, 0, 0, 3, 6, 0, 0, 0, 0, 0, 0},
{5, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 0},
{4, 0, 2, 0, 1, 0, 0, 5, 0, 0, 0, 0},
{0, 3, 0, 1, 0, 2, 0, 0, 4, 0, 0, 0},
{0, 6, 0, 0, 2, 0, 0, 0, 0, 5, 0, 0},
{0, 0, 4, 0, 0, 0, 0, 3, 0, 0, 6, 0},
{0, 0, 0, 5, 0, 0, 3, 0, 6, 0, 7, 0},
{0, 0, 0, 0, 4, 0, 0, 6, 0, 3, 0, 5},
{0, 0, 0, 0, 0, 5, 0, 0, 3, 0, 0, 9},
{0, 0, 0, 0, 0, 0, 6, 7, 0, 0, 0, 8},
{0, 0, 0, 0, 0, 0, 0, 0, 5, 9, 8, 0}
};

// Call Prim's algorithm to find


prim(graph, 12);

return 0; // Exit the program


}

FLOYD
#include <stdio.h>
#include <stdlib.h>

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


#define INT_MAX 999999 // Representation of infinity for unreachable nodes

// Function to print a 2D array


void print(int arr[][V]) {
// Iterate through each row of the array
for (int i = 0; i < V; i++) {
// Iterate through each column of the array
for (int j = 0; j < V; j++) {
printf("\t%d", arr[i][j]); // Print each element of the array
}
printf("\n"); // Newline after each row
}
}

// Function to implement the Floyd-Warshall algorithm


void floyd(int graph[][V]) {
// Arrays for shortest distances and path reconstruction
int dp[V][V], s[V][V];

// Initialize dp array with graph values and s array for path


reconstruction
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
dp[i][j] = graph[i][j]; // Copy the graph values to dp array
s[i][j] = j; // Initialize s array with direct paths
}
}

// Set the diagonal of dp array to 0 and s array to 0 for self-loops


for (int i = 0; i < V; i++) {
dp[i][i] = 0; // Distance from a vertex to itself is 0
s[i][i] = 0; // Path to itself is 0
}

// Floyd-Warshall algorithm to find shortest paths between all pairs


of vertices
for (int k = 0; k < V; k++) { // Iterate through each vertex as an
intermediate point
for (int i = 0; i < V; i++) { // Iterate through each source
vertex
for (int j = 0; j < V; j++) { // Iterate through each
destination vertex
// Update the shortest distance and path if a shorter path
is found
if (dp[i][j] > (dp[i][k] + dp[k][j])) {
dp[i][j] = dp[i][k] + dp[k][j]; // Update shortest
distance
s[i][j] = k; // Update path reconstruction
}
}
}
}
// Print the final DP matrix of shortest distances
printf("\nThe FINAL DP matrix is \n ");
print(dp);

// Print the final status matrix for path reconstruction


printf("\nThe final status matrix is :\n");
print(s);
}

// Main function
void main() {
// Example graph with 5 vertices, using INT_MAX to represent infinity
int graph[V][V] = {
{INT_MAX, 10, 20, INT_MAX, 5},
{10, INT_MAX, INT_MAX, 2, 5},
{20, INT_MAX, INT_MAX, 3, INT_MAX},
{INT_MAX, 2, 3, INT_MAX, 7},
{5, 5, INT_MAX, 7, INT_MAX}
};

// Call the Floyd-Warshall algorithm


floyd(graph);
}

You might also like