-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path01. Programming Assignment #1.cpp
132 lines (110 loc) · 3.02 KB
/
01. Programming Assignment #1.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* File Name: Programming Assignment #1
*
* Credit : Course : Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming
* Instructor : Tim Roughgarden, Professor
*
* Author: Mrunal Nirajkumar Shah
* Date: 5th Of MARCH, 2023
*
* Description: Your task in this problem is to run the clustering algorithm from lecture on this data set, where the target number kk of clusters is set to 4. What is the maximum spacing of a 4-clustering?
*
* Dependencies: This Code requires a file named "01.clustering1.txt" and you can add all the test cases there.
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
class UF {
int N, cnt, *id, *size;
public:
// create an empty union-find data structure with N separate clusters
UF(int n){
N = n;
cnt = n;
id = new int[n];
size = new int[n];
for(int i=0; i<n; ++i){
id[i] = i;
size[i] = 1;
}
}
// return the leader of the group that point p belongs to
int find(int p){
return id[p];
}
// replace clusters containing x and y with their union
void merge(int x, int y){
int i = find(x);
int j = find(y);
if(i == j)
return;
// update the leader of the smaller group
if(size[i] < size[j]){
for(int k=0; k<N; ++k){
if(id[k] == i)
id[k] = j;
}
size[j] += size[i];
} else {
for(int k=0; k<N; ++k){
if(id[k] == j)
id[k] = i;
}
size[i] += size[j];
}
cnt--;
}
bool connected(int x, int y){
return find(x) == find(y);
}
int count(){
return cnt;
}
};
bool compareTwoEdges(vector<int> e1, vector<int> e2){
// return true for lower cost
return e1[2] < e2[2];
}
class KruskalMST{
public:
int clustering(int k, int n, vector<vector<int> > E) {
// sort edges in order of increasing distance
sort(E.begin(), E.end(), compareTwoEdges);
// initialize each node as a separate cluster
UF uf(n);
// merge two closest clusters until only k clusters
int j = 0;
while(uf.count()>k){
while(uf.connected(E[j][0], E[j][1]))
j++;
uf.merge(E[j][0], E[j][1]);
}
// find the next closest clusters
while(uf.connected(E[j][0], E[j][1]))
j++;
return E[j][2];
}
};
int main()
{
ifstream myfile;
myfile.open("01.clustering1.txt");
int n; // number of nodes
myfile >> n;
int m = n*(n-1)/2; // number of edges
vector<vector<int> > E(m, vector<int>(3));
for(int i=0; i<m; ++i)
{
myfile >> E[i][0];
myfile >> E[i][1];
myfile >> E[i][2];
E[i][0]--;
E[i][1]--;
}
KruskalMST kmst;
int max_space = kmst.clustering(4, n, E);
cout << max_space << endl;
return 0;
}