CPSC 320 Midterm 2 Thursday March 13th, 2014

Similar documents
CPSC W2 Midterm #2 Sample Solutions

Midterm solutions. n f 3 (n) = 3

Lecture 5: Sorting Part A

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Multiple-choice (35 pt.)

Divide and Conquer 4-0

CSC Design and Analysis of Algorithms

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CSci 231 Final Review

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Lower Bound on Comparison-based Sorting

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Data Structures and Algorithms Week 4

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

Data Structures and Algorithms Chapter 4

Analysis of Algorithms - Quicksort -

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

CISC 621 Algorithms, Midterm exam March 24, 2016

MergeSort. Algorithm : Design & Analysis [5]

Searching, Sorting. part 1

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

Pseudo code of algorithms are to be read by.

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Divide-and-Conquer. Dr. Yingwu Zhu

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CS 303 Design and Analysis of Algorithms

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

4. Sorting and Order-Statistics

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Class Note #02. [Overall Information] [During the Lecture]

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

The divide and conquer strategy has three basic parts. For a given problem of size n,

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Lecture Notes for Advanced Algorithms

Mergesort again. 1. Split the list into two equal parts

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

100 points total. CSE 3353 Homework 2 Spring 2013

ECE250: Algorithms and Data Structures Midterm Review

Basic Data Structures (Version 7) Name:

Recurrences and Divide-and-Conquer

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Module 2: Classical Algorithm Design Techniques

Quicksort (Weiss chapter 8.6)

Divide-and-Conquer CSE 680

1 Probabilistic analysis and randomized algorithms

EECS 2011M: Fundamentals of Data Structures

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

II (Sorting and) Order Statistics

Test 1 Review Questions with Solutions

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

Examination Questions Midterm 2

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

Divide and Conquer Algorithms

Data Structures and Algorithms. Roberto Sebastiani

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

O(n): printing a list of n items to the screen, looking at each item once.

INDIAN STATISTICAL INSTITUTE

SORTING AND SELECTION

Problem Set 6 Due: 11:59 Sunday, April 29

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

Questions Total Points Score

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Design and Analysis of Algorithms

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Principles of Algorithm Design

Algorithms and Data Structures

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

Matriculation number:

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

We can use a max-heap to sort data.

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Chapter 8 Sort in Linear Time

Transcription:

CPSC 320 Midterm 2 Thursday March 13th, 2014 [12] 1. Answer each question with True or False, and then justify your answer briefly. [2] (a) The Master theorem can be applied to the recurrence relation { T ( n T (n) = 2/3 ) + n if n 2 Θ(1) if n = 1 to prove that T (n) Θ(n). This is false: T ( n 2/3 ) can not be written as T (n/b) for any positive real number b, and so this recurrence does not have the form needed to apply the Master theorem (its solution is in Θ(n), however). [2] (b) A divide and conquer algorithm always splits a problem into approximately equalsized subproblems. This is false. Most of them do, but it is not required (Quicksort is an example of a divide and conquer algorithm that does not usually split the problem into approximately equal-sized subproblems). [2] (c) The worst-case performance for the Randomized Caching algorithm discussed in class is at least as good as the worst-case performance for the Least-Recently-Used Caching algorithm. This is true: both algorithms are marking algorithms, and hence their worst-case performance is at most k times the performance of the optimal algorithm. [2] (d) Let S be a skip list with 25 keys. It is possible that every node of S, except for the head node and the tail node (if the implementation uses one), will have only a single level. This is true; it s very unlikely, but it s possible. [2] (e) Amortized analysis is used to obtain a tight bound on the running time of one operation on a data structure. This is false: it is used to obtain a tight bound on the running time of a sequence of operations on a data structure. [2] (f) When we use the potential method for amortized analysis, the potential of a data structure can only become negative after an operation has been performed if it becomes 0 during the execution of the next operation. This is false: a valid potential function can never be negative.

page 2 [7] 2. In class, we proved that given the recurrence { at (n/b) + f(n) if n n0 T (n) = Θ(1) if n < n 0 and a value n = b t, the function T (n) is equal to t 1 Θ(n log b a ) + a j f(n/b j ). j=0 [2] (a) Where did the term Θ(n log b a ) come from? This is the work being done by the leaves of the recursion tree. [2] (b) Where did the term t 1 j=0 aj f(n/b j ) come from? the leaves. This is the work being done at all levels of the recursion tree other than [3] (c) How is this formula related to the three cases of the Master Theorem? The three cases are simply cases where we can evaluate the summation t 1 j=0 aj f(n/b j ), and determine which of it or the term Θ(n log b a ) is the dominating term. [9] 3. Prove upper and lower bounds on the function T (n) defined by { 2T (n/2) + 8T (n/4) + n 2 if n 4 T (n) = 1 if n 3 Your grade will depend on the quality of the bound you provide (so, proving that T (n) Ω(1) and T (n) O(100 n ), while true, will not give you many marks). Here are the first three levels of the recursion tree (some of the repeated nodes have been removed, and replaced by an indication that there are 2 of these since the tree would not have fit on the page otherwise).

page 3 The children of the root do 2 n 2 /4 + 8 n 2 /16 = n 2 /2 + n 2 /2 = n 2 work. The grand-children of the root do 4 n 2 /16 + 16 n 2 /64 + 16 n 2 /64 + 64 n 2 /256 = n 2 /4 + n 2 /4 + n 2 /4 + n 2 /4 = n 2 work as well. As we can see, the children of a node do the same amount of work, together, as their parent. Hence the work done on every level of the tree is exactly n 2, at least up to the level containing the leaf closest to the root (after which the amount of work done starts decreasing). The tree contains log 2 n levels, since the path along which the size of the subproblems decreases slowest is that where the size is divided by 2 when we go from one level to the next. Hence the total amount of work done is in O(n 2 log n) (recall that the base of the log does not matter when we use asymptotic notations, because log x n is within a constant factor of log y n). For the lower bound, notice that the first leaf occurs at level log 4 n, and that every level up to that one does exactly n 2 work, and so the total amount of work done is in Ω(n 2 log n). [13] 4. The Hidden Surface Removal problem occurs in Computer Graphics and can be described as follows: you are given a collection of coloured objects, which are projected onto a screen to produce an image. The objects are at different distances (depth) from the screen; objects that are closer cover those that are further back. The problem is to decide how to color each point of the screen. In this question we consider the one-dimensional version of the problem: each object is an interval on the x-axis, with a depth and a color, and can be represented by a 4-tuple (x l, x r, d, c). For instance, (0, 4, 3, blue) is an interval that goes from x = 0 to x = 4, has depth 3, and color blue. [10] a. Describe an efficient divide-and-conquer algorithm that takes as input a collection of n shapes, and returns a list of 4-tuples (x l, x r, d, c) where such a 4-tuple indicates that the points from x = x l to x = x r should be colored c (and the line segment with that color is at depth d). For instance, given the shapes (2, 5, 1, red), (9, 10, 6, red), (0, 4, 3, blue), (1, 8, 5, green) your algorithm should return (0, 2, 3, blue), (2, 5, 1, red), (5, 8, 5, green), (9, 10, 6, red).

page 4 (the vertical bars were added as separators between colors because the exam is printed in black and white). More space is available for your answer on the next page. Note: the solution is presented here in a lot more details than I expected you to provide on the exam. The first part alone would have been worth 5/10, even if you provided no details about how the merge is carried out. We mimic the Mergesort algorithm. The algorithm will return the 4-tuples sorted from left to right (the ordering simplifies the merge operation considerably). Algorithm OneDSurfaceRemoval(S, first, last) if first = last then return S[first... first] mid (first + last)/2 scene1 OneDSurfaceRemoval(S, first, mid) scene2 OneDSurfaceRemoval(S, mid + 1, last) return OneDSurfaceMerge(scene1, scene2) The merge stage takes in two arrays of intervals (with depths and colors) that are already sorted from left to right. No two intervals in one array overlap, although of course intervals in the first array may overlap with intervals in the second array. The algorithm proceeds from left to right. It maintains a variable xmin that contains the smallest value of x that has not yet been assigned a color. At each step, we consider one interval from each subproblem, and decide how to deal with them up to the end of the first one to terminate. There are 3 cases: i. There is no interval immediately to the right of x = xmin. In this case we simply advance xmin to the beginning of the next interval. No output is produced. ii. There is only one interval immediately to the right of x = xmin. We add the portion of this interval that comes before the start of the first interval in the other array to our output, and update xmin. iii. There are two intervals immediately to the right of x = xmin. Only one of them will be visible until one of the two terminates. We add that one to the output, and then update xmin. In every case we move xmin from one endpoint of an interval to another endpoint of an interval. Since there are at most O(n) interval endpoints when we have n intervals, this merge process thus runs in O(n) time. Pseudo-code follows (it was not required on the exam! I will only be looking for the right ideas): Algorithm OneDSurfaceMerge(scene1, scene2) i 0 j 0

page 5 S { } xmin while (i < length[scene1] and j < length[scene2]) do Case 1 if xmin < scene1[i].xl and xmin < scene2[j].xl then xmin min (scene1[i].xl, scene2[j].xl) Case 2 (with one interval). if xmin scene1[i].xl and xmin < scene2[j].xl then if scene1[i].xr scene2[j].xl then add (xmin, scene1[i].xr, scene1[i].depth, scene1[i].col) to S xmin scene1[i].xr i i + 1 add (xmin, scene2[j].xl, scene1[i].depth, scene1[i].col) to S xmin scene2[j].xl Case 2 again (with the other interval). if xmin scene2[j].xl and xmin < scene1[i].xl then if scene2[j].xr scene1[i].xl then add (xmin, scene2[j].xr, scene2[j].depth, scene2[j].col) to S xmin scene2[j].xr add (xmin, scene1[i].xr, scene2[j].depth, scene2[j].col) to S xmin scene1[i].xl Case 3 (the interval in scene 1 finishes first, or maybe both). if scene1[i].xr scene2[j].xr if scene1[i].depth < scene2[j].depth then add (xmin, scene1[i].xr, scene1[i].depth, scene1[i].col) to S add (xmin, scene1[i].xr, scene2[j].depth, scene2[j].col) to S

page 6 xmin scene1[i].xr i i + 1 if (xmin = scene2[j].xr) then Case 3 (the interval in scene 2 finishes first). if scene2[j].xr scene2[i].xr if scene1[i].depth < scene2[j].depth then add (xmin, scene2[j].xr, scene1[i].depth, scene1[i].col) to S add (xmin, scene2[j].xr, scene2[j].depth, scene2[j].col) to S xmin scene2[j].xr endwhile Now we need to collect the remaining intervals in each scene. Only one of these loops will actually execute. while i < length[scene1] do add (xmin, scene1[i].xr, scene1[i].depth, scene1[i].col) to S i i + 1 if i < length[scene1] then xmin scene1[i].xl endwhile while j < length[scene2] do add (xmin, scene2[j].xr, scene2[j].depth, scene2[j].col) to S if j < length[scene2] then xmin scene2[j].xl endwhile return S

page 7 After this, there may be multiple consecutive intervals with the same depth and color. Consolidating these is left as an exercise. [3] b. Analyze the running time of your algorithm. The running time of the algorithm can be described by the same recurrence relation as the running time of Mergesort, and so it is in Θ(n log n). [4] 5. Mr. Isulo, an alien computer scientist, decided he does not like the definition of good pivot used in the expected time analysis of the RandomizedQuickSelect algorithm. Instead, he decides to call a pivot good if n/10 leftsize n 1 n/10. That is, good pivots are any pivot that ends up in the middle 80% of the array after the call to RandomizedPartition. What recurrence relation would Mr. Isulo get for the expected time E(T (n)) of algorithm RandomizedQuickSelect using his definition of good pivot? The recurrence relation for Mr. Isulo s algorithm is: E(T (n)) = { E(T (9n/10)) + Θ(n) if n 10 Θ(1) if n 9. The expected value of g(n) in this case is 1.25, which is a constant.