-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprims.cpp
63 lines (54 loc) · 1.21 KB
/
prims.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <bits/stdc++.h>
#include <limits.h>
using namespace std;
#define V 5
int minIndex(bool visit[V], int key[V])
{
int small = INT_MAX;
int loc;
for(int i=0; i<V; i++)
if(visit[i]==false && small>key[i])
{
small=key[i];
loc=i;
}
return loc;
}
void primMST(int graph[][V])
{
//key holds the distance to adj vertices
//parent stores the parent of a vertice
int parent[V],key[V];
bool visited[V];
for(int i=0; i<V; i++)
{
key[i]=INT_MAX;
visited[i]=false;
}
parent[0]=-1;
key[0]=0;
for(int i=0; i<V; i++)
{
int loc = minIndex(visited,key);
visited[loc]=true;
for(int j=0; j<V; j++)
if(graph[loc][j] && visited[j]==false && graph[loc][j]<key[j])
{
key[v]=graph[loc][j];
parent[j]=loc;
}
}
}
int main()
{
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0},
};
// Print the solution
primMST(graph);
return 0;
}