



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Solution: int sumOfDigits(int i) if(i==0) return 0; else return (i mod 10) + sumOfDigits(i div 10) c. A partition of a positive integer n is a way to write n as a sum of positive integers. For example, 7=3+2+1+1 is a partition of 7. Give a recursive algorithm to find the number of partitions of a positive integer. Solution: int countPartitions(int i,int maxAllowed) if((i==1)or(maxAllowed==1)) return 1; if(maxAllowed>i) return countPartitons(i,i); if(maxAllowed==i) return 1+maxPartitions(i,maxAllowed-1); return countPartitions(i,maxAllowed-1)+ countPartitions(i-maxAllowed,maxAllowed); The idea: we want to order the partitions by the largest number in the partition, so we prevent counting the same partition twice (with different order of elements) for example: if we want to get all the partitions of 4 we get: {4},{3,1},{2,2},{2,1,1},{1,1,1} In the code above, 'maxAllowed' keeps track of what's the maximal number we can use in partitioning a number. To get the number of partitions for n, we should call: countPartitions(n,n). d. What is the number of recursive calls these functions make (both the one you gave in (a) (b) and in (c)). Note that here we're asking you what's Solution: (a) log 2 (n); (b) log 10 (n); (c) A tight limit here is difficult to give, but observe that if we consider a variable that is a sum of 'i' and 'maxAllowed' then we can come up with the following definition: P(n,m)<=P(n-1,m)+P(n,m-1) where P(1,m)=1 and P(n,1)=1 has no more recursive calls than: P(m+n)=P(m+n-1)+P(m+m-1)=2P(m+n-1) Since the initial call is P(n,n), the upper bound on the number of recursive calls is: 2 2n.
if n = 0 then recFindA (n) := 1 else if n = 1 then recFindA (n) := 2 else if n = 2 then recFindA (n) := 3 else recFindA (n) := recFindA (n-1) + recFindA (n-2) + recFindA (n-3) 4 points, 1 for each case of the conditional. b. Devise an iterative algorithm to find the n th term of the sequence defined in part a. Solution: procedure iterFindA (n: non-negative integer) if n = 0 then iterFindA (n) := 1 else if n = 1 then iterFindA (n) := 2 else if n = 2 then iterFindA (n) := 3 else begin x := 1 y := 2 z := 3 sum := x + y + z for i := 3 to n begin x := y y := z z := sum sum := x + y + z end end iterFindA (n) := sum 4 points, 2 points for setting up the base cases appropriately, 1 for setting up the variables, 1 for the correct sum. c. Is the algorithm from part a or the algorithm from part b more efficient? Explain. Solution:
Algorithm from part b is more efficient. Assume each addition operation takes constant time, then part a has a complexity of (^3 ) O n while part b is O^ (^ n )^. Details: Notice that in part a, the recursive algorithm calls itself three times in the procedure body. Each of the three procedures will call itself three times respectively and so on. This “recursion” goes on until one of the base cases is hit and returns. We can imagine the process of recursively calling itself to be the process of constructing a tree. The root, recFindA (n), has three children, namely, recFindA (n-1), recFindA (n-2), and recFindA (n-3). Each of these children grows its own subtree. (Take recFindA (n-1) for example, it has recFindA (n-2), recFindA (n-3), and recFindA (n-4) as its children). Leaf nodes are just base cases, recFindA (0), recFindA (1), and recFindA (2). As stated above, this inefficient recursive algorithm has a time complexity O ( 3 n ) because of its tree structure (each internal nodes has 3 children). Why this exponential growth happens? It is because we have redundant computations. For example, we want the result of recFindA (6). We need recFindA (5), recFindA (4), and recFindA (3). Then we first compute recFindA (5). In this procedure, we need recFindA (3). Similarly, recFindA (4) needs the result of recFindA (3). So we at least compute recFindA (3) three times. Each time, we compute recFindA (3) from scratch. They are repetitive and therefore redundant. Is there a way to improve this? The answer is yes. We just reuse the result previously computed. That is, we only compute recFindA (3) once. When we encounter it the second time, we just use the result computed in the first round. Implementation is not covered by CS173. On the other hand, the iterative algorithm only takes linear time. Basically, it is a loop (for, while, etc.). In each iteration, we do two things: update three variables and compute a temporary result by adding three variables. Obviously, it is linear to the input index n: we have O(n) iterations, each has a fixed set of operations. 4 points. 2 for explaining the inefficiency of the tree structure, and 2 for explaining the contrasting efficiency of the iterative algorithm.