-
Notifications
You must be signed in to change notification settings - Fork 50
/
nQueen.cpp
266 lines (206 loc) · 9.69 KB
/
nQueen.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include<bits/stdc++.h>
#define maxi 1000
using namespace std;
struct Item //Structure declaration
{
int weight; //variable to store weight of the item
int value; //variable to store value of the item
};
bool compare(Item a,Item b) //Comparator function to sort according to value by weight ratio
{
double c = ( double)a.value / (double)a.weight; // checking value by weight ratio for item a
double d = (double)b.value / (double)b.weight; // checking value by weight ratio for item b
return c > d;
}
int main()
{
int n,capacity; // Declaration of variables for no of items and capacity of bag
int cur_weight=0; // Initializing current value of weight to 0
double total_profit=0; // Initializing value of total_profit to 0
cout<<"Enter the number of items you want :: ";
cin>>n; //taking no of items from user
cout<<"\nEnter maximum capacity weight which you can carry :: ";
cin>>capacity; //taking capacity of bag from user
Item obj[n]; //Declaring array of structure
vector<vector<int>> DP(n + 1, vector<int>(capacity + 1)); //Declaring 2d array for 0-1 knapsack using dynamic Programming
int choice; // Declaring choice variable to take choice from the user
do //using do-while loop
{
cout<<"\n\n-------------------------------------------------"<<endl; // Displaying Menu
cout<<"1. Input"<<endl;
cout<<"2. Display"<<endl;
cout<<"3. Fractional Knapsack using Greedy Algorithm"<<endl;
cout<<"4. 0-1 Knapsack using Dynamic Programming"<<endl;
cout<<"5. 0-1 Knapsack using Greedy Algorithm"<<endl;
cout<<"0. Exit the Program"<<endl;
cout<<"-------------------------------------------------\n"<<endl;
cout<<"Enter an choice\n";
cin>>choice; // Taking choice from user
switch(choice) //menu driven program using switch case
{
case 1: // case 1 for input
for(int i=0;i<n;i++)
{
cout<<"Weight["<<i<<"]:\t";
cin>>obj[i].weight; //Taking weight from user for each item
cout<<"Value["<<i<<"]:\t";
cin>>obj[i].value; // taking value from user for each item
cout<<endl;
}
break; // end of case one
case 2: // case 2 for display
cout<<"\n(Weight,Value) pairs"<<endl;
for (int i = 0; i < n; i++)
{
cout<<i+1<<". ("<<obj[i].weight<<", "<<obj[i].value<<") "<<endl; //displaying weight and value of each item
}
break; // end of case two
case 3: // case 3 for Fractional knapsack using Greedy algorithm
sort(obj,obj+n,compare); // calling sort function to sort items according to value by weight ratio
for(int i=0;i<n;i++) // using for loop for iterating over n items
{
if (cur_weight + obj[i].weight <= capacity) //checking if sum of current weight and object weight is less than bag capacity
{
cur_weight+= obj[i].weight; // if true then adding object weight in bag
total_profit+=obj[i].value; // also adding the object value to profit value
cout<<"Added object "<<(i+1)<<" ("<<obj[i].value<<" Rs., "<<obj[i].weight<<"Kg) completely in the bag. Space left: "<<cur_weight<<endl;
}
else
{
int remain = capacity - cur_weight; // calculating remaining weight than the capacity of bag
double item_percent = (int) (((double)remain/(double)obj[i].weight)*100); // calculating percentage of weight that must be added in the bag
total_profit += obj[i].value*((double)remain/(double)obj[i].weight); // calculating total profit value of that percent weight of item
cout<<"Added "<<item_percent<<"% ("<<obj[i].value<<" Rs., "<<obj[i].weight<<" Kg) of object "<<i + 1<<" in the bag.\n";
break;
}
}
cout<<"\nFill the bag with objects worth "<<total_profit<<" Rs."<<endl;
cout<<"Maximum Profit value by fractional knapsack using greedy algorithm is "<<total_profit<<"\n"; //printing final profit value
break; // end of case three
case 4: // case 4 for 0-1 knapsack using Dynamic Programming
for(int i = 0; i <= n; i++) // using for loop for iterating over n items
{
for(int w = 0; w <= capacity; w++) // using for loop for iterating over capacity of bag
{
if (i == 0 || w == 0) // if its first row and first column
{
DP[i][w] = 0; // then fill the first row and all columns with all zero's
}
else if (obj[i - 1].weight <= w)
{
DP[i][w] = max(obj[i - 1].value + DP[i - 1][w - obj[i-1].weight],DP[i - 1][w]);
}
else{
DP[i][w] = DP[i - 1][w];
}
}
}
cout<<"\nMaximum Profit value by 0-1 knapsack using dp is "<<DP[n][capacity]<<"\n"; //printing final profit value
break; // end of case four
case 5: // case 5 for 0-1 knapsack using Greedy algorithm
sort(obj,obj+n,compare); // calling sort function to sort items according to value by weight ratio
cur_weight=0;
total_profit=0;
for(int i=0;i<n;i++) // using for loop for iterating over n items
{
if (cur_weight + obj[i].weight <= capacity) //checking if sum of current weight and object weight is less than bag capacity
{
cur_weight+= obj[i].weight; // if true then adding object weight in bag
total_profit+=obj[i].value; // also adding the object value to profit value
}
}
cout<<"\nMaximum Profit value by 0-1 knapsack using greedy algorithm is "<<total_profit<<"\n"; //printing final profit value
break; // end of case five
case 0: //zeroth case to terminate the program
cout<<"Program Executed Successfully.... \n******Thank You !!!******";
return 0; // end of program
default: //default case
cout<<"Enter valid choice\n";
break; // end of default case
} //end of switch case
}while(1); //end of do-while loop
} //end of main function
/*
OUTPUT OF KNAPSACK PROBLEM ::--
Enter the number of items you want :: 3
Enter maximum capacity weight which you can carry :: 50
-------------------------------------------------
1. Input
2. Display
3. Fractional Knapsack using Greedy Algorithm
4. 0-1 Knapsack using Dynamic Programming
5. 0-1 Knapsack using Greedy Algorithm
0. Exit the Program
-------------------------------------------------
Enter an choice
1
Weight[0]: 10
Value[0]: 60
Weight[1]: 20
Value[1]: 100
Weight[2]: 30
Value[2]: 120
-------------------------------------------------
1. Input
2. Display
3. Fractional Knapsack using Greedy Algorithm
4. 0-1 Knapsack using Dynamic Programming
5. 0-1 Knapsack using Greedy Algorithm
0. Exit the Program
-------------------------------------------------
Enter an choice
2
(Weight,Value) pairs
1. (10, 60)
2. (20, 100)
3. (30, 120)
-------------------------------------------------
1. Input
2. Display
3. Fractional Knapsack using Greedy Algorithm
4. 0-1 Knapsack using Dynamic Programming
5. 0-1 Knapsack using Greedy Algorithm
0. Exit the Program
-------------------------------------------------
Enter an choice
3
Added object 1 (60 Rs., 10Kg) completely in the bag. Space left: 10
Added object 2 (100 Rs., 20Kg) completely in the bag. Space left: 30
Added 66% (120 Rs., 30 Kg) of object 3 in the bag.
Fill the bag with objects worth 240 Rs.
Maximum value in fractional knapsack using greedy algorithm is 240
-------------------------------------------------
1. Input
2. Display
3. Fractional Knapsack using Greedy Algorithm
4. 0-1 Knapsack using Dynamic Programming
5. 0-1 Knapsack using Greedy Algorithm
0. Exit the Program
-------------------------------------------------
Enter an choice
4
Maximum value in 0-1 knapsack using dp is 220
-------------------------------------------------
1. Input
2. Display
3. Fractional Knapsack using Greedy Algorithm
4. 0-1 Knapsack using Dynamic Programming
5. 0-1 Knapsack using Greedy Algorithm
0. Exit the Program
-------------------------------------------------
Enter an choice
5
Maximum value in 0-1 knapsack using greedy algorithm is 160
-------------------------------------------------
1. Input
2. Display
3. Fractional Knapsack using Greedy Algorithm
4. 0-1 Knapsack using Dynamic Programming
5. 0-1 Knapsack using Greedy Algorithm
0. Exit the Program
-------------------------------------------------
Enter an choice
0
Program Executed Successfully....
******Thank You !!!******
*/