minimum cost spanning tree uskin prims
#include <stdio.h>
#define INF 9999
#define MAX 10


int main()
{
    int n, cost[MAX][MAX], visited[MAX] = {0};
    int i, j, edges = 0, min, u, v, total = 0;


    printf("Enter number of vertices: ");
    scanf("%d", &n);


    printf("Enter the cost adjacency matrix (0 for no edge):\n");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
        {
            scanf("%d", &cost[i][j]);
            if (cost[i][j] == 0)
                cost[i][j] = INF;
        }


    visited[0] = 1;


    printf("\nEdges in Minimum Spanning Tree:\n");
    while (edges < n - 1)
    {
        min = INF;
        for (i = 0; i < n; i++)
        {
            if (visited[i])
            {
                for (j = 0; j < n; j++)
                {
                    if (!visited[j] && cost[i][j] < min)
                    {
                        min = cost[i][j];
                        u = i;
                        v = j;
                    }
                }
            }
        }


        printf("Edge %d: (%d - %d) cost = %d\n", edges + 1, u, v, min);
        total += min;
        visited[v] = 1;
        edges++;
    }


    printf("Total Minimum Cost = %d\n", total);
    return 0;
}
