Solutions for Algorithm Design Exercises and Tests

Similar documents
CMSC351 - Fall 2014, Homework #2

Multiple-choice (35 pt.)

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Data Structures and Algorithms Week 4

CSE 332, Spring 2010, Midterm Examination 30 April 2010

EECS 2011M: Fundamentals of Data Structures

Binary Search to find item in sorted array

Test 1 Review Questions with Solutions

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

Data Structures and Algorithms Chapter 4

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

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Chapter 4: Trees. 4.2 For node B :

CSC236H Lecture 5. October 17, 2018

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

Figure 4.1: The evolution of a rooted tree.

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Multi-Way Search Trees

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

CSC236 Week 5. Larry Zhang

Algorithms and Data Structures

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Midterm solutions. n f 3 (n) = 3

COMP Analysis of Algorithms & Data Structures

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

Chapter 3 Dynamic programming

Divide and Conquer Algorithms

UNIT 1 BASICS OF AN ALGORITHM

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Recurrences and Divide-and-Conquer

COMP Analysis of Algorithms & Data Structures

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

COMP Analysis of Algorithms & Data Structures

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

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

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

DATA STRUCTURES AND ALGORITHMS

Algorithms and Data Structures for Mathematicians

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Recursively Defined Functions

Jalil Boudjadar, Tommy Färnqvist. IDA, Linköping University

CPSC 320 Midterm 2 Thursday March 13th, 2014

Multi-Way Search Trees

Chapter 8 Sort in Linear Time

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

Lamé s Theorem. Strings. Recursively Defined Sets and Structures. Recursively Defined Sets and Structures

Agenda. We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them

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

This chapter covers recursive definition, including finding closed forms.

Section 05: Solutions

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

CS2 Algorithms and Data Structures Note 6

Solutions to Exam Data structures (X and NV)

II (Sorting and) Order Statistics

CS2 Algorithms and Data Structures Note 6

Recursive Definitions Structural Induction Recursive Algorithms

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

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

Design and Analysis of Algorithms

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

Algorithms. AVL Tree

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

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

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Section 05: Solutions

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

COMP 250 Winter 2010 Exercises 6 - trees March 2010

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

Analysis of Algorithms

Propositional Logic. Andreas Klappenecker

Algorithm 23 works. Instead of a spanning tree, one can use routing.

Search Trees. Undirected graph Directed graph Tree Binary search tree

Computer Science 302 Spring 2007 Practice Final Examination: Part I

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

Data Structure and Algorithm Midterm Reference Solution TA

Selection (deterministic & randomized): finding the median in linear time

Lower bound for comparison-based sorting

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

CSCE 411 Design and Analysis of Algorithms

Data Structures and Algorithms. Roberto Sebastiani

Properties of red-black trees

Foundations of Computer Science Spring Mathematical Preliminaries

CSE Winter 2015 Quiz 2 Solutions

Data Structures and Algorithms CSE 465

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

Trees and Inductive Definitions

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Sample Solutions to Homework #2

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

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

More Complicated Recursion CMPSC 122

AVL Trees. See Section 19.4of the text, p

Transcription:

Chapter 4 Solutions for Algorithm Design Exercises and Tests 4.1 Divide and Conquer 4.1.1 Solutions for Selected Exercises Solution for Exercise #1 in Section 1.9 Solution for Part (a): This problem requires a recursive algorithm to produce a balanced binary search tree storing the first n positive integers, given n as input, and its analysis. Suppose that a binary tree is to be given by a pointer to the root node of the tree, and that nodes are structures (or objects, or records) whose components include the value stored, and a left child and right child, each of which is a pointer to another node. A sketch of the algorithm to be written was also given in the question. In order to solve part of the problem using this algorithm, it is useful to write an auxiliary function, add to value, which takes an integer k and a pointer p to a node as input, and which adds k to the value of every node in the subtree whose root is pointed to by p. In the following pseudocode, node will be used to declare something to be a pointer to a node and, if p has this type, then p will represent the node that p points to. add to value(k : integer, p : node) if p null then p.value := p.value + k add to value(k, p.left child) add to value(k, p.right child) It can be argued that if the pointer p points to a binary tree of size s (for any integer s 0), then the above function uses Θ(s) pointer dereferences and accesses of data, arithmetic operations, and assignments of values to variables. Now, a function build tree, which takes a positive integer n as input and returns a pointer to a balanced binary search tree storing the first n positive integers, corresponds to the pseudocode shown in Figure 4.1 (on page 104). 103

build tree(n : integer) : node p, q1, q : node; middle : integer if n 0 then return null else middle := n+1 q1 := build tree(middle 1) q := build tree(n middle) add to value(middle, q ) new(p) p.value := middle p.left child := q1 p.right child := q return p Figure 4.1: Algorithm build tree Solution for Part (b): Let T (n) be the number of operations used by the function build tree, when it is given a positive integer n as input. Since this function calls the function add to value with a pointer to a binary tree of size n middle = n n+1 = n 1 as input, and since middle 1= n 1, it can be shown that T (n) satisfies a recurrence c1 if n 1 T (n) T ( n 1 )+T( n 1 )+c n if n, for positive constants c 1 and c, and that it also satisfies a recurrence ĉ1 if n 1 T (n) T ( n 1 )+T( n 1 )+ĉ n if n, for (smaller) positive constants ĉ 1 and ĉ. Now, suppose that R(n) satisfies the recurrence 1 if n 1 R(n) = R( n 1 )+R( n 1 )+n if n. Then, it can be shown that T (n) Θ(R(n)), so it s sufficient to find an asymptotic bound in closed form for R(n) instead. Exercise: If you d like to have more practice, use the substitution method to prove that T (n) Θ(R(n)). It can also be shown that R(n) is a nondecreasing function of n. Exercise: If you d like to have more practice in the use of mathematical induction, then prove that R(n) R(n + 1) for every integer n 1 using induction on n. 104

