NOTE:. Attempt all seve questios. Major CSL 02 2. Write your ame ad etry o o every sheet of the aswer script. Time 2 Hrs Max Marks 70 Q No Q Q 2 Q 3 Q 4 Q 5 Q 6 Q 7 Total MM 6 2 4 0 8 4 6 70 Q. Write a iterative (tail recursive) fuctio i SML for the followig recursive fuctio. The fuctio is of the type f : N Z It takes a iput as a atural umber ad produces a positive iteger. Show the ru of your fuctio o f(6). 6 if = 0 f ( ) = { f ( ) otherwise Aswer: fu f() = fu fiter(,p,c) = if c= the p else fiter(,c+-p,c+); i fiter(,,0) ed; f(6) = f_iter(6,,0) = f_iter(6,0,) = f_iter(6,2,2) = f_iter(6,,3) = f_iter(6,3,4) = f_iter(6,2,5) = f_iter(6,4,6) => 4 (sice c = )
Q2. As follows is a iterative fuctio i SML to compute the x. What is the ivariat i the fuctio? Prove the correctess of the fuctio usig Priciple of Mathematical Iductio. Give the order of the time complexity of the fuctio with explaatio (formal proof is ot required). 2 fu power(x, ) = fu power_iter(p, q, m) = fu odd(m) = (m mod 2 = ); i if m=0 the p else if odd(m) the power_iter(p*q, q*q, m div 2) else power_iter(p, q*q, m div 2) ed i power_iter(, x, ) ed; Aswer: Ivariat: (0<=m<=) ad pq m = p 0 x (Here p 0 =) Correctess: If we show power_iter(p,x,) = px the we have power(x,) = power_iter(,x,) = x Basis: = 0 piter(p,x,) = p = px 0 Iductio Hypothesis: for m 0<=m<=- power_iter(p,x,m) = px m Iductio Step:. If is odd (2k+) for k>=0 (the div 2 = k) power_iter(p, x, ) = power_iter(p*x, x*x, div 2) =p*x*(x*x) div2 (by IH) =p*x*(x) div2 *(x) div2 =p*x*(x) div2 *(x) div2 =p*x*x - (div2 = k ad 2k+=) =p*x*x - =p*x 2. If is eve (2k) for k>=0 (the div 2 = k) power_iter(p, x, ) = power_iter(p, x*x, div 2) =p*(x*x) div2 (by IH) = p*x Complexity: O(log 2 ):The algorithm does successive halvig, that makes O(log) calls which have (2-3)operatios of multiplicatio/ divisio. Also if we cosider =2 r the a recurrece relatio T() = T(/2) + c ca be writte ad the solutio of which gives O(log) 2
Q3. Write a sigle fuctio SpecialSort i SML to sort a list of iteger elemets such that the list is sorted i ascedig order ad there is o duplicatio of elemets. You ca however, defie local fuctios (usig ) withi the SpecialSort fuctio if eed be. Please ote that your program should ot remove the duplicates before or after the sortig of the list as a pre or post processig step, it should be doe durig the sortig procedure. The algorithm should have a worst case time complexity of order O(log 2 ). 4 Aswer: It is a modifed murge sort, merge sort is the oly soritg method that was studied that gives O(log) worst case time complexity. fu SpecialSort([]) = [] SpecialSort ([x]) = [x] SpecialSort (ls) = fu split(ls) = fu iter([],l,l2,i) = (l,l2) iter(x::xs,l,l2,i) = if (i mod 2 = 0) the iter(xs,x::l,l2,i+) else iter(xs,l,x::l2,i+) i iter(ls,[],[],0) ed; val (l,l2) = split(ls); fu merge([],l2) = l2 merge(l,[]) = l merge(x::xs,y::ys) = if (x < y) the x::merge(xs,y::ys) else if (x > y) the y::merge(x::xs,ys) else x::merge(xs,ys) i merge(specialsort(l),specialsort(l2)) ed; 3
Q4. The k th momet about the mea of the sequece [x, x 2, x 3,.x ] is give as follows. k th momet = i = ( x i x) k Where x = i= x i You are give the high order fuctios mymap ad myreduce as defied below. Write a SML fuctio momet(l, k) to compute k th momet usig these fuctios. I case you eed ay other fuctio defie it before you use it. The iput to the fuctio momet are the list of reals [x, x 2, x 3,.x ]. 0 exceptio Emptylist; fu myreduce(f,il)=raise Emptylist myreduce(f,[a]) = a myreduce(f,x::xs) = F(x,myreduce(F,xs)); fu mymap(f,il) = il mymap(f,x::xs) = F(x)::mymap(F,xs) Aswer: fu power x = if =0 the.0 else x*(power (-) x); fu plus(x:real,y)=x+y; fu legth(il) = 0.0 legth(x::xs) =.0 + legth(xs); (* assumig myreduce ad mymap available as above *) fu momet(l,i) = val = legth(l); val avg = myreduce(plus,l)/ i myreduce(plus,mymap(power i, mymap(f(x)=>x-avg,l)))/ ed; 4
Q5. What does the followig part of a JAVA program compute? A ad B are arrays of itegers. What is the time complexity? Ca you do it better i terms of computatioal time? If so give the chaged code ad the improvemet i time of computatio. 8 it a; for (i=0; i<; i++) { a = 0; for (it j=0; j<; j++) a = a + A[j]; B[i] = a/; Aswer: The above program computes the average of A[] ad puts it i B[]. The complexity is O( 2 )for the reaso there are two ested loops. The program ca be improved as follows: it a; a = 0; for (i=0; i<; i++) a = a + A[j]; a = a/; for (it i=0; i<; i++) B[i] = a; This makes the time complexity O(). 5
Q6. A array A of size N cotais three possible values:, 2, 3 i a radom order. Give a efficiet algorithm to partitio the array such that all etries come first, the all 2 s ad the all 3 s, see the example give. You ca use commo costructs of Java like if-the-else, while, for loop, etc., to write your algorithm. Your algorithm should ot use ay additioal array ad should sca the array A oly oce ad should have the time complexity of O(N). The program should ot cout the umber of s, 2 s ad 3 s. The algorithm should be based o compariso ad swappig, for swap or exchage you ca defie a fuctio ad use it. Give the ivariat property of the loop used i your algorithm. 4 Sample Iput 2 3 3 2 2 2 Sample Output 2 2 2 2 3 3 Algorithm: We ca cosider three couters lo, mid ad hi. Let the array A be defied to N lo = ; mid =; hi = N; while (mid <= hi) do if a[mid] == { swap a[lo] ad a[mid] lo++; mid++; else if a[mid] == 2 mid++; else swap a[mid] ad a[hi] hi--; ed swap (a, b) { temp =a; a = b; b =temp; Ivariat: At ay stage algorithm should have A[ lo-] = A[lo mid-] = 2 A[hi+ N] = 3 A[mid hi] =? (to be discovered) lo mid hi N That is -----> -2- ---?-- ----3-----> 6
Q7. A polyomial of degree is defied as follows. P ( x ) = a + a x + a x 2 + a x 3 0 2 3 +... + a x You have see array implemetatio of polyomials i Java. Whe may of the coefficiets are 0 (zero), i.e., the polyomial is sparse, it may ot be efficiet to use array to represet the polyomial. Therefore it is suggested to use liked lists to store/represet polyomials. That is, a polyomial i x, such as P(x) = 4.0 x 20 + 9.0 x 6-6.0 x 5 + 3.0 ca be more efficietly stored as a liked list. Defie the class Node i Java for the liked list to represet a polyomial. How ca you fid the first derivative of the polyomial usig liked list? Give the steps for your algorithm i plai Eglish (you are ot required to write the Java code, if you wat you ca). Aswer: class Node { it exp; double coeff; Node lik; Node (c, e, Node ) { Coeff = c; exp = e; lik = ; The derivative ca also be represeted by a list as the polyomial. The derivative list ca be foud:. Traverse the polyomial list ad check if the e >=, 2. The ode ca be added (attached) to the derivative list with the data fields as coeff = c*e; ad exp = e-, where c ad e correspod to the data (coeff ad exp) of the curret ode i the polyomial list. 3. The may be reversed if eed be (ot required) 6 7