Discrete Mathematics: Logic. Discrete Mathematics: Lecture 17. Recurrence

Similar documents
Discrete Mathematics: Logic. Discrete Mathematics: Lecture 14. Recursive algorithm

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

Recurrence Relations

CSE 421 Dynamic Programming

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

6. DYNAMIC PROGRAMMING I

CSC2100B Data Structures Analysis

CS 2210 Discrete Structures Advanced Counting. Fall 2017 Sukumar Ghosh

6. DYNAMIC PROGRAMMING I

CS481: Bioinformatics Algorithms

CSE 21 Mathematics for

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

COMP 555 Bioalgorithms. Fall Lecture 3: Algorithms and Complexity

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

Coin Changing: Give change using the least number of coins. Greedy Method (Chapter 10.1) Attempt to construct an optimal solution in stages.

2. (25%) Suppose you have an array of 1234 records in which only a few are out of order and they are not very far from their correct positions. Which

Recurrences COMP 215

Generating Function Notes , Fall 2005, Prof. Peter Shor

Notes for Recitation 14

Chapter Summary. Mathematical Induction Strong Induction Well-Ordering Recursive Definitions Structural Induction Recursive Algorithms

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

Advanced Counting Techniques. 7.1 Recurrence Relations

Dynamic Programming( Weighted Interval Scheduling)

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

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

Week 7 Solution. The two implementations are 1. Approach 1. int fib(int n) { if (n <= 1) return n; return fib(n 1) + fib(n 2); } 2.

Dynamic Programming. Cormen et. al. IV 15

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

CSE 421 Weighted Interval Scheduling, Knapsack, RNA Secondary Structure

Copyright 2000, Kevin Wayne 1

Induction and recursion. Chapter 5

CS483 Design and Analysis of Algorithms

CS 580: Algorithm Design and Analysis

Discrete Math Notes. Contents. William Farmer. April 8, Overview 3

CSE 202 Dynamic Programming II

Advanced Counting Techniques

Divide and Conquer. Recurrence Relations

CS173 Running Time and Big-O. Tandy Warnow

Induction and Recursion

Greedy Algorithms My T. UF

Dynamic Programming: Interval Scheduling and Knapsack

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

Lecture 2: Divide and conquer and Dynamic programming

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

CS2223 Algorithms D Term 2009 Exam 3 Solutions

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

Fundamental Algorithms

Fundamental Algorithms

Dynamic Programming 1

Activity selection. Goal: Select the largest possible set of nonoverlapping (mutually compatible) activities.

The Divide-and-Conquer Design Paradigm

Advanced Counting Techniques. Chapter 8

Every subset of {1, 2,...,n 1} can be extended to a subset of {1, 2, 3,...,n} by either adding or not adding the element n.

Lecture 7: Dynamic Programming I: Optimal BSTs

6. DYNAMIC PROGRAMMING I

Introduction to Algorithms 6.046J/18.401J/SMA5503

Divide and Conquer Algorithms

Recursion: Introduction and Correctness

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

Data Structures in Java

Dynamic Programming. Weighted Interval Scheduling. Algorithmic Paradigms. Dynamic Programming

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

DAA Unit- II Greedy and Dynamic Programming. By Mrs. B.A. Khivsara Asst. Professor Department of Computer Engineering SNJB s KBJ COE, Chandwad

Math Circle: Recursion and Induction

CS483 Design and Analysis of Algorithms

Divide and Conquer. Andreas Klappenecker

Discrete Mathematics -- Chapter 10: Recurrence Relations

Divide and Conquer Algorithms

CHAPTER 8 Advanced Counting Techniques

CS Lunch. Dynamic Programming Recipe. 2 Midterm 2! Slides17 - Segmented Least Squares.key - November 16, 2016

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

CPS 616 DIVIDE-AND-CONQUER 6-1

data structures and algorithms lecture 2

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

12 Sequences and Recurrences

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

17 Advancement Operator Equations

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

Algorithm Design and Analysis

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

arxiv: v1 [math.co] 22 Jan 2013

CS Analysis of Recursive Algorithms and Brute Force

Design and Analysis of Algorithms

ICS141: Discrete Mathematics for Computer Science I

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

Algorithms and Theory of Computation. Lecture 9: Dynamic Programming

ECOM Discrete Mathematics

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

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

Algorithm Analysis Recurrence Relation. Chung-Ang University, Jaesung Lee

Data Structures and Algorithms Chapter 3

Appendix: Solving Recurrences

Applications. More Counting Problems. Complexity of Algorithms

MATH 324 Summer 2011 Elementary Number Theory. Notes on Mathematical Induction. Recall the following axiom for the set of integers.

Lecture 4. Quicksort

Sorting DS 2017/2018

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

CSI2101-W08- Recurrence Relations

Outline / Reading. Greedy Method as a fundamental algorithm design technique

Transcription:

Discrete Mathematics: Logic Discrete Mathematics: Lecture 17. Recurrence

