TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
partition_problem.cpp
1/******************************************************************************
2 * @file
3 * @brief Implementation of the [Partition
4 * Problem](https://en.wikipedia.org/wiki/Partition_problem )
5 * @details
6 * The partition problem, or number partitioning, is the task of deciding
7 * whether a given multiset S of positive integers can be partitioned into two
8 * subsets S1 and S2 such that the sum of the numbers in S1 equals the sum of
9 * the numbers in S2. Although the partition problem is NP-complete, there is a
10 * pseudo-polynomial time dynamic programming solution, and there are heuristics
11 * that solve the problem in many instances, either optimally or approximately.
12 * For this reason, it has been called "the easiest hard problem".
13 *
14 * The worst case time complexity of Jarvis’s Algorithm is O(n^2). Using
15 * Graham’s scan algorithm, we can find Convex Hull in O(nLogn) time.
16 *
17 * ### Implementation
18 *
19 * Step 1
20 * Calculate sum of the array. If sum is odd, there can not be two subsets with
21 * equal sum, so return false.
22 *
23 * Step 2
24 * If sum of array elements is even, calculate sum/2 and find a subset of array
25 * with sum equal to sum/2.
26 *
27 * @author [Lajat Manekar](https://github.com/Lazeeez)
28 *
29 *******************************************************************************/
30#include <cassert>
31#include <cstdint>
32#include <iostream>
33#include <numeric>
34#include <vector>
35/******************************************************************************
36 * @namespace dp
37 * @brief Dynamic programming algorithms
38 *******************************************************************************/
39namespace dp {
40
41/******************************************************************************
42 * @namespace partitionProblem
43 * @brief Partition problem algorithm
44 *******************************************************************************/
45namespace partitionProblem {
46
47/******************************************************************************
48 * @brief Returns true if arr can be partitioned in two subsets of equal sum,
49 * otherwise false
50 * @param arr vector containing elements
51 * @param size Size of the vector.
52 * @returns @param bool whether the vector can be partitioned or not.
53 *******************************************************************************/
54bool findPartiion(const std::vector<uint64_t> &arr, uint64_t size) {
55 uint64_t sum = std::accumulate(arr.begin(), arr.end(),
56 0); // Calculate sum of all elements
57
58 if (sum % 2 != 0) {
59 return false; // if sum is odd, it cannot be divided into two equal sum
60 }
61 std::vector<bool> part;
62 // bool part[sum / 2 + 1];
63
64 // Initialize the part array as 0
65 for (uint64_t it = 0; it <= sum / 2; ++it) {
66 part.push_back(false);
67 }
68
69 // Fill the partition table in bottom up manner
70 for (uint64_t it = 0; it < size; ++it) {
71 // The element to be included in the sum cannot be greater than the sum
72 for (uint64_t it2 = sum / 2; it2 >= arr[it];
73 --it2) { // Check if sum - arr[i]
74 // ould be formed from a subset using elements before index i
75 if (part[it2 - arr[it]] == 1 || it2 == arr[it]) {
76 part[it2] = true;
77 }
78 }
79 }
80 return part[sum / 2];
81}
82} // namespace partitionProblem
83} // namespace dp
84
85/*******************************************************************************
86 * @brief Self-test implementations
87 * @returns void
88 *******************************************************************************/
89static void test() {
90 std::vector<uint64_t> arr = {{1, 3, 3, 2, 3, 2}};
91 uint64_t n = arr.size();
92 bool expected_result = true;
93 bool derived_result = dp::partitionProblem::findPartiion(arr, n);
94 std::cout << "1st test: ";
95 assert(expected_result == derived_result);
96 std::cout << "Passed!" << std::endl;
97}
98
99/*******************************************************************************
100 * @brief Main function
101 * @returns 0 on exit
102 *******************************************************************************/
103int main() {
104 test(); // run self-test implementations
105 return 0;
106}
void test()
int main()
Main function.
for std::vector