Midterm Exam. CS 3110: Design and Analysis of Algorithms. June 20, Group 1 Group 2 Group 3

Similar documents
CPSC 320 (Intermediate Algorithm Design and Analysis). Summer Instructor: Dr. Lior Malka Final Examination, July 24th, 2009

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

COMP 382: Reasoning about algorithms

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

CPSC 320 Sample Final Examination December 2013

COMP Analysis of Algorithms & Data Structures

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

Analysis of Algorithms - Using Asymptotic Bounds -

COMP Analysis of Algorithms & Data Structures

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Problem 5. Use mathematical induction to show that when n is an exact power of two, the solution of the recurrence

DM507 - Algoritmer og Datastrukturer Project 1

Fall 2017 November 10, Written Homework 5

Lecture 4. Quicksort

COMP 355 Advanced Algorithms

MIDTERM I CMPS Winter 2013 Warmuth

Analysis of Algorithms

Advanced Analysis of Algorithms - Midterm (Solutions)

COMP 355 Advanced Algorithms Algorithm Design Review: Mathematical Background

CS 577 Introduction to Algorithms: Strassen s Algorithm and the Master Theorem

Fall 2016 Test 1 with Solutions

Discrete Math in Computer Science Solutions to Practice Problems for Midterm 2

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

CS161: Algorithm Design and Analysis Recitation Section 3 Stanford University Week of 29 January, Problem 3-1.

Math 391: Midterm 1.0 Spring 2016

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Advanced Analysis of Algorithms - Homework I (Solutions)

CS 361, Lecture 14. Outline

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

Inf 2B: Sorting, MergeSort and Divide-and-Conquer

CS173 Running Time and Big-O. Tandy Warnow

Introduction to Divide and Conquer

Data Structures and Algorithms Running time and growth functions January 18, 2018

Module 1: Analyzing the Efficiency of Algorithms

Week 5: Quicksort, Lower bound, Greedy

Asymptotic Analysis and Recurrences

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Review Of Topics. Review: Induction

Chapter 2. Recurrence Relations. Divide and Conquer. Divide and Conquer Strategy. Another Example: Merge Sort. Merge Sort Example. Merge Sort Example

Mergesort and Recurrences (CLRS 2.3, 4.4)

Mid-term Exam Answers and Final Exam Study Guide CIS 675 Summer 2010

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Problem Set 1 Solutions

Chapter 4 Divide-and-Conquer

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

CS 5321: Advanced Algorithms - Recurrence. Acknowledgement. Outline. Ali Ebnenasir Department of Computer Science Michigan Technological University

Outline. 1 Introduction. Merging and MergeSort. 3 Analysis. 4 Reference

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

CS 5321: Advanced Algorithms Analysis Using Recurrence. Acknowledgement. Outline

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

CS 4407 Algorithms Lecture 2: Growth Functions

NATIONAL UNIVERSITY OF SINGAPORE CS3230 DESIGN AND ANALYSIS OF ALGORITHMS SEMESTER II: Time Allowed 2 Hours

Algorithms And Programming I. Lecture 5 Quicksort

COL106: Data Structures and Algorithms (IIT Delhi, Semester-II )

CS 350 Midterm Algorithms and Complexity

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

CS 161 Summer 2009 Homework #2 Sample Solutions

Analysis of Algorithms

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

Algorithms Design & Analysis. Analysis of Algorithm

Divide-and-Conquer Algorithms Part Two

Hon.Algorithms (CSCI-GA 3520), Professor Yap Fall 2012 MIDTERM EXAM. Oct 11, 2012 SOLUTION

CS2223 Algorithms D Term 2009 Exam 3 Solutions

CS Data Structures and Algorithm Analysis

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

MIDTERM Fundamental Algorithms, Spring 2008, Professor Yap March 10, 2008

Analysis of Algorithms

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt): Name of Neighbor to your right (1 pt):

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

Asymptotic Running Time of Algorithms