greedy algorithm greedy algorithm makes the best choice at each step (locally optimal solution) according to a specified criterion with the hope of finding a global optimum make 67 cents change with quaters(25 cents), dimes(10 cents), nickels(5 cents), and pennies(1 cent), and using the least total number of coins 2 quaters => 50 cents 1 dime => 10 cents 1 nickel => 5 cents 2 cents => 2 cents

greedy algorithm procedure change (a1, a2,..., ar: different types of coins, a1 > a2 >... > ak n: positive integer) for i := 1 to k di := 0 { di counts the coins ai used } while n ai di := di + 1 { add a coin ai} n := n - ai {di is the number of the coin ai for i = 1, 2,... k}

greedy algorithm make 40 cents change with quaters(25 cents), dimes(10 cents), and pennies(1 cent), and using the least total number of coins 1 quaters => 25 cents 1 dime => 10 cents 1 cents => 5 cents

greedy algorithm make 40 cents change with quaters(25 cents), dimes(10 cents), and pennies(1 cent), and using the least total number of coins 1 quaters => 25 cents 1 dime => 10 cents 1 cents => 5 cents 4 dime => 40 cents

modeling with recurrence relations Minimum number of changes Input: n denominations of coins V1 (=1) < V2 < V3 <... < Vk problem: use the smallest number of coins to make the amount of money C M(j): minimum number of coins required to make the amount of money j M(j) = min {M(j - vi)} + 1 vi <= j

modeling with recurrence relations Input: n denominations of coins V1 (=1) < V2 < V3 <... < Vk change(p) if (p = 0) then return 0 else return 1 + min{ change(p-v1), change(p-v2),..., change(p-vk)}

modeling with recurrence relations Input: n denominations of coins V1 (=1) < V2 < V3 <... < Vk change_dp(n) C[0] = 0 for p = 2 to n min = inf for i = 1 to k if (p >= vi && C[p - vi] + 1 < min) min = C[p - vi] + 1 C[p] = min

modeling with recurrence relations Scheduling talks with the maximum attendees when we have n talks, we want to maximize the total number of student attendees. T(j): the maximum number of total attendees for an optimal schedule from the first j talks P(j): talk ending latest among talks compatible with talk j that end before talk j the largest integer i, i< j for which ei <= sj wj: the number of students for talk j T(j) = max(wj + T(p(j)), T(j-1))

modeling with recurrence relations procedure Maximum Attendees(s1, s2,... sn: start times of talks; e1, e2,...en: end times of talks; w1, w2,..., wn: number of attendees to talks) sort talks by end time and relabel so that e1 e2... en for j := 1 to n if no job i with i < j is compatible with job j p(j) = 0 else p(j) := max{ i i < j and job i is compatible with job j} T(0) := 0 for j := 1 to n T(j) := max(wj + T(p(j)), T(j-1)) return T(n) {T(n) is the maximum number of attendees}

linear homogeneous recurrence relation( ) linear homogeneous recurrence relation of degree k with constant coefficients an = c1an-1 + c2an-2 +... +ckan-k, c1, c2,... ck are real numbers and ck 0 Pn = (1.11)Pn-1 : fn = fn-1 + fn-2 : an = an-5 : an = an-1 + an-2 2 : Hn = 2Hn-1 + 1: Bn = nbn-1: linear homogeneous recurrence relation of degree one linear homogeneous recurrence relation of degree two linear homogeneous recurrence relation of degree five not linear not homogeneous does not have constant coefficients

linear homogeneous recurrence relation Let an = c1an-1 + c2an-2 +... +ckan-k be a linear homogeneous recurrence relation and bn = c1bn-1 + c2bn-2 +... +ckbn-k be a linear homogeneous recurrence relation pn = an + bn = (c1an-1 + c2an-2 +... +ckan-k ) + (c1bn-1 + c2bn-2 +... +ckbn-k) = c1(an-1 + bn-1) + c1(an-2 + bn-2) +.. c1(an-k + bn-k) = c1pn-1 + c2pn-2 +... +ckpn-k thus, pn is a solution of the recurrence qn = α an = c1(α an-1) + c2 (α an-2) +... +ck (α an-k) = c1qn-1 + c2qn-2 +... +ckqn-k thus, qn is a solution of the recurrence if we find some solutions to a linear homogeneous recurrence, then any linear combination of them will be a solution to the linear homogeneous recurrence.

linear homogeneous recurrence relation solving linear homogeneous recurrence relation is to find solutions of the form an = r n for the recurrence relation an = c1an-1 + c2an-2 +... +ckan-k r n = c1r n-1 + c2r n-2 +... +ckr n-k ( = r k - c1r k-1 + c2r k-2 +... +ck-1r - ck = 0 : characteristic equation) the solutions of this equation are called the characteristic roots of the recurrence relation

