Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Similar documents
Divide and Conquer. Recurrence Relations

Algorithms, Design and Analysis. Order of growth. Table 2.1. Big-oh. Asymptotic growth rate. Types of formulas for basic operation count

data structures and algorithms lecture 2

Divide-and-Conquer Algorithms and Recurrence Relations. Niloufar Shafiei

Divide&Conquer: MergeSort. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Spring 2014

Algorithms And Programming I. Lecture 5 Quicksort

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Recurrences COMP 215

CPS 616 DIVIDE-AND-CONQUER 6-1

Data Structures and Algorithms CSE 465

Divide and Conquer. CSE21 Winter 2017, Day 9 (B00), Day 6 (A00) January 30,

Algorithms and Their Complexity

Divide & Conquer. CS 320, Fall Dr. Geri Georg, Instructor CS320 Div&Conq 1

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Selection and Adversary Arguments. COMP 215 Lecture 19

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

Notes for Recitation 14

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Review Of Topics. Review: Induction

COMP 382: Reasoning about algorithms

CS173 Running Time and Big-O. Tandy Warnow

CSCI 3110 Assignment 6 Solutions

Data Structures and Algorithms Chapter 3

Data Structures and Algorithms

Problem Set 1. CSE 373 Spring Out: February 9, 2016

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Fast Sorting and Selection. A Lower Bound for Worst Case

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Quicksort algorithm Average case analysis

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Lecture 14: Nov. 11 & 13

The maximum-subarray problem. Given an array of integers, find a contiguous subarray with the maximum sum. Very naïve algorithm:

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

CMPT 307 : Divide-and-Conqer (Study Guide) Should be read in conjunction with the text June 2, 2015

Partition and Select

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

5. DIVIDE AND CONQUER I

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

Advanced Analysis of Algorithms - Midterm (Solutions)

CS2223 Algorithms D Term 2009 Exam 3 Solutions

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

IS 709/809: Computational Methods in IS Research Fall Exam Review

5. DIVIDE AND CONQUER I

Divide-Conquer-Glue Algorithms

Chapter 5. Divide and Conquer CLRS 4.3. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Lecture 4. Quicksort

Asymptotic Analysis and Recurrences

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Data Structures in Java

CSC2100B Data Structures Analysis

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

Analysis of Algorithms

CS 470/570 Divide-and-Conquer. Format of Divide-and-Conquer algorithms: Master Recurrence Theorem (simpler version)

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Kartsuba s Algorithm and Linear Time Selection

Data Structures and Algorithms Chapter 3

Divide and Conquer Algorithms

Divide & Conquer. Jordi Cortadella and Jordi Petit Department of Computer Science

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: October 11. In-Class Midterm. ( 7:05 PM 8:20 PM : 75 Minutes )

Searching. Sorting. Lambdas

Solving Recurrences. 1. Express the running time (or use of some other resource) as a recurrence.

Divide and Conquer Strategy

COMP Analysis of Algorithms & Data Structures

Big-O Notation and Complexity Analysis

Divide and Conquer. Andreas Klappenecker

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 45

Lecture 6: Recurrent Algorithms:

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CSE 613: Parallel Programming. Lecture 9 ( Divide-and-Conquer: Partitioning for Selection and Sorting )

Divide and Conquer Algorithms

Recap: Prefix Sums. Given A: set of n integers Find B: prefix sums 1 / 86

Recursion. Computational complexity

Advanced Counting Techniques. Chapter 8

CSE 421 Algorithms. T(n) = at(n/b) + n c. Closest Pair Problem. Divide and Conquer Algorithms. What you really need to know about recurrences

Divide and Conquer CPE 349. Theresa Migler-VonDollen

Sorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort

Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Algorithms, CSE, OSU Quicksort. Instructor: Anastasios Sidiropoulos

Bucket-Sort. Have seen lower bound of Ω(nlog n) for comparisonbased. Some cheating algorithms achieve O(n), given certain assumptions re input

CS313H Logic, Sets, and Functions: Honors Fall 2012

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

Asymptotic Algorithm Analysis & Sorting

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Asymptotic Analysis, recurrences Date: 9/7/17

Fundamental Algorithms

Fundamental Algorithms

Computational Complexity

Divide-and-Conquer Algorithms Part Two

Algorithm Design and Analysis

Introduction to Divide and Conquer

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Chapter 4 Divide-and-Conquer

CS 344 Design and Analysis of Algorithms. Tarek El-Gaaly Course website:

Divide and Conquer. Andreas Klappenecker. [based on slides by Prof. Welch]

An analogy from Calculus: limits

Recurrence Relations

