PDS Lab Spring 23 Sec6 Week4
PDS Lab Spring 23 Sec6 Week4
a) [40 Points]
A subcollection of these n ducklings is said to be UTOPIC if the
subcollection does not contain any two consecutive ducklings, i.e.,
duckling i and i+1 for some value of i. The empty subcollection is
also treated as UTOPIC. Let Ln denote the number of UTOPIC
subcollections of n ducklings on a line. These collections together
with Ln for some small values of n are given in the table below:
1 Empty,{1} 2
2 Empty,{1},{2} 3
3 Empty,{1},{2},{3},{1,3} 5
4 Empty,{1},{2},{3},{4},{1,3},{2,4},{1,4} 8
5 Empty,{1},{2},{3},{4},{5},{1,3},{2,4},{3,5},{1,4},{2,5}, 13
{1,5},{1,3,5}
The function should take two arguments: 𝑛 and the last duckling 𝑙 chosen
so far. The next choice must be one from 𝑙 + 2, ... , 𝑛. For each such
choice m, pass n and m to a recursive call. Another possibility is that no
other duckling is chosen in the collection at all. Take a sum of the counts
from all possibilities and return the sum. The outermost call of the
recursive function in the main() function must pass a suitable value for
the second parameter.
b) [40 Points]
Next suppose that the ducklings are sitting on a circle, so that
duckling 1 and 𝑛 are now adjacent to one another. Let Cn denote the
number of UTOPIC subcollections of n ducklings in a circle. The
following table summarizes some small values.
1 Empty,{1} 2
2 Empty,{1},{2} 3
3 Empty,{1},{2},{3} 4
4 Empty,{1},{2},{3},{4},{1,3},{2,4} 7
5 Empty,{1},{2},{3},{4},{5},{1,3},{2,4},{3,5},{1,4},{2,5} 11
Write a second recursive function for computing the value of Cn. Here
break the computation in two parts according to whether the first duckling
is chosen or not. In addition to 𝑛 and the last duckling 𝑙 chosen so far, you
should also pass an indicator (flag) about the choice of the first duckling.
Note that you are not asked to find out the UTOPIC collections
themselves. You are asked only to compute their counts. These counts
can be obtained in a variety of ways. Here we urge you strongly to follow
the recursive strategy outlined above. This will help you in understanding
recursive behavior of functions. Following other algorithms may defeat
this basic purpose.
Sample output
Enter number of ducklings : 10
Number of UTOPIC collections in a line = 144
Number of UTOPIC collections in a circle = 123
Sample output
Enter numbers: 1, 2, 4
Subsets: {}, {1}, {2}, {4}, {1,2}, {2,4}, {1,4}, {1,2,4}
Enter Set: 5, 1
Entered set is a subset
—---------------------