From 162e1a4b45ee0f11f5ca98490432d98d9e716b5c Mon Sep 17 00:00:00 2001 From: Rohan Singh Rathore Date: Tue, 19 Oct 2021 09:50:22 +0530 Subject: [PATCH] Create prims_algo.cpp --- prims_algo.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 prims_algo.cpp diff --git a/prims_algo.cpp b/prims_algo.cpp new file mode 100644 index 0000000..a6ef085 --- /dev/null +++ b/prims_algo.cpp @@ -0,0 +1,107 @@ +// 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<