PRIMS and Floyd
PRIMS and Floyd
#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;
}
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}
};
FLOYD
#include <stdio.h>
#include <stdlib.h>
// 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}
};