Analysis of Algorithms. Randomizing Quicksort

1 Terminology and setup

Data Structures and Algorithms Chapter 3

COMP 633: Parallel Computing Fall 2018 Written Assignment 1: Sample Solutions

Ch 01. Analysis of Algorithms

CS Non-recursive and Recursive Algorithm Analysis

CS1800 Discrete Structures Spring 2018 February CS1800 Discrete Structures Midterm Version A

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Written Homework #1: Analysis of Algorithms

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

Divide and Conquer Algorithms

CSCI 3110 Assignment 6 Solutions

Solving recurrences. Frequently showing up when analysing divide&conquer algorithms or, more generally, recursive algorithms.

CS 4104 Data and Algorithm Analysis. Recurrence Relations. Modeling Recursive Function Cost. Solving Recurrences. Clifford A. Shaffer.

Algorithm Design and Analysis

ECE608 Midterm 1 Spring 2009 THEN PUT YOUR ADDRESS ON EVERY PAGE OF THE EXAM! NAME: Neatness counts. We will not grade what we cannot read.

Ch01. Analysis of Algorithms

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Time Analysis of Sorting and Searching Algorithms

Module 1: Analyzing the Efficiency of Algorithms

A design paradigm. Divide and conquer: (When) does decomposing a problem into smaller parts help? 09/09/ EECS 3101

Transcription:

Banner ID: Name: Midterm Exam CS 3110: Design and Analysis of Algorithms June 20, 2006 Group 1 Group 2 Group 3 Question 1.1 Question 2.1 Question 3.1 Question 1.2 Question 2.2 Question 3.2 Question 3.3 Instructions: The questions are divided into three groups. You have to answer all questions in Groups 1 and 2 and one question in Group 3. In the above table, put a check mark in the small box beside the question in Group 3 you want me to mark. Provide your answer in the box after each question. If you absolutely need extra space, use the backs of the pages; but try to avoid it. Keep your answers short and to the point. You are not allowed to use a cheat sheet. For the design questions (Group 3), if you cannot design an algorithm that achieves the stated running time, design a slower algorithm that is correct. A correct and slow algorithm earns you 50% of the marks. A fast and incorrect algorithm earns 0 marks. In the design questions, you are allowed to use algorithms and data structures you learnt in class as black boxes, without explaining how they work, as long as these algorithms and data structures do not directly answer the question. Read every question carefully before answering. In particular, do not waste time on an analysis if none is asked for, and do not forget to provide one if it is required. Do not forget to write your banner number and name on the top of this page. This exam has 13 pages, including the title page. Notify me immediately if your copy has fewer than 13 pages. 1

Question 1.1 (Asymptotic running time) 15 marks a. Explain the following two terms. Worst-case running time The worst-case running time of an algorithm is T(n) if there is no input of size n on which the algorithm takes longer than T(n) time, and there is an input of size n on which the algorithm takes T(n) time. Average-case running time The average-case running time of an algorithm is T(n) if T(n) is the average over the running times of the algorithm for all possible inputs of size n. b. Given two algorithms A and B with running times T A (n) and T B (n), what does it mean that T A (n) = O(T B (n)). If T A (n) = O(T B (n)), then for sufficiently large n, algorithm A is by at most a constant factor slower than algorithm B. 2

Question 1.2 (Asymptotic growth) 20 marks a. Sort the following functions by order of growth: n lg n n 3/2 lgn 2 log 4 n 1. 2 log 4 n = n 2. n lg n 3. n 3/2 lg n b. Prove that the function f(n) in box 1 and the function g(n) in box 2 satisfy f(n) = o(g(n)). lim n n n lg n = lim n = 0. 1 n lg n c. Prove that the function g(n) in box 2 and the function h(n) in box 3 satisfy g(n) = o(h(n)). lim n n lg n n 3/2 / lg n = lim lg 2 n n n = = = ( lim n ( lim n ( lim n = 0 2 = 0. lg n n 1/4 ) 2 lg e(1/n) n 3/4 /4 4 lg e n 1/4 ) 2 ) 2 3

