-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathk-partition-problem.cpp
204 lines (174 loc) · 4.85 KB
/
k-partition-problem.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @file k-partition-problem.cpp
* @author prakash (prakashsellathurai@gmail.com)
* @brief
In the k-partition problem, we need to partition a multiset of positive
integers into k disjoint subsets that have equal sum. Design and implement an
algorithm for solving the k-partition problem
* @version 0.1
* @date 2021-08-07
*
* @copyright Copyright (c) 2021
*
*/
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
/*
******************************* Naive method *****************************
*/
// Function to check if all subsets are filled or not
bool checkSum(int sumLeft[], int k) {
int r = true;
for (int i = 0; i < k; i++) {
if (sumLeft[i] != 0) {
r = false;
break;
}
}
return r;
}
bool naive_subset_sum(const vector<int> &nums, int n, int sumLeft[], int A[],
int k) {
// return true if a subset is found
if (checkSum(sumLeft, k)) {
return true;
}
// base case: no items left
if (n < 0) {
return false;
}
bool result = false;
// consider current item `S[n]` and explore all possibilities
// using backtracking
for (int i = 0; i < k; i++) {
if (!result && (sumLeft[i] - nums[n] >= 0)) {
A[n] = i + 1;
sumLeft[i] = sumLeft[i] - nums[n];
result = naive_subset_sum(nums, n - 1, sumLeft, A, k);
sumLeft[i] = sumLeft[i] + nums[n];
}
}
return result;
}
/**
* @brief 2^n - 1 naive backtracking algorithm
*
* @param nums
* @param n
* @param k
*/
void k_partition_naive(vector<int> &nums, int n, int k) {
if (n < k) {
cout << "K -partition of set is not possible";
return;
}
int sum = accumulate(nums.begin(), nums.end(), 0);
int A[n], sumLeft[k];
for (int i = 0; i < k; i++) {
sumLeft[i] = sum / k;
}
bool result = !(sum % k) && naive_subset_sum(nums, n - 1, sumLeft, A, k);
if (!result) {
cout << "K -partition of set is not possible";
return;
}
// print all k–partitions
for (int i = 0; i < k; i++) {
cout << "Partition " << i << " is ";
for (int j = 0; j < n; j++) {
if (A[j] == i + 1) {
cout << nums[j] << " ";
}
}
cout << endl;
}
}
/******************************** Bit masking
* *******************************************************/
/**
* @brief using bit masking
*
* @param nums
* @param k
* @return true
* @return false
*/
bool canPartitionKSubsets(vector<int> &nums, int k) {
int dp[(1 << 16) + 2];
int n = nums.size(), sum = 0;
fill(dp, dp + (1 << 16) + 2, -1);
dp[0] = 0;
for (int i = 0; i < n; i++)
sum += nums[i];
if (sum % k)
return false;
int tar = sum / k;
for (int mask = 0; mask < (1 << n); mask++) {
if (dp[mask] == -1)
continue; // if current state is illegal, simply ignore it
for (int i = 0; i < n; i++) {
if (!(mask & (1 << i)) &&
dp[mask] + nums[i] <= tar) { // if nums[i] is unchosen && choose
// nums[i] would not cross the target
dp[mask | (1 << i)] = (dp[mask] + nums[i]) % tar;
}
}
}
return dp[(1 << n) - 1] == 0;
}
/***************************** Bucket methodO(n^k)
* ******************************/
/*
Put n items into k bucket so each bucket has same total item value.
For each bucket, try all possible content O(k*2^n) -- no good.
For each item, try all possible destined bucket O(n^k) -- doable.
*/ // put #n item of ns into some bucket to meet target
bool put(int n, int target, vector<int> ns, vector<int> bucket
) {
for (int i = 0; i < bucket.size(); ++i) {
if (bucket[i] + ns[n] > target)
continue; // try next bucket
bucket[i] += ns[n]; // put it in!
if (n == ns.size() - 1)
return true; // all items in bucket, no overflow
if (put(n + 1, target, ns, bucket))
return true; // move on to next item
else { // no solution = wrong bucket
bucket[i] -= ns[n]; // take it out
if (bucket[i] == 0)
return false; // no need to try other empty bucket
}
}
return false; // no bucket fits
}
bool CanPartitionKBucket(vector<int> &nums, int k) {
int target; // of each bucket
vector<int> ns;
vector<int> bucket;
int sum = 0;
for (int &n : nums)
sum += n;
if (sum % k)
return false; // not divisible
target = sum / k;
ns = vector<int>(nums);
bucket = vector<int>(k, 0);
// starting with bigger ones makes it faster
sort(ns.begin(), ns.end());
reverse(ns.begin(), ns.end());
return put(0, target, ns, bucket);
}
int main(int argc, const char **argv) {
vector<int> nums = {7, 3, 5, 12, 2, 1, 5, 3, 8, 4, 6, 4};
;
int n = nums.size();
int k = 5;
cout << "Naive method :" << endl;
k_partition_naive(nums, n, k);
cout << "\nBit masking method : " << canPartitionKSubsets(nums, k) << endl;
cout << "\nBucket method : " << CanPartitionKBucket(nums, k) << endl;
return 0;
}