diff --git a/GreedyAlgorithms/prims_algorithm.cpp b/GreedyAlgorithms/prims_algorithm.cpp new file mode 100644 index 00000000..95c9af58 --- /dev/null +++ b/GreedyAlgorithms/prims_algorithm.cpp @@ -0,0 +1,105 @@ +// A C++ program for Prim's Minimum +// Spanning Tree (MST) algorithm. The program is +// for adjacency matrix representation of the graph +#include +using namespace std; + +// Number of vertices in the graph +#define V 5 + +// A utility function to find the vertex with +// minimum key value, from the set of vertices +// not yet included in MST +int minKey(int key[], bool mstSet[]) +{ + // Initialize min value + int min = INT_MAX, min_index; + + for (int v = 0; v < V; v++) + if (mstSet[v] == false && key[v] < min) + min = key[v], min_index = v; + + return min_index; +} + +// A utility function to print the +// constructed MST stored in parent[] +void printMST(int parent[], int graph[V][V]) +{ + cout<<"Edge \tWeight\n"; + for (int i = 1; i < V; i++) + cout<