linear homogeneous recurrence relation Theorem consider an = c1an-1 + c2an-2. if characteristic equation r 2 - c1r - c2 = 0 has two distinct roots r1 and r2, the solution to the recurrence relation is an = α1r1 n + α2r2 n if r1 and r2 are roots of r 2 - c1r - c2 = 0, r1 2 = c1r1 + c2, r2 2 = c1r2 + c2 c1an-1 + c2an-2 = c1(α1r1 n-1 + α2r2 n-1 ) + c2(α1r1 n-2 + α2r2 n-2 ) = α1r1 n-2 (c1r1 + c2) + α2r2 n-2 (c1r2 + c2) = α1r1 n-2 r1 2 + α2r2 n-2 r2 2 = α1r1 n + α2r2 n = an

linear homogeneous recurrence relation an = an-1 + 2an-2 with a0 = 2 and a1 = 7? r 2 -r -2 = 0 (r - 2)(r + 1) = 0 r = 2, r = -1 an = α12 n + α2(-1) n an = α1r1 n + α2r2 n a0 = α1 + α2 = 2 a1 = α1 2 + α2 (-1) = 7 α1 = 3, α2 = -1, an = 3 2 n - (-1) n

linear homogeneous recurrence relation an = an-1 + an-2 with a0 = 0 and a1 = 1 (Fibonacci numbers)? r 2 -r -1 = 0 (1 ± (1+4))/2 an = α1 ((1 + 5)/2) n + α2((1-5)/2) n an = α1r1 n + α2r2 n a0 = α1 + α2 = 0 a1 = α1 ((1 + 5)/2) + α2((1-5)/2) = 1 α1 = 1/ 5, α2 = -1/ 5, an = 1/ 5((1 + 5)/2) n - 1/ 5((1-5)/2) n

linear homogeneous recurrence relation Theorem consider an = c1an-1 + c2an-2 if characteristic equation r 2 - c1r - c2 = 0 has only one root r0, the solution to the recurrence relation is an = α1r0 n + α2nr0 n an = 6an-1-9an-2 with initial conditions a0 = 1 and a1 = 6? the only root of r 2-6r + 9 = 0 is r=3 an = α13 n + α2n3 n a0 = α1 = 1 a1 = 1 3 + α2 3 = 6 α2 = 1 an = 3 n + n3 n

linear homogeneous recurrence relation consider an = c1an-1 + c2an-2 +... + ckan-k if characteristic equation r k - c1r k-1 -... - ck = 0 has k distinct roots r1, r2,..., rk, the solution to the recurrence relation is an = α1r1 n + α2r2 n +... + αkrk n an = 6an-1-11an-2 + 6an-3 with initial conditions a0 = 2, a1 = 5, and a2 = 15? the roots of r 3-6r 2 + 11r - 6 = (r - 1)(r - 2)(r - 3) are r=1, r=2, and r=3 an = α11 n + α22 n + α33 n a0 = α1 + α2 + α3 = 2 a1 = α1 + α2 2 + α3 3 = 5 a2 = α1 + α2 4 + α3 9 = 15 α1 = 1, α2 = -1, α3 = 2, an = 1 1 n -1 2 n + 2 3 n

divide-and-conquer recurrence relations when a recursive algorithm divides a problem of size n into a subproblems, f(n) = a f(n/b) + g(n) n/b: the size of each subproblem g(n): the number of operation in the conquer step

recursive binary search procedure binary search(i, j, x: integers, 1 i j n) m := (i+j)/2 if x = am then return m else if (x < am and i < m) then return binary search(i, m-1, x) else if (x > am and j > m) then return binary search(m+1, j, x) else return 0 {i is the start output is the location of x in a1, a2,... an if it appears; otherwise it is 0} f(n) = f(n/2) + 1

recursive binary search f(n) = f(n/2) + 1 = (f(n/4) + 1) + 1 = f(n/2 2 ) + 2 = (f(n/8) + 1) + 1 = f(n/2 3 ) + 3 = : = f(n/2 k ) + k = 1 + log n when n = 2 k, k = log n, f(n) = log n

merge sort 27 13 26 1 15 2 24 38 1 11 2 27 13 26 1 15 2 24 38 6 divide 27 13 26 1 15 2 24 38 3 4 7 8 27 13 26 1 15 2 24 38 5 9 13 27 1 26 2 15 24 38 10 conquer 1 13 26 27 2 15 24 38 21 1 2 13 15 24 26 27 38

merge sort procedure mergesort(l=a1,... an) if n > 1 then m := n/2 L1 := a1, a2,..., am L2 := am+1, am+2,...,an L := merge(mergesort(l1), mergesort(l2)) {L is now sorted into elements in nondecreasing order} procedure merge(l1, L2) L := empty list while L1 and L2 are nonempty remove smaller of first elements of L1 and L2 from its list put it at the right end of L if this removal makes one list empty then remove all elements from the other list and append them to L return L

merge sort f(1) = 1 f(n) = 2f(N/2) + N f(n)/n = f(n/2) / (N/2) + 1 f(n/2) / (N/2) = f(n/4) / (N/4) + 1 f(n/4) / (N/4) = f(n/8) / (N/8) + 1 : f(2) / (2) = f(1) / (1) + 1 f(n)/n = f(1)/1 + log N f(n) = N log N + N = O(N log N)