Question 2.1 (Algorithm analysis) 20 marks Consider the following procedure: MAGIC(x) x is an integer 1 if x > 0 2 then MAGIC( x/2 ) 3 if x is odd 4 then print 1 5 else print 0 a. Given a positive integer x, what does this procedure print on the screen? Do not prove your claim. (Your statement should be simple and shouldn t simply rephrase the computation of the algorithm in mathematical terms.) The procedure prints out x in binary. b. Let the input size be the value of x. Give a recurrence for the running time of the algorithm in terms of the value of x. T(x) = { Θ(1) x = 0 Θ(1) + T( x/2 ) x > 0 4

c. Solve this recurrence using whatever method you like, in order to determine the running time of procedure MAGIC. Observe that we have T(x) Θ(1) + T(x/2) for x > 0, that is, we can ignore the floor. Master theorem: In the language of the Master theorem, we have a = 1, b = 2, and f(n) = Θ(1). Since n log b a = n 0 = Θ(1) = Θ(f(n)), the second case applies, and we get T(x) = Θ(lg x). Substitution: We guess that T(x) = O(lg x), that is, T(x) c lg x, for some c > 0 and all x 2. For x {2, 3}, this is most certainly true because in this case T(x) = Θ(1), and we can choose c large enough so that T(x) c lg x. For x 4, we use induction: as long as c a. T(x) a + c lg(x/2) = a + c(lg x 1) = c lg x + (a c) c lg x, 5

Question 2.2 (Correctness proof) Consider the following procedure: 20 marks DIVISIBLEBYNINE(x) 1 if x < 10 2 then if x = 0 or x = 9 3 then return yes 4 else return no 5 else y 0 6 while x > 0 7 do y y + (x mod 10) 8 x x/10 9 return DIVISIBLEBYNINE(y) a. Argue that the algorithm always terminates, that is, that the algorithm does not call itself recursively until all eternity. (Hint: Prove that the value of y in Line 9 is less than the value of x at the beginning of the procedure and draw the right conclusions.) We prove this by induction on x. For x < 10, the algorithm obviously terminates because it executes Lines 1 4 and, which take constant time. For x 10, we prove that the value of y in Line 9 is less than x. Thus, for x 10, the algorithm spends constant time in Lines 1, 5 8 and then invokes itself on y in Line 9. By the induction hypothesis, the recursive call on y terminates. Thus, the current invocation on x terminates. To prove that x > y, let us write k x = x i 10 i. Then it is easy to see that This implies that x y = y = i=0 k x i. i=0 k (10 i 1)x i. i=0 Every term in this summation is non-negative because x i 0 and 10 i 1 0, for all 0 i k. Since x 10, there must be an i > 0 such that x i > 0. For this value of i, we have x i > 0 and 10 i 1 10 1 > 0. Thus, x y > 0, that is, x > y. 6

b. Prove that the algorithm always gives the correct answer. (Hint: Use induction. For the inductive step, argue that y is divisible by 9 if and only if this is true for x. Use the fact that 9 divides 10 k 1 for any integer k.) The proof is again by induction. For x < 10, the algorithm obviouly returns the correct answer because 0 and 9 are the only two numbers less than 10 that are divisible by 9. If x 10, we need to prove that x y is divisible by 9. Indeed, since y < x, we can assume inductively that DIVISIBLEBYNINE(y) returns yes if and only if y is divisible by 9. If y is divisble by 9, then y = 9y, for some integer y. Similarly x y = 9q, for some integer q. Thus, x = y + (x y) = 9y + 9q = 9(y + q), that is, x is divisible by 9, and the recursive call gives the correct answer for x. If y is not divisible by 9, then y = 9y + y, where y and y are integers and 0 < y < 9. Then x = y + (x y) = 9y + y + 9q = 9(y + q) + y, that is, x also leaves remainder y when divided by 9; x is not divisible by 9, and the no answer returned by the recursive call is again correct for x. IT remains to prove that x y is divisible by 9. We have observed in Part (a), howewer, that x y = k (10 i 1)x i. i=0 10 i 1 is divisible by 9 for any integer i. Thus, every term in the summation is divisible by 9 and, thus, x y is divisible by 9. 7

Question 3.1 (Greedy algorithms) 25 marks You are going on a trip from Halifax to San Francisco. You know that, along the route, there are gas stations at kilometers k 1, k 2,..., k n. You also know that your car can go m kilometers on a tank full of gas. Develop an algorithm that determines at which gas stations to stop so that a. The distance between two consecutive stops is at most m (that is, you can always get from one stop to the next on a tank full of gas) and b. You make as few stops as possible while respecting the previous condition. The running time of your algorithm should be linear. You do not have to prove that it is. Prove that your algorithm is correct, that is, that it indeed produces a sequence of stops that satisfies the above conditions. The question is in fact somewhat underspecified because, as it stands, we would have no reason ever to stop at the last gas station because we don t know how long the trip will continue after it. So let me assume here that the last gas station is in San Francisco (one can assume that a city of the size of San Francisco has a gas station), and let us assume that I want to arrive with a full tank. So I always stop at the last gas station to fill up the car. This leads to the following algorithm. ComputeStops(k, n, m) k is the array of gas station positions; n is the number of gas stations m is the mileage of my car 1 s 0 The position of my first stop, the start. 2 S The set of gas stations where I stop. 3 for i 1 to n 1 4 do if k[i + 1] s > m 5 then S S {k[i]} 6 s k[i] 7 return S {k n } The running time of the algorithm is obviously linear. In each step, we include the current gas station if we cannot make it to the next one from the last stop on one tank full of gas. Let S = (k i1, k i2,..., k iq, k n ) be the sequence of stops we compute, and let O = (k j1, k j2,..., k jr, k n ) be the optimal sequence. We prove by induction h that k ih k jh, for all h. This then implies that q r: Indeed, assume that q > r. Then we know that k n k jr m because, as O is an optimal sequence of stops, we must be able to reach for the stop at kilometer k jr to our final stop at kilometer k n. But k n k jr k n k ir because k jr k ir. Hence, k ir+2 k ir k n k ir m. This contradicts that we included stop k ir+1 in our sequence of stops. Thus, q r, and our stop sequence is as short as possible. It remains to prove the central claim: k ih k jh, for all h. For h = 1, the claim is most certainly true because our first stop is at kilometer k i1 because we could not reach kilometer k i1 +1 on a tank full of gas from the start kilometer 0. For h > 1, we distinguish two cases: If k ih 1 k jh, then the claim is trivial because k ih k ih 1. So let us assume that k ih 1 < k jh. In that case, our sequence S cannot include any stop at a kilometer strictly between k ih 1 and k jh because k jh k ih 1 k jh k jh 1 m. (Note that this uses the induction hypothesis: k ih 1 k jh 1.) This implies that k ih k jh, and we are done. 8

Extra space for Question 3.1: 9

Question 3.2 (Divide and conquer) 25 marks Let A = (x 1,..., x n ) be an array storing n numbers, and let [a 1, b 1 ],..., [a n, b n ] be n intervals with 1 a i b i n, for all 1 i n. Your task is to develop an algorithm which, for every interval [a i, b i ], computes the value m i = min{x j a i j b i }. Note that your algorithm has to compute all values m 1, m 2,..., m n at the same time. The running time of your algorithm should be O(n lg n). Do not analyze your algorithm and do not prove its correctness. The algorihm is based on the following idea: Split array A into pieces L = A[1.. n/2] and R = A[n/2 + 1.. n]. Any minimum m i with b i n/2 can be computed by recursively applying the algorithm to L. Similarly, any minimum m i with a i > n/2 can be computed recursively by applying the algorithm to R. Thus, the only minima we have to worry about are those where a i n/2 and b i > n/2. For these, we observe that m i = min(s ai, p bi ), where s j = min{a[j], A[j + 1],..., A[n/2]} and p j = min{a[n/2 + 1], A[n/2 + 2],..., A[j]}. What does this buy us? Well, once we have all the s j an p j values, we can compute all the m i that aren t computed in recursive calls in constant time. The s j and p j can be computed in linear time because s j = min(a[j], s j+1 ) and p j = min(p j 1, A[j]). Thus, we obtain the following algorithm: RANGEMINIMA(A, Q, l, r) A is the array; Q contains the query A[l.. r] is the subarray on which the current recursive call operates. 1 if Q = 2 then return immediately 3 if l = r 4 then for every interval I Q 5 do Output A[l] as the answer 6 else q l+r 2 7 s[q 1] A[q 1] 8 for i q 2 downto l 9 do s[i] min(s[i + 1], A[i]) 10 for i q + 1 to r 11 do p[i] min(p[i 1], A[i]) 12 Q l 13 Q r 14 for every interval [a, b] Q 15 do if a < q and b q 16 then OUtput min(s[a], p[b]) as the answer 17 else if b then < qq l Q l {[a, b]} 18 else Q r Q r {[a, b]} 19 RANGEMINIMA(A, Q l, l, q 1) 20 RANGEMINIMA(A, Q r, q, r) The running time of each invocation, excluding the recursive calls, is obviously linear in the size of the current subarray A[l.. r]. The two recursive calls are on subarrays of A[l.. r] containing only half the elements. Thus, the recurrence is the same as for MergeSort: T(n) = 2T(n/2) + Θ(n), which solves to T(n) = Θ(n lg n). 10

Extra space for Question 3.2: 11

Question 3.3 (Graph algorithms) 25 marks Given an undirected graph G, a 3-cycle is a triple of vertices (u, v, w) such that edges (u, v), (u, w), and (v, w) belong to G. Assume that G is given in adjacency list representation. Develop an O(nm)-time algorithm that finds all such cycles in G. Make sure that your algorithm reports every cycle exactly once. Prove that the running time of your algorithm is indeed O(nm). We start by observing that a 3-cycle is defined by an edge (u, v) whose endpoints u and v have a common neighbour w. Thus, the basic idea is to iterate over all m edges and identify all common neighbours of the endpoints of each of them. This would of course report every 3-cycle three times, once per edge. We ensure that we report it only once by outputting the 3-cycle (u, v, w) only for the edge (u, v) such that w > u, v in some numbering of the vertices. So our algorithm is as follows: Find3Cycles(G) 1 Number the vertices 1 through n in an arbitrary order. 2 For every vertex v G, sort the edges in its adjacency list E(v) by the endpoints not equal to v. 3 for every edge (u, v) G 4 do (u, x) the first edge in E(u) 5 (v, y) the first edge in E(v) 6 while (u, x) nil and (v, y) nil 7 do if x = y and x > u and x > v 8 then Output the 3-cycle (u, v, x) 9 (v, y) next edge in E(v) 10 (u, x) next edge in E(u) 11 else if x > y 12 then (v, y) next edge in E(v) 13 else (u, x) next edge in E(u) The running time of this algorithm is easy to analyze. Numbering the vertices takes O(n) time because there are n vertices. Sorting the adjacency lists takes O( E(v) lg E(v) ) per adjacency list E(v). Since E(v) < n (every vertex can be adjacent to at most n 1 other vertices), the cost per adjacency list is O( E(v) lg n), and the total cost is ( ) O E(v) lg n = O(m lg n) = O(mn). v G Finally, the cost of the while-loop is bounded by O(nm) because it has m iterations, one per edge, and every iteration spends constant time per entry in two adjacency list of total size at most 2n 2. 12

Extra space for Question 3.3: 13