From bf000284bca891f95b31bf70f8bbfc50a0c56679 Mon Sep 17 00:00:00 2001 From: Akshit Kharbanda Date: Sun, 31 Oct 2021 14:44:20 -0500 Subject: [PATCH] add prims.cpp --- .../Graph Theory Algorithms/prims.cpp | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 C++/Algorithms/Graph Theory Algorithms/prims.cpp diff --git a/C++/Algorithms/Graph Theory Algorithms/prims.cpp b/C++/Algorithms/Graph Theory Algorithms/prims.cpp new file mode 100644 index 000000000..0a435c4ce --- /dev/null +++ b/C++/Algorithms/Graph Theory Algorithms/prims.cpp @@ -0,0 +1,102 @@ +#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<