-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset.cpp
125 lines (102 loc) · 1.93 KB
/
subset.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
// Program for subset sum problem.
#include <iostream>
using namespace std;
class SubSet
{
int **arr;
int *set;
int n;
int w;
public:
SubSet( int count, int sum )
{
n = count;
w = sum;
arr = new int*[n + 1];
for( int i = 0; i <= n; i++ )
arr[i] = new int[w + 1];
set = new int[n + 1];
}
void input()
{
cout << " \n\t\t\t Enter the positive Set...\n\t\t\t ";
for( int i = 1; i <= n; i++ )
{ cin >> set[i]; cout << " \n\t\t\t "; }
}
int max( int x, int y )
{
if( x > y )
return x;
else
return y;
}
bool subset_sum()
{
for( int i = 0; i <= w; i++ )
arr[0][i] = 0;
for( int i = 0; i <= n; i++ )
arr[i][0] = 0;
for( int i = 1; i <= n; i++ )
{
for( int j = 1; j <= w; j++ )
{
if( j < set[i] )
arr[i][j] = arr[i - 1][j];
else
arr[i][j] = max( arr[i - 1][j], set[i] + ( arr[i - 1][j - set[i] ] ) );
}
}
/*for( int i = n; i >= 0; i-- )
{
for( int j = 0; j <= w; j++ )
cout << arr[i][j] <<" ";
cout << endl;
}
*/
if( arr[n][w] == w )
return true;
else
return false;
}
void backtrack( int i, int W )
{
if( i == 0 || W == 0 )
return;
else
{
if( set[i] > W || arr[i][W] == arr[i - 1][W] )
backtrack( i-1, W );
else
{
cout << i << " ";
backtrack( i - 1, W - set[i] );
}
}
}
void print()
{
cout << " \n\t\t\t Positions of elements Included : \n\t\t\t ";
backtrack( n, w );
cout << endl;
}
};
int main()
{
cout << " \n\t\t\t SUBSET SUM PROBLEM \n\t\t\t ";
int count,sum;
cout << " \n\t\t\t Enter the count of Numbers in a Set : \n\t\t\t ";
cin >> count;
cout << " \n\t\t\t Enter the Sum Expected : \n\t\t\t ";
cin >> sum;
SubSet *obj = new SubSet( count, sum );
obj -> input();
if( obj -> subset_sum() )
{
cout << " \n\t\t\t Yes, A subset can be Created... \n\t\t\t ";
obj -> print();
}
else
cout << " \n\t\t\t No, A subset can't be Created...\n\t\t\t ";
cout << "\n\n";
return 0;
}