-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa12dd3
commit 8ea07f5
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* This program constructs an undirected graph by taking edges and vertices from the user | ||
* and sing Prim's Algorithm | ||
* constructs a Minimum Spanning Tree | ||
* | ||
* Coded by: Abdurrezak Efe | ||
* */ | ||
|
||
#include <iostream> | ||
#include <limits.h> | ||
using namespace std; | ||
|
||
const int V = 1000; //assumed number of vertices | ||
int edges[V+1][V+1] = {0}; //the graph itself | ||
|
||
int minKeyIndex(int keys[], bool inMST[], int v) //finding the minimum keyed vertex | ||
{ | ||
//finding the vertex with smallest key to add to MST | ||
int minKey = INT_MAX, minIndex=-1; | ||
for(int i=0; i<v; i++) | ||
if(!inMST[i] && keys[i] < minKey) | ||
minKey = keys[i], minIndex = i; | ||
|
||
return minIndex; | ||
} | ||
|
||
void Prim( int v, int e) | ||
{ | ||
int keys[v]; | ||
int previous[v]; | ||
bool inMST[v]; | ||
|
||
for(int i=0;i<v;i++) | ||
keys[i] = INT_MAX, inMST[i] = false; | ||
|
||
keys[0] = 0; | ||
previous[0] = -1; | ||
|
||
int vertices = 0; | ||
while(vertices < v-1) //there will be v vertices in MST | ||
{ | ||
|
||
int u = minKeyIndex(keys, inMST, v); | ||
|
||
//adding u to MST | ||
inMST[u] = true; | ||
|
||
//operating on u's neighbors to add the smallest | ||
for(int i=0; i<v; i++) | ||
if(edges[u][i] && !inMST[i] && edges[u][i] < keys[i]) | ||
previous[i] = u, keys[i] = edges[u][i]; | ||
|
||
vertices++; | ||
} | ||
|
||
|
||
//printing MST | ||
int i = 1; | ||
while(i<v) | ||
cout << i << ", " << previous[i] << ", " << edges[i][previous[i]] << endl, i++; | ||
|
||
} | ||
|
||
// driver program to test above function | ||
int main() | ||
{ | ||
int v,e; | ||
cin >> v >> e; | ||
|
||
//constructing the graph | ||
int x,y,w; | ||
for(int i=0;i<e;i++) | ||
cin >> x >> y >> w, edges[x][y] = edges[y][x] = w; | ||
|
||
Prim(v, e); | ||
} |