-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim.cpp
More file actions
74 lines (68 loc) · 1.52 KB
/
Prim.cpp
File metadata and controls
74 lines (68 loc) · 1.52 KB
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
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<cmath>
using namespace std;
int minKey(int* key, bool* mstSet, int n)
{
int min = INT_MAX, min_index;
for (int v = 0; v < n; v++)
if (mstSet[v] == false && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
void printMST(int* parent, int** graph, int n)
{
cout << "Edge \tWeight\n";
for (int i = 1; i < n; i++)
cout << parent[i] << " - " << i << " \t" << graph[i][parent[i]] << " \n";
}
void primMST(int** graph, int n)
{
int* parent = new int[n];
int* key = new int[n];
bool* mstSet = new bool[n];
for (int i = 0; i < n; i++)
key[i] = INT_MAX, mstSet[i] = false;
key[0] = 0;
parent[0] = -1;
for (int count = 0; count < n - 1; count++)
{
int u = minKey(key, mstSet, n);
mstSet[u] = true;
for (int v = 0; v < n; v++)
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, graph,n);
}
void Input(int** a, int n)
{
cout << "\n\t========== Enter element of Graph ==========";
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
cout << "\nEnter the value at position a[" << i << "][" << j << "]: ";
cin >> a[i][j];
}
}
void DeleteData(int** graph, int& n)
{
for (int i = 0; i < n; i++)
delete graph[i];
delete graph;
n = NULL;
}
int main()
{
int n;
cout << "Enter the size of graph: ";
cin >> n;
int** graph = new int* [n];
for (int i = 0; i < n; i++)
graph[i] = new int[n];
Input(graph, n);
primMST(graph, n);
DeleteData(graph, n);
cout << endl;
system("pause");
return 0;
}