Solution for Part (c): The fact that R is a nondecreasing function implies that R( n 1 ) R( n n 1 ) and that R( ) R( n ), and that if S(n) satisfies the recurrence 1 if n 1, S(n) = S( n )+n if n, then R(n) S(n) for every integer n 1 so, that, in particular, R(n) O(S(n)). Now, the master theorem can be used to establish that S(n) Θ(n log n), and this implies that R(n) O(n log n), and hence that T (n) O(n log n) as well. We would also like to show that R(n) Ω(n log n). To do this, note first that since R(n) isan increasing function, R( n 1 ) R( n 1 ), and that if U(n) satisfies the recurrence 1 if n 1, U(n) = U( n 1 )+n if n, then R(n) U(n) for every integer n 1. The recurrence for U(n) isstill not in the form required so that we can apply the master theorem (although it s getting closer). Now, let V (n) =U(n 1) for all n. Then V (n) =U(n 1)=1ifn, and if n> then Now, finally, set V (n) =U(n 1) =U( n )+n 1 (since n 1 ) =U( n 1) + n 1 (since n 1 = n 1) =V( n )+n 1. 1 W (n) = if n 1, W ( n )+n if n. Then, W (1) = 1 < V(1), W () = 1 V(), and one can show using induction on n that V (n) W (n) for every integer n 1. (Note that W (1) was defined to be 1 instead of 1 in order to ensure that all these inequalities are satisfied: If W (1) had been set to be 1 and W () had been defined using the recursive definition shown above, then W () would have turned out to be greater than V (), so that the above claim V (n) W (n) for every integer n 1 would have been false.) Now (at last) the recurrence for W (n) has a form that allows us to apply the master theorem. This theorem implies that W (n) Θ(n log n). Since V (n) W (n) for all n 1, it follows that V (n) Ω(n log n). Since U(n) =V(n+ 1), U(n) Ω((n + 1) log(n +1))=Ω(nlog n), too. Finally, since R(n) U(n) for every positive integer n, it follows that R(n) Ω(n log n). Since we have already shown that R(n) O(n log n), it follows that R(n) Θ(n log n). Thus, Θ(R(n))=Θ(nlog n), and we can conclude that T (n) (the running time of the algorithm we started with) is in Θ(n log n), since we had already observed that T (n) Θ(R(n)). Exercise: If you want still more practice, try to confirm directly that T (n) Θ(n log n) without performing changes of recurrences, and so on, but by working with the original recurrences (shown at the bottom of the first page of these solutions) instead. 105

Solution for Exercise # in Section 1.9 This question required an algorithm that generalizes the one obtained in the solution for Exercise #1 above and the analysis of the new algorithm. In particular, we now wish to write an algorithm which accepts two integers n and k as inputs (with n being nonnegative) and which produces a balanced binary search tree with nodes k, k + 1,...,k+n 1 as output. An algorithm that solves this problem (and agrees with the description given in the question) is shown in Figure 4., which is shown below. build tree(n, k : integer) : node p, q1, q : node; middle : integer if n 0 then return null else middle := n 1 q1 := build tree(middle, k) q := build tree(n middle 1, k + middle +1) new(p) p.value := k + middle p.left child := q1 p.right child := q return p Figure 4.: Algorithm build tree In order to convince yourself that the algorithm is correct, consider the cases n =0,n= 1, and n =, and then try to prove using induction on n that the algorithm always returns the required binary search tree, after that. It isn t too hard to see that this algorithm has a running time T (n) that s a function of the input n and that satisfies a recurrence c 1 if n 0, T (n) = ( ) ( ) T n 1 + T n 1 + c if n 1. for positive constants c 1 and c, assuming that pointer dereferences, reading the current values of variables, assignments of values to variables, and integer arithmetic are all counted as having unit cost. Using this recurrence it s easy to confirm that T (0) = c 1, T (1) = c +c 1, T()=c +3c 1, and T (3)=3c +4c 1. After this you might guess that T (n) =c n+(n+1)c 1 =(c 1 +c )n+c 1 for every integer n 0 and it s easy to use induction on n to prove that this is correct. 106

An alternative strategy (for finding a solution for the above recurrence) would be to guess that it has the same asymptotic complexity as a function U(n) (that is, it s in Θ(U(n))), where ĉ1 if n 1, U(n) = U ( n ) +ĉ if n, so that U(n) satisfies a similar recurrence, but also so that an asymptotic bound for U(n) can be obtained using the master theorem. The master theorem could then be used to confirm that U(n) Θ(n), and the substitution method could then be used to prove that T (n) Θ(n) as well. Thus there are several ways to prove that T (n) Θ(n) (assuming the unit cost criterion as it s given above). Note that this implies that the above generalized algorithm is asymptotic more efficient than the algorithm that was given as an answer for the previous question, as this question suggested. 107