Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Similar documents
Data Structures and Algorithms Part 1.4

Homework 1 Solutions MA 522 Fall 2017

Lecture 1: Introduction and Strassen s Algorithm

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

Algorithm. Counting Sort Analysis of Algorithms

Algorithm Design Techniques. Divide and conquer Problem

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

Math Section 2.2 Polynomial Functions

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Lecture 5. Counting Sort / Radix Sort

CSE 417: Algorithms and Computational Complexity

Chapter 3 Classification of FFT Processor Algorithms

1.2 Binomial Coefficients and Subsets

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

Module 8-7: Pascal s Triangle and the Binomial Theorem

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

. Written in factored form it is easy to see that the roots are 2, 2, i,

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Big-O Analysis. Asymptotics

Big-O Analysis. Asymptotics

condition w i B i S maximum u i

prerequisites: 6.046, 6.041/2, ability to do proofs Randomized algorithms: make random choices during run. Main benefits:

Polynomial Functions and Models. Learning Objectives. Polynomials. P (x) = a n x n + a n 1 x n a 1 x + a 0, a n 0

Recursion. Recursion. Mathematical induction: example. Recursion. The sum of the first n odd numbers is n 2 : Informal proof: Principle:

Minimum Spanning Trees

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

EE123 Digital Signal Processing

CIS 121. Introduction to Trees

Examples and Applications of Binary Search

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

2. ALGORITHM ANALYSIS

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

CMPT 125 Assignment 2 Solutions

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

Fundamental Algorithms

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

CS Polygon Scan Conversion. Slide 1

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

1 Graph Sparsfication

The Graphs of Polynomial Functions

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

9.1. Sequences and Series. Sequences. What you should learn. Why you should learn it. Definition of Sequence

6.854J / J Advanced Algorithms Fall 2008

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Computational Geometry

Σ P(i) ( depth T (K i ) + 1),

Data Structures and Algorithms. Analysis of Algorithms

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

2) Give an example of a polynomial function of degree 4 with leading coefficient of -6

arxiv: v2 [cs.ds] 24 Mar 2018

How do we evaluate algorithms?

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

Random Graphs and Complex Networks T

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

5.3 Recursive definitions and structural induction

Last class. n Scheme. n Equality testing. n eq? vs. equal? n Higher-order functions. n map, foldr, foldl. n Tail recursion

Analysis of Algorithms

Data Structures Week #9. Sorting

top() Applications of Stacks

Lower Bounds for Sorting

Math 3201 Notes Chapter 4: Rational Expressions & Equations

Elementary Educational Computer

Counting Regions in the Plane and More 1

Priority Queues. Binary Heaps

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Ones Assignment Method for Solving Traveling Salesman Problem

Name of the Student: Unit I (Logic and Proofs) 1) Truth Table: Conjunction Disjunction Conditional Biconditional

EE University of Minnesota. Midterm Exam #1. Prof. Matthew O'Keefe TA: Eric Seppanen. Department of Electrical and Computer Engineering

Alpha Individual Solutions MAΘ National Convention 2013

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

Array Applications. Sorting. Want to put the contents of an array in order. Selection Sort Bubble Sort Insertion Sort. Quicksort Quickersort

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

The number n of subintervals times the length h of subintervals gives length of interval (b-a).

LU Decomposition Method

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

APPLICATION NOTE PACE1750AE BUILT-IN FUNCTIONS

Analysis of Algorithms

Lecture 9: Exam I Review

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

What are we going to learn? CSC Data Structures Analysis of Algorithms. Overview. Algorithm, and Inputs

Arithmetic Sequences

n Some thoughts on software development n The idea of a calculator n Using a grammar n Expression evaluation n Program organization n Analysis

Our Learning Problem, Again

Fast Fourier Transform (FFT) Algorithms

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

PLEASURE TEST SERIES (XI) - 04 By O.P. Gupta (For stuffs on Math, click at theopgupta.com)

Transcription:

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