COMP285 Midterm Exam Department of Mathematics

Similar documents
Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse Zurich Phone:

Matriculation number: You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

Analysis of Algorithms. Introduction. Contents

Data Structures Lecture 9

A recurrence equation is just a recursive function definition. It defines a function at one input in terms of its value on smaller inputs.

Test One (Answer Key)

CS 270 Algorithms. Oliver Kullmann. Growth of Functions. Divide-and- Conquer Min-Max- Problem. Tutorial. Reading from CLRS for week 2

Divide & Conquer. Divide-and-conquer algorithms. Conventional product of polynomials. Conventional product of polynomials.

CS / MCS 401 Homework 3 grader solutions

CS 332: Algorithms. Linear-Time Sorting. Order statistics. Slide credit: David Luebke (Virginia)

Chapter 22 Developing Efficient Algorithms

Analysis of Algorithms -Quicksort-

Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

CS583 Lecture 02. Jana Kosecka. some materials here are based on E. Demaine, D. Luebke slides

Model of Computation and Runtime Analysis

Model of Computation and Runtime Analysis

Lecture 3: Asymptotic Analysis + Recurrences

CSE 4095/5095 Topics in Big Data Analytics Spring 2017; Homework 1 Solutions

Design and Analysis of Algorithms

Data Structures and Algorithm. Xiaoqing Zheng

Fundamental Algorithms

Average-Case Analysis of QuickSort

Classification of problem & problem solving strategies. classification of time complexities (linear, logarithmic etc)

Design and Analysis of ALGORITHM (Topic 2)

This Lecture. Divide and Conquer. Merge Sort: Algorithm. Merge Sort Algorithm. MergeSort (Example) - 1. MergeSort (Example) - 2

4.3 Growth Rates of Solutions to Recurrences

Merge and Quick Sort

Divide and Conquer. 1 Overview. 2 Multiplying Bit Strings. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

A Probabilistic Analysis of Quicksort

Ch3. Asymptotic Notation

COMP26120: Introducing Complexity Analysis (2018/19) Lucas Cordeiro

Algorithm Analysis. Algorithms that are equally correct can vary in their utilization of computational resources

Mathematical Foundation. CSE 6331 Algorithms Steve Lai

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

OPTIMAL ALGORITHMS -- SUPPLEMENTAL NOTES

CS161: Algorithm Design and Analysis Handout #10 Stanford University Wednesday, 10 February 2016

Data Structures and Algorithms

Algorithms and Data Structures Lecture IV

62. Power series Definition 16. (Power series) Given a sequence {c n }, the series. c n x n = c 0 + c 1 x + c 2 x 2 + c 3 x 3 +

Divide and Conquer. 1 Overview. 2 Insertion Sort. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

COMP26120: More on the Complexity of Recursive Programs (2018/19) Lucas Cordeiro

2. ALGORITHM ANALYSIS

Enumerative & Asymptotic Combinatorics

Lecture Notes for CS 313H, Fall 2011

Recurrence Relations

Sums, products and sequences

SCORE. Exam 2. MA 114 Exam 2 Fall 2016

Recursive Algorithm for Generating Partitions of an Integer. 1 Preliminary

Area As A Limit & Sigma Notation

CSI 2101 Discrete Structures Winter Homework Assignment #4 (100 points, weight 5%) Due: Thursday, April 5, at 1:00pm (in lecture)

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, February 4/Tuesday, February 5

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

Chapter 5.4 Practice Problems

Let us give one more example of MLE. Example 3. The uniform distribution U[0, θ] on the interval [0, θ] has p.d.f.

CS161 Design and Analysis of Algorithms. Administrative

CS 332: Algorithms. Quicksort

Sequences, Mathematical Induction, and Recursion. CSE 2353 Discrete Computational Structures Spring 2018

10.1 Sequences. n term. We will deal a. a n or a n n. ( 1) n ( 1) n 1 2 ( 1) a =, 0 0,,,,, ln n. n an 2. n term.

) n. ALG 1.3 Deterministic Selection and Sorting: Problem P size n. Examples: 1st lecture's mult M(n) = 3 M ( È

Infinite Sequences and Series

SCORE. Exam 2. MA 114 Exam 2 Fall 2017

MATH 2300 review problems for Exam 2

MATH 2300 review problems for Exam 2

Algorithms. Elementary Sorting. Dong Kyue Kim Hanyang University

Singular Continuous Measures by Michael Pejic 5/14/10

Linear chord diagrams with long chords

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

1 Counting and Stirling Numbers

Unit 6: Sequences and Series

SCORE. Exam 2. MA 114 Exam 2 Fall 2016

Divide and Conquer II

ITEC 360 Data Structures and Analysis of Algorithms Spring for n 1

Please do NOT write in this box. Multiple Choice. Total

Economics 250 Assignment 1 Suggested Answers. 1. We have the following data set on the lengths (in minutes) of a sample of long-distance phone calls

WRITTEN ASSIGNMENT 1 ANSWER KEY

Skip Lists. Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 S 3 S S 1

Advanced Course of Algorithm Design and Analysis

