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

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

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

Analysis of Algorithms. Introduction. Contents

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

Test One (Answer Key)

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

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

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

Algorithms and Data Structures Lecture IV

4.3 Growth Rates of Solutions to Recurrences

Chapter 6. Advanced Counting Techniques

CS:3330 (Prof. Pemmaraju ): Assignment #1 Solutions. (b) For n = 3, we will have 3 men and 3 women with preferences as follows: m 1 : w 3 > w 1 > w 2

Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

Sums, products and sequences

CS / MCS 401 Homework 3 grader solutions

Model of Computation and Runtime Analysis

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

Data Structures and Algorithms

Data Structures and Algorithm. Xiaoqing Zheng

V. Adamchik 1. Recursions. Victor Adamchik Fall of x n1. x n 2. Here are a few first values of the above sequence (coded in Mathematica)

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

Algorithm Analysis. Chapter 3

6.046 Recitation 5: Binary Search Trees Bill Thies, Fall 2004 Outline

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

Model of Computation and Runtime Analysis

Recurrence Relations

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

Mathematics review for CSCI 303 Spring Department of Computer Science College of William & Mary Robert Michael Lewis

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

Average-Case Analysis of QuickSort

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

Chapter 22 Developing Efficient Algorithms

Data Structures Lecture 9

Design and Analysis of Algorithms

Lecture 3: Asymptotic Analysis + Recurrences

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

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

Advanced Course of Algorithm Design and Analysis

Fundamental Algorithms

CSE 202 Homework 1 Matthias Springer, A Yes, there does always exist a perfect matching without a strong instability.

ORIE 633 Network Flows September 27, Lecture 8

The Discrete Fourier Transform

Bertrand s Postulate

Chapter 2. Asymptotic Notation

1 Generating functions for balls in boxes

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

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

Lecture 7: Solving Recurrences

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

UNIT #5. Lesson #2 Arithmetic and Geometric Sequences. Lesson #3 Summation Notation. Lesson #4 Arithmetic Series. Lesson #5 Geometric Series

Ch3. Asymptotic Notation

Math 475, Problem Set #12: Answers

Mathematical Foundation. CSE 6331 Algorithms Steve Lai

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

SNAP Centre Workshop. Basic Algebraic Manipulation

Zeros of Polynomials

The Growth of Functions. Theoretical Supplement

Modern Algebra 1 Section 1 Assignment 1. Solution: We have to show that if you knock down any one domino, then it knocks down the one behind it.

MATH 304: MIDTERM EXAM SOLUTIONS

Principle Of Superposition

COMP285 Midterm Exam Department of Mathematics

Ma 530 Introduction to Power Series

CS 332: Algorithms. Quicksort

An Introduction to Randomized Algorithms

NUMERICAL METHODS FOR SOLVING EQUATIONS

Merge and Quick Sort

MT5821 Advanced Combinatorics

CALCULATION OF FIBONACCI VECTORS

Sequences, Sums, and Products

2.4 - Sequences and Series

Discrete Mathematics Recurrences

Hand Out: Analysis of Algorithms. September 8, Bud Mishra. In general, there can be several algorithms to solve a problem; and one is faced

CSE 5311 Notes 1: Mathematical Preliminaries

9.3 The INTEGRAL TEST; p-series

Infinite Sequences and Series

Symbolic computation 2: Linear recurrences

5. Solving recurrences

In algebra one spends much time finding common denominators and thus simplifying rational expressions. For example:

Rademacher Complexity

Properties and Tests of Zeros of Polynomial Functions

NICK DUFRESNE. 1 1 p(x). To determine some formulas for the generating function of the Schröder numbers, r(x) = a(x) =

Generating Functions II

Problem 4: Evaluate ( k ) by negating (actually un-negating) its upper index. Binomial coefficient

MAT 271 Project: Partial Fractions for certain rational functions

CALCULUS BASIC SUMMER REVIEW

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

Precalculus MATH Sections 3.1, 3.2, 3.3. Exponential, Logistic and Logarithmic Functions

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

Calculus II - Problem Drill 21: Power Series, Taylor and Maclaurin Polynomial Series

Theorem: Let A n n. In this case that A does reduce to I, we search for A 1 as the solution matrix X to the matrix equation A X = I i.e.

Calculus 2 - D. Yuen Final Exam Review (Version 11/22/2017. Please report any possible typos.)

CSE 332. Data Structures and Parallelism

Signals & Systems Chapter3

Mathematics 116 HWK 21 Solutions 8.2 p580

CS161 Design and Analysis of Algorithms. Administrative

Recurrences: Methods and Examples

MATH2007* Partial Answers to Review Exercises Fall 2004

Hashing and Amortization

MATH 10550, EXAM 3 SOLUTIONS

Worksheet on Generating Functions

Transcription:

CIS 11 Data Structures ad Algorithms with Java Sprig 019 Code Sippets ad Recurreces Moday, February 4/Tuesday, February 5 Learig Goals Practice provig asymptotic bouds with code sippets Practice solvig recurreces usig the methods of iteratio ad recurrece trees Code Sippets We ca apply our kowledge of Big-Oh ad summatios to fid the ru time of a sippet of our code Besides recursio, ested iteratio is where our code s efficiecy will be bottleecked We should cosider the loop as a summatio, ad use our kowledge to simplify it from there Try startig from the iermost loop with fixed bouds ad workig outwards Problems Problem 1 Problem 1 a Provide a ruig time aalysis of the followig loop That is, fid both Big-Oh ad Big Ω: for ( it i = 0 ; i < ; i ++ for ( it j = i ; j <= ; j++ for ( it k = i ; k <= j ; k++ sum++; Problem 1 b Provide a ruig time aalysis of the followig loop: for ( it i = 4 ; i < ; i = i i for ( it j = ; j < Math s q r t ( i ; j = j+j System out p r i t l ( ; Recurreces As you have see i class, recurreces are equatios that ca help us describe the ruig time of a recursive algorithm You have thus far see two differet ways of solvig recurreces: Iteratio I this method, we expad T ( fully by substitutio ad solve for T ( directly Recursio trees I this method, we draw the recursive calls to T ( i a tree format ad cout the amout of work doe i each level of the tree I this lab, we will focus o the method of iteratio Let s first go through some examples before ruig through problems 1

Example: Method of Iteratio I Let s examie the followig recurrece T ( T ( 1 + 1 0 otherwise Usig the method of iteratio, we expad T ( as follows: T ( 1 + = [T ( + ( 1] + = [[T ( + ( ] + ( 1] + = i i=1 Example: Method of Iteratio II = 1 ( + 1( = + 1 = Θ( This is a more complex versio of the recurrece i problem 1c What makes this tricky is all of the algebraic maipulatio, but the strategy of settig the problem up is the same! Note: this is probably the most difficult thigs will get i terms of solvig recurreces via iteratio T + 1 This is tricky, but let s jump right i First, let s assume that is some power of such that = k = k = log T + ( = T + + ( ( = T + + + We ca also write T = T T = T + + k 1 T k 1 = k T k + k 1 k 1

If we add all of the equatios above ad subtract commo terms, we ed up with the followig (also usig the base case + + + + k 1 + k k 1 = k + k 1 ( i i=0 = k + k 1 1 ( = log log + 1 = log + ( log = log + c log c 1 = log = log = Θ( log That last step might be a bit cofusig We observe that log 06 = 05 + ɛ for some positive ɛ (We itroduce ɛ to make evaluatig the limit simpler We ca cofirm that T ( is i fact Θ( log via the limit defiitio of Big-Θ (c + 1 05+ɛ c 05 lim 05+ɛ = c + 1 Therefore, we have a Big-Θ Example: Method of Recursio Trees Let s examie the recurrece of merge sort For those that are ufamiliar with it, the algorithm works by takig a usorted array, sortig the left ad right halves of the array recursively, ad the mergig the two sorted halves together to ed up with the fial sorted list Let T ( represet the time the algorithm takes for a iput of size Sice the two halves are sorted recursively by the same algorithm, but with iputs that are each half the size of the origial, each half should take time T The mergig takes liear time So we ca write T + c for some costat c The recursio-tree is show below

Note that at the very top level (ie, the ed of the algorithm, it costs c to merge the two sorted halves of the array But to get there, we eeded to solve the two problems of size Each costs c to solve Therefore, across that level, the total cost is c + c = c We ca cotiue this all the way util we get to the very bottom of the tree, which are sigle elemets Note the that every level eds up costig c The height of the tree is lg Therefore, the total cost is c lg To see why the height is lg, observe that subproblem sizes decrease by a factor of each time we go dow oe level, we stop whe we reach sigleto elemets The subproblem size for a ode at depth i is Thus, the subproblem size hits = 1 whe i = 1 or, equivaletly, whe i = lg You ca read more i examples i CLRS 44 Problems You may assume i the followig cases that is either some power of or You may also fid the followig helpful: k i=0 Geometric series: q i = qk+1 1 q 1 = 1 qk+1 1 q Sum of biomial coefficiets over upper idex: j=0 ( j = m ( + 1 m + 1 4

Problem 1 Problem Solve the followig recurreces 1 4 5 T ( 1 + ( T + 1 T + 1 T ( 1 + 1 T ( + 1 1 Problem The CIS160 ad CIS11 TAs are havig aother social gatherig after goig to see the Pigeohole Priciple Players of Philadelphia (PPPP For some reaso or aother, the 160 TAs particularly ejoy playig the Tower of Haoi ad isist that the 11 TAs compete with them to see who ca solve the game the fastest Of course, the clever 11 TAs are very proficiet with their ruig time aalysis skills, ad kow that a optimal strategy is detailed by the followig code: fuctio HaoiTower(h, f rom, to, mid if h 1 the HaoiTower(h 1, from, mid, to MoveDisk(f rom, to HaoiTower(h 1, mid, to, from O(1 time operatio Problem What is the ruig time of the above algorithm? Prove it iductively 5