-
Notifications
You must be signed in to change notification settings - Fork 0
/
q10.cpp
321 lines (260 loc) · 8.65 KB
/
q10.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include<iostream>
using namespace std;
bool **adj_matrix;
bool *isPotential;
int countknown(int n, int person_x)
{
/*
Description
-----------
This function calculates number of people that a person_x knows.
Input variables
---------------
- n : size of array isPotenital
- person_x : person for which number of his/her acquaintance has to be calculated
Global variable
---------------
- isPotenital : a boolean array, where value of isPotenital[i] states whether a person i can considered for invititation. True, if yes. False, if no.
Return value
------------
number of people that person_x knows
*/
int count=0;
for(int i=1; i<=n; i++){
// test whether person i is a potenital invitee and also if person i and person_x know each other
if(isPotential[i] && adj_matrix[i][person_x])
count++;
}
return count;
}
void plan_party(int n){
/*
Description
-----------
This function computes all persons who can be invitated for a party out of n people based on following conditions:
1. Every person invited should know at least five other people that are invited.
2. Every person invited should not know at least five other people that are invited.
3. Number of invitees is maximized
Input variable
--------------
- n : Number of people to choose from
Global variables
----------------
- adj_matrix : a boolean 2D matrix, where value at adj_matrix[x][y] states whether person x knows person y. True, if yes. False, if no.
- isPotenital : a boolean array, where value at isPotenital[i] states whether a person i can considered for invititation. True, if yes. False, if no.
Return value
------------
None
Side effects
------------
If either condition 1 or condition 2 is not true for a person x then, update isPotenital[x] to false
*/
int potential_count = n;
for(int j=1; j<=n; j++)
{
//stores the number of people j knows
int known = countknown(n,j);
//stores the number of people j does not know
int unknown = potential_count - known - 1;
//if a potenital invitee disqualifies any of condition 1 or 2, make him invalid and start checking from person 1 again
if( isPotential[j] && (known < 5 || unknown < 5) )
{
isPotential[j]=false;
potential_count--;
j = 0;
//if number of potenital invitee become less than 11, there is no way we can find a person who know 5 person and does not know 5 person
if(potential_count < 11){
for(int i=1;i<=n;i++)
isPotential[i] = false;
break;
}
}
}//the loop ends when every potential invitee satisfy both condition 1 and 2 or everyone become impotential
}
void print_invitees(int n){
/*Description
-----------
This function print the final invitees and the count of invitees.
*/
int count = 0;
cout<<"\n\nAlice can invite following people\n\n";
for(int i=1; i<=n; i++){
if(isPotential[i]){
cout<<i<<"\t";
count++;
}
}
if(count == 0)
cout<<"\n\nNo one can be invited.";
else
cout<<"\n\nMaximum number of people who can be invited is : "<<count;
}
int main()
{
int n;
while(true){
cout<<"\nEnter value of n(greater than or equal to 11): ";
cin>>n;
if(n>=11)
break;
}
//dyanamic allocation
adj_matrix = new bool*[n+1];
for(int i=0;i<=n;i++)
adj_matrix[i] = new bool[n+1];
isPotential = new bool[n+1];
//initialize adj_matrix such that nobody knows anyone
for(int i=1; i<=n; i++)
{
isPotential[i] = true;
for(int j=1; j<=n; j++)
{
adj_matrix[i][j] = false;
}
}
char choice;
//input pairs
while(true)
{
int x, y;
cout<<"\nEnter pair (x,y) who know each other(person number starts from 1 and goes till n) \n";
cout<<"\n Person x : ";
cin>>x;
if(x<1 || x>n){
cout<<"\nInvald pair!Enter again!\n";
continue;
}
cout<<"\n Person y : ";
cin>>y;
if(y<1 || y>n || y == x){
cout<<"\nInvald pair!Enter again!\n";
continue;
}
//update adjacency matrix
adj_matrix[x][y] = true;
adj_matrix[y][x] = true;
cout<<"\nAre there any more pairs who know each other?(y/n): ";
cin>>choice;
if(choice=='n')
break;
x++;
y++;
}
plan_party(n);
print_invitees(n);
return 0;
}
____________________________________________________________________________________________________________________________________
.......................................................PROGRAMMING APPROACH.......................................................
1. Greedy
2. Iterative
__________________________________________________________________________________________________________________________________
...........................................................EXPLANATION..........................................................
Global variables
----------------
- adj_matrix : a boolean 2D matrix, where value at adj_matrix[x][y] states whether person x knows person y. True, if yes. False, if no.
- isPotenital : a boolean array, where value at isPotenital[i] states whether a person i can considered for invititation. True, if yes.
False, if no.
======================================================================================================================
--------------------------------------
Function 1: countknown(n, person_x)
--------------------------------------
**********
Pseudocode
**********
countknown(n, person_x)
1. count = 0;
2. for i = 1 to n
3. if isPotential[i] == true and adj_matrix[i][person_x]
4. count = count + 1
5. return count
Description
-----------
The function countknown(n, person_x) calculates number of people that a person_x knows.
Input variables
---------------
- n : size of array isPotenital
- person_x : person for which number of his/her acquaintance has to be calculated
Return value
------------
number of people that person_x knows
======================================================================================================================
-------------------------
Function 2: plan_party(n)
-------------------------
**********
Pseudocode
**********
plan_party(n):
1. potential_count = n;
2. for j = 1 to n
3. known = countknown(n,j);
4. unknown = potential_count - known - 1;
5. if isPotential[j] and (known < 5 or unknown < 5)
6. isPotential[j] = false
7. potential_count = potential_count - 1
8. j = 0
9. if potential_count < 11
10. for i = 1 to n
11. isPotential[i] = false
12. break
Description
-----------
This function computes all persons who can be invitated for a party out of n people based on following conditions:
1. Every person invited should know at least five other people that are invited.
2. Every person invited should not know at least five other people that are invited.
3. Number of invitees is maximized
Input variable
--------------
- n : Number of people to choose from
Return value
------------
None
Side effects
------------
If either condition 1 or condition 2 is not true for a person x then, update isPotenital[x] to false
======================================================================================================================
-----------------------------
Function 3: print_invitees(n)
-----------------------------
**********
Pseudocode
**********
print_invitees(n):
1. count = 0
2. for i = 1 to n
3. if isPotential[i] == true
4. print i
5. count = count + 1
6. print count
Description
-----------
print_invitee(n) function prints the final invitees and the count of invitees.
======================================================================================================================
Approach
--------
Initially, zero people are removed. Check for condition 1 and 2 for every person.
Whenever a failure occur remove that person and start all over again with remaining persons.
Stop when no one will fail the conditions or everyone fails it.
----------
Complexity
----------
Function 1:
line 1 -> O(1)
loop 2-4 -> O(n) linear time
line 5 -> O(1)
--------------------------------------------------
Function 2:
line 1 -> O(1)
line 2 -> j iterates from 1 to n,
line 3 -> O(n) it makes a call to countknown in every iteration
line 4 -> O(1)
line 5 -> if the condition in this line is satisfied, the value of j is again set to 1.
which implies there will be more than n iterations in line 2. In worst case value of j is set to 1 by every value of j,
therefore the main loop runs atmost n^2 times.
line 6-8 -> O(1)
line 9 -> if this condition is true, we will come out of the main loop. and line 10-11 will iterate n times but only once, hence it will not effect the degree of time complexity
--------------------------------------------------
Since, countknown is called atmost n^2 time.
Time complexity = O(n^3)
Space complexity = O(n^2)