It is always the case that unions, intersections, complements, and set differences are preserved by the inverse image of a function.

CS161 Handout 05 Summer 2013 July 10, 2013 Mathematical Terms and Identities

Algorithms Design & Analysis. Divide & Conquer

SOLUTIONS TO EXAM 3. Solution: Note that this defines two convergent geometric series with respective radii r 1 = 2/5 < 1 and r 2 = 1/5 < 1.

1 Cabin. Professor: What is. Student: ln Cabin oh Log Cabin! Professor: No. Log Cabin + C = A Houseboat!

Math 155 (Lecture 3)

Chapter 2. Asymptotic Notation

Examples: data compression, path-finding, game-playing, scheduling, bin packing

CS4115 Database. 20. What is the time-complexity of squaring a matrix of size n n in Big-Oh notation?

The log-behavior of n p(n) and n p(n)/n

DATA STRUCTURES I, II, III, AND IV

6 Integers Modulo n. integer k can be written as k = qn + r, with q,r, 0 r b. So any integer.

CSE 332. Data Structures and Parallelism

Math 116 Practice for Exam 3

n m CHAPTER 3 RATIONAL EXPONENTS AND RADICAL FUNCTIONS 3-1 Evaluate n th Roots and Use Rational Exponents Real nth Roots of a n th Root of a

Ma 530 Introduction to Power Series

The picture in figure 1.1 helps us to see that the area represents the distance traveled. Figure 1: Area represents distance travelled

Chapter 6. Advanced Counting Techniques

On a Smarandache problem concerning the prime gaps

Sequence A sequence is a function whose domain of definition is the set of natural numbers.

Fall 2018 Exam 2 PIN: 17 INSTRUCTIONS

Homework 9. (n + 1)! = 1 1

The multiplicative structure of finite field and a construction of LRC

Transcription:

COMP85 Midterm Exam Departmet of Mathematics Fall 010/011 - November 8, 010 Name: Studet Number: Please fiish withi 90 miutes. All poits above 100 are cosidered as bous poit. You ca reach maximal 1 poits. Good Luck!!Mustafa Riza 1. Asymptotic Notatios: (a) Use the formal defiitios of O, Θ, ad Ω to determie weather the followig assertios are true or false. i. ( + 1)/ O( 3 ) ( + 1)/ ( 3 1 lim 3 = lim + ) 1 3 = lim 1 + 1 Therefore the statemet is true. ii. 0 + 7 + 3 Θ() = 0 iii. lim 0 + 7 + 3 = lim Therefore the statemet is true. 3 O() 0 + 7 + 3 = lim 0 + 7 + 3 = 0 = cost Therefore the statemet is true. (b) Cosider the followig Algorithm: Algorithm 1 Eigma(A[0.. 1, 0.. 1] lim 3 = lim /3 = 0 1: {Iput: A matrix A[0.. 1, 0.. 1] of real umbers} : for i 0 to do 3: for j i + 1 to 1 do 4: if A[i, j] A[j, i] the 5: retur false 6: ed if 7: ed for 8: ed for 9: retur true (a) What does this algorithm compute? ( P) This program checks if the matrix is symmetric or ot. 1

(b) What is its basic operatio? (3 P) The basic operatio is the compariso of the matrix elemets. (c) How may times is the basic operatio executed ad what is the efficiecy class of this altorithm? Depedig o if the matrix is symmetric or ot the umber of comparisos differ. I the best case the first compariso shows that the matrix is ot symmetric so i the best case we have Θ(1) efficiecy class. I the worst case we have to perform / comparisos, so i the worst case we have Θ( ) efficiecy class. I average we have to calculate the followig: 1 1 i = i=0 ( 1) = 1 Θ(). Recurrece Relatios: (10 P) (a) Solve the recurrece relatio: x() = x( 1) +, for > 0, x(0) = 0 x() = x( 1) + x() = x( ) + ( 1) + x() = x( 3) + ( 3) + ( 1) +. =. x() = x(0) + 1 + + + x() = 1 ( + 1) (b) Solve the recurrece relatio: Substitute = k x() = x(/) +, for > 1, x(1) = 1 (Solve for = k ) x() = x( k ) + k x() = x( k ) + k 1 + k x() = x( k 3 ) + k + k 1 + k. =. x() = x(0) + 1 + + + k + k 1 + k x() = 1 + 1 + + + k + k 1 + k k x() = i = k+1 1 i=0 back substitutio k = x() = 1

3. Brute Force Algorithms: (1 P) (a) For the the Selectio Sort Algorithm give below Algorithm SelectioSort(A[0.. 1] 1: {Sorts a give array by selectio sort} : {Iput: A usorted Array A[0.. 1] of elemets.} 3: {Output: A sorted Array A[0.. 1] i ascedig order.} 4: for i 0 to do 5: mi i 6: for j i + 1 to 1 do 7: if A[j] < A[mi] the 8: mi j 9: ed if 10: swap A[i] ad A[mi] 11: ed for 1: ed for (a) Please sort the list A, L, G, O, R, I, T, H, M i alphabetical order usig Selectio Sort Algorithm give below. A L G O R I T H M A G L O R I T H M A G H O R I T L M A G H I R O T L M A G H I L O T R M A G H I L M T R O A G H I L M O R T A G H I L M O R T (b) What is the basic operatio of the selectio sort algorithm? ( P) The basic operatio is compariso of elemets. (c) What is the efficiecy class of the selectio sort algorithm? The efficiecy class is Θ( ) 4. Divide ad Coquer: (35P) (a) Usig the Master Theorem, fid the order of growth of the followig recurreces: Recallig the Master Theorem: Theorem 1 If f() Θ( d ) with d 0 i the recurrece relatio T () = at (/b) + f(), the Θ( d ) if a < b d T () = Θ( d log ) if a = b d Θ( log b a ) if a > b d. i. T () = 4T (/) +, T (1) = 1. Accordig to the master theorem 1 we have a = 4, b =, d = 1, so we have the case of 4 > 1, i.e. T () = Θ( log 4 ) = Θ( ) 3

ii. T () = 4T (/) + 3, T (1) = 1. Accordig to the master theorem 1 we have a = 4, b =, d = 3, so we have the case of 4 < 3, i.e. T () = Θ( 3 ) (b) Apply the mergesort algorithm to sort the list A, L, G, O, R, I, T, H, M i alphabetical order. ALGORITHM ALGOR ITHM ALG OR IT HM AL G O R I T H M A L OR IT HM AL HIMT AGL AGLOR AGHILMORT (c) Give is the Quicksort Algorithm as followig: The partitio fuctio partitios the subarray usig a pivot key, where all elemets less tha the pivot key are put ito the array A[l..s 1] ad all elemets greater tha the pivot key are put ito the array A[s + 1..r]. The pivot key will be put as first elemet of the secod array, i.e. A[s]. i. State the recurrece relatio for umber of key comparisos for the best case ad solve this recurrece relatio to get the exact umber of key comparisos. (10 P) 4

Algorithm 3 Quicksort(A[l..r] 1: {Sorts a subarray by quicksort} : {Iput: A usorted subarray A[l..r] of A[0.. 1], defied by its left ad right idices l ad r} 3: {Output: Subarray A[l..r] i odecreasig order.} 4: if l < r the 5: s P artitio(a[l..r]) {s is the split positio} 6: Quicksort(A[l..s 1]) 7: Quicksort(A[s..r]) 8: ed if I the best case the Quicksort algorithm works like the Mergesort algorithm so its recursio relatio is give as: C() = C(/) + Usig the Master Theorem Theorem 1 we ca easily verify that C() Θ( log ). ii. Describe the worst case sceario ad state the umber of comparisos i the worst case sceario usig the Θ otatio. I the worst case the pivot key is either the smallest or the biggest elemet of the list, so that the other elemets are either all greater or all smaller. So the we ca ot really say that we have a partitio. I this case the Algorithm works like the selectio sort algorithm ad its efficiecy class is Θ( ). iii. Sort the list A, L, G, O, R, I, T, H, M usig the quicksort algorithm for the partitio fuctio usig the first elemet of the subarray as pivot key. A G I H L O R T M A G I H L M O R T 5. Biary Trees: (15 P) (a) Draw the biary search tree by isertig the elemets of the list L, O, G, A, R, I, T, H, M oe by oe to the biary search tree. (8 P) L G O A I M R H T (b) Perform pre-, i-, ad postorder traversal for the biary search tree i questio 5a. (7 P) pre-order: i-odert: post-order: L G A I H O M R T A H I G M T R O L 5

6. Decrease ad Coquer: Give is the followig Isertio Sort Algorithm: (15 P) Algorithm 4 IsertioSort(A[l..r]) 1: {Sorts a array by isertiosort} : {Iput: A usorted array A[0.. 1] of elemets.} 3: {Output: A array A[0.. 1] sorted i odecreasig order.} 4: for i 1 to 1 do 5: v A[i] 6: j i 1 7: while j 0 ad A[j] > v do 8: A[j + 1] A[j] 9: j j 1 10: ed while 11: A[j + 1] v 1: ed for (a) Use the isertio sort algorithm to sort the array A, L, G, O, R, I, T, H, M. A G L O R I T H M A G L O R I T H M A G L O R I T H M A G I L O R T H M A G I L O R T H M A G H I L O R T M Note: The bold face letters deote the sorted array. We start at the secod positio as oe elemet i a array is by defiitio already sorted. The we go oe by oe through the array by comparig startig from the biggest elemet to the smallest elemet i the sorted array. Oce we have foud the correct positio we isert the elemet ito the correct positio. (b) Describe the worst case sceario for the isertio sort algorithm ad state the umber of key comparisios usig the Θ otatio. I the worst case the array has to be sorted i reverse order so we have to carry out 1 + + 3 + 1 comparisos, i.e. Θ( ) comparisos. (c) Describe the best case sceario for the isertio sort algorithm ad state the umber of key comparisos usig the Θ otatio. I the best case the array is already sorted. Therefore for each step we have to perform exactly oe compariso, i.e. i total we have to perform Θ() comparisos for a array with elemets. 6