Transcription:

Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) { Algorithms Test 1 for (k = 1; k <= j; k++) { something that takes O(1) } // end for k j = j * 2 } // end while j } // end for i a) Analyze the above algorithm to determine an obvious big-oh notation, O( ). b) Analyze (by tracing) the above algorithm to determine its theta notation, ( ). Question 2. (10 points) Suppose that you have an (n 5 ) algorithm that required 10 seconds to run on a problem size of 1,000. How long would you expect the algorithm to run on a problem size of 10,000? 1

Question 3. (10 points)the general idea of 4-way merge sort is as follows. Assume n items to sort. Divide the unsorted part into quarters to get four smaller sorting problems of about equal size = n/4 Conquer/Solve the smaller problems recursively using 4-way merge sort Merge the solution to the smaller problems together using two levels of merging Unsorted size n Divide Conquer recursively Merge quarters pairs into halves Sorted size n/2 Sorted size n/2 Sorted size n Merge halves to get whole sorted array. Note: Assume that the same merge code is used as in the 2-way merger sort algorithm discussed in class. a) In the worst-case, how many total comparisons are performed to merge the quarters into two halves followed by merging the two halves into the whole sorted array? b) Write a recurrence relation for the 4-way merge sort's worst-case number of compares, including base-case(s). W(n) = W(1) = 2

Question 4. (15 points) The "Master Theorem" (Theorem B.5) for solving most common recurrences is as follows: Suppose a complexity function T(n) is eventually nondecreasing and satisfies the recurrence T(n) = at(n/b) + cn k for n > 1, n a power of b T(1) = d, where constant a > 0, b is an integer 2, c > 0, d 0, and k is an integer 0. Then T(n) { θ ( n k ) θ ( n k log n) θ ( n log b a ) if a < b k if a = b k if a > b k Use the Master Theorem to solve both of the following recurrence relations: a) T(n) = 4T( n ) 2 + 3n 3 T(1) = 2 b) T(n) = 5T( n ) 4 + 4n T(1) = 3 Question 5. (15 points) The general idea of Quick sort is as follows. Assume n items to sort. Select a random item (e.g., the first item) in the unsorted part as the pivot item Rearrange (called partitioning) the unsorted items such that: low Pivot Point high Pivot All items < Pivot Item All items >= Pivot Quick sort the unsorted part to the left of the pivot item Quick sort the unsorted part to the right of the pivot item Explain why Quick sort is ( n 2 ) in the worst-case. 3

Question 6. (15 points) Theorem B.1 Let the homogeneous linear recurrence equation with constant coefficients a 0 t n + a 1 t n 1 + a 2 t n 2 +... +a k t n k = 0 be given. If the characteristic equation a 0 r k + a 1 r k 1 + a 2 r k 2 +... +a k r 0 = 0 has k distinct solutions r 1, r 2,..., r k, then the only solutions to the recurrence are t n = c 1 r 1 n + c 2 r 2 n + c 3 r 3 n +... +c k r k n where the c i terms are arbitrary constants. (The value of these c i can be determined by the initial/base-case conditions, but we generally can stop here to find the theta-notation.) Theorem B.2 Let r be a root of multiplicity m of the characteristic equation for a homogeneous linear recurrence equation with constant coefficients. Then, the general solution includes the terms t n =... + c 1 r n + c 2 nr n + c 3 n 2 r n +... +c m 1 n m 1 r n +... where the c i terms are arbitrary constants. Theorem B.3 A nonhomogeneous linear recurrence equation of the form a 0 t n + a 1 t n 1 + a 2 t n 2 +... +a k t n k = b n p(n) can be transformed into a homogeneous linear recurrence that has the characteristic equation (a 0 r k + a 1 r k 1 + a 2 r k 2 +... + a k r 0 )(r b) d+1 = 0, where b is a constant and p(n) is a polynomial of degree d. If there is more than one term like b n p(n) on the right-hand side, each one contributes a (r b) d+1 term to the characteristic equation. a) For the recurrence: t n = 5 t n-1-6 t n-2 + n 2 4 n for n > 0 t 0 = 4 Obtain the characteristic equation Factor the characteristic equation to find the roots Write the general solution to the characteristic equation and determine its theta notation. b) For the recurrence: T(n) = 4T( n ) 2 + 3n 3 T(1) = 2 for n > 2 with n a power of 2 (n = 2 k and log 2 n = k) Obtain the characteristic equation Factor the characteristic equation to find the roots Write the general solution to the characteristic equation and determine its theta notation. 4

For the last question, you can choose which of the problems to solve (do only one)! Question 7. (25 points) Let A be an array indexed from 0 to n-1. Also, let A contains n different integer values that are sorted in ascending order (from smallest to largest). Give a divide-and-conquer algorithm that finds an index i such that A[i] = i (if one exists) and runs in O(log 2 n) time. If such an index does not exist, then it should return -1. Question 7. (25 points) The longest ascending subsequence problem is defined as follows. Given an array A of n integers, find the length of the longest ascending subsequence in A. For example, n = 8 A: 0 1 2 3 4 5 6 7 5 8 2 3 9 8 5 7 The longest ascending subsequence is of length 3 from A[2] to A[4]. Write a dynamic programming algorithm to solve this problem. Hint: Construct an n element array B such that B[i] contains the length of the longest ascending subsequence that starts at A[i]. Fill B in one pass by starting at one end and filling to the other. You may describe your algorithm in English or high-level language (C++, Java, Python, Ada, etc.) pseudo-code. 5