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

Similar documents
Induction and recursion. Chapter 5

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

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

Induction and Recursion

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

Induction and recursion. Topics. Induction and Recursion Vojislav Kecman CMSC 302

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

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

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

Fall 2017 Test II review problems

1. (16 points) Circle T if the corresponding statement is True or F if it is False.

1 Recursive Algorithms

Fall 2015 Lecture 14: Modular congruences. cse 311: foundations of computing

Math 230 Final Exam, Spring 2008

Divide and Conquer Algorithms

MAT 243 Test 2 SOLUTIONS, FORM A

Divide and Conquer Algorithms

MATH 215 Final. M4. For all a, b in Z, a b = b a.

ICS141: Discrete Mathematics for Computer Science I

Lecture 5: Loop Invariants and Insertion-sort

Mathematical Fundamentals

Algorithm efficiency analysis

8. Sequences, Series, and Probability 8.1. SEQUENCES AND SERIES

Number Theory Basics Z = {..., 2, 1, 0, 1, 2,...} For, b Z, we say that divides b if z = b for some. Notation: b Fact: for all, b, c Z:

Recurrences COMP 215

MAT 243 Test 2 SOLUTIONS, FORM A

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

Complete Induction and the Well- Ordering Principle

CSCI Honor seminar in algorithms Homework 2 Solution

201-1A5-MT - Mathematics Summer 2015 HOMEWORK 2 Deadline : Sunday, August 30, 2015 at 12 :30.

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Recursion. Slides by Christopher M. Bourke Instructor: Berthe Y. Choueiry. Fall 2007

Administrivia. COMP9020 Lecture 7 Session 2, 2017 Induction and Recursion. Lecture 6 recap. Lecture 6 recap

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

We want to show P (n) is true for all integers

Divide and Conquer. Recurrence Relations

Basic Proof Examples

Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices. Integers & Algorithms (2.5)

Discrete Mathematics U. Waterloo ECE 103, Spring 2010 Ashwin Nayak May 17, 2010 Recursion

Chapter 5.1: Induction

Lecture 4. Quicksort

INDUCTION AND RECURSION. Lecture 7 - Ch. 4

ICS141: Discrete Mathematics for Computer Science I

Advanced Counting Techniques. Chapter 8

Arithmetic Algorithms, Part 1

IS 709/809: Computational Methods for IS Research. Math Review: Algorithm Analysis

Lecture Notes 1 Basic Concepts of Mathematics MATH 352

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures

Why do we need math in a data structures course?

Mathematical Induction

2301 Assignment 1 Due Friday 19th March, 2 pm

1 Examples of Weak Induction

Climbing an Infinite Ladder

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

Partition of Integers into Distinct Summands with Upper Bounds. Partition of Integers into Even Summands. An Example

Cpt S 223. School of EECS, WSU

PUTNAM PROBLEMS SEQUENCES, SERIES AND RECURRENCES. Notes

With Question/Answer Animations

Fast Polynomial Multiplication

Recursion and Induction

CHAPTER 8 Advanced Counting Techniques

Computational Complexity - Pseudocode and Recursions

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

Divide and Conquer Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 14

Section 4.1: Sequences and Series

Problem Set 1 Solutions

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 3

The Euclidean Algorithm and Multiplicative Inverses

Worst-case analysis of Weber s GCD algorithm

Writing Assignment 2 Student Sample Questions

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

MAS114: Exercises. October 26, 2018

0.Axioms for the Integers 1

Lecture 12 : Recurrences DRAFT

Practice Exam 1 CIS/CSE 607, Spring 2009

Name CMSC203 Fall2008 Exam 2 Solution Key Show All Work!!! Page (16 points) Circle T if the corresponding statement is True or F if it is False.

CSE 613: Parallel Programming. Lecture 8 ( Analyzing Divide-and-Conquer Algorithms )

Computer Science B Homework #2 Due Monday February 9, 2004, 13:30

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

Algorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida

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

Problem Set 5 Solutions

Basic Algorithms in Number Theory

Discrete Mathematics. Spring 2017

Recursive Definitions

Advanced Counting Techniques

Algoritmi di Bioinformatica. Computational efficiency I

0 Sets and Induction. Sets

Number theory (Chapter 4)

5: The Integers (An introduction to Number Theory)

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

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

CHAPTER 8: EXPLORING R

Chapter 5: The Integers

Divide and Conquer. Slides by Carl Kingsford. Feb. 17, Based on AD Sections

CSI Mathematical Induction. Many statements assert that a property of the form P(n) is true for all integers n.

UNIVERSITY OF VICTORIA DECEMBER EXAMINATIONS MATH 122: Logic and Foundations

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Divide-Conquer-Glue Algorithms

Transcription:

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

recursive algorithms a n = a a a a a a a = a a n-1 power (a, n) = a power (a, n-1) basis step: if n = 0, power(a, 0) = 1, which is correct since a 0 = 1 inductive step: inductive hypothesis: power(a, k) = a k for all a 0 power(a, k+1) = a power(a, k) = a a k = a k+1

recursive algorithms power (a, n) = a power (a, n-1) procedure power (a: nonzero real number, n: nonnegative integer) if n = 0 then return 1 else return a power(a, n-1) {output is a n }

recursive algorithms an algorithm is recursive if it solves a problem by reducing it to an instance of the same problem with smaller input 0! = 1 n! = n (n-1)! n is positive integer 4! = 4 3! = 4 3 2! = 4 3 2 1! = 4 3 2 1 0! = 4 3 2 1 procedure factorial (n: nonnegtive integer) if n = 0 then return 1 else return n factorial(n-1) {output is n!}

recursive algorithms procedure factorial (n: nonnegtive integer) if n = 0 then return 1 else return n factorial(n-1) {output is n!}

recursive modular exponentiation b n mod m, where b, n, and m are integers with m 2, n 0, and 1 b < m b 0 mod m = 1 b n mod m = ( b b n-1 ) mod m = (b (b n-1 mod m)) mod m, n>0 procedure mpower (b, n, m) if n = 0 then return 1 else return b mpower(b, n-1, m)mod m

recursive modular exponentiation b n mod m, where b, n, and m are integers with m 2, n 0, and 1 b < m b n mod m = (b n/2 mod m) 2 mod m, n is even = ((b n/2 mod m) 2 mod m) (b mod m) mod m n is odd procedure mpower (b, n, m) if n = 0 then return 1 else if n is even then return mpower(b, n/2, m) 2 mod m else return ((mpower(b, n/2, m) 2 mod m) (b mod m) ) mod m

Fibonacci number 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

Fibonacci number 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... procedure iterative fibonacci(n: nonnegative integer) if n=0 then return 0 else x := 0 y := 1 for i :=1 to n-1 z := x + y x := y y := z return y { output is the nth Fibonacci number}

Fibonacci number fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) procedure fibonacci(n: nonnegative integer) if n=0 then return 0 else if n=1 then return 1 else return fibonacci(n-1) + fibonacci(n-2) {output is fibonacci(n)}

Fibonacci number fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) procedure fibonacci(n: nonnegative integer) if n=0 then return 0 else if n=1 then return 1 else return fibonacci(n-1) + fibonacci(n-2) {output is fibonacci(n)}

Fibonacci number procedure fastfibonacci(n: nonnegative integer) if n=0 return 0 return findfib(0,1,1,n) procedure findfib(a, b, m, n) if m=n return b return findfib(b, a+b, m+1, n) findfib(0, 1, 1, 4) return findfib(1,1, 2, 4) return findfib(1,2, 3, 4) return findfib(2, 3, 4, 4) return 3 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

recursive Euclidean algorithm a = bq + r, a, b, q, r are integer gcd (a, b) = gcd (b, r), a > b gcd(8, 5)? 8 = 5 1 + 3 5 = 3 1 + 2 3 = 2 1 + 1 2 = 1 2 gcd(8, 5) = gcd(5, 3) = gcd(3,2) =1 procedure gcd (a, b: positive integer, a > b) if b = 0 then return a else return gcd (b, a mod b)

linear search procedure linear search(x: integer, a1, a2,..., an: integers) i := 1 while (i n and x ai) i := i + 1 if i n then location := i else location := 0 return location {location is the subscript of the term that equals x, or 0 if x is not found}

recursive linear search search (i, j, x): searches for the first occurrence of x in the sequence ai, ai+1,... aj. procudure search(i, j, x: integers, 1 i j n) if ai = x then return i else if i = j then return 0 else return search(i+1, j, x) {output is the location of x in a1, a2,... an if it appears; otherwise it is 0}

binary search procedure binary search(x: integer, a1, a2,..., an: integers in increasing order) i := 1 {i is left endpoint of search interval} j := n {j is right endpoint of search interval} while (i < j) m := (i + j)/2 if x > am then i := m + 1 else j := m if x = ai then location := i else location := 0 return location {location is the subscript i of the term ai equal to x, or 0 if x is not found}

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}

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 27 13 26 1 15 2 24 38 27 13 26 1 15 2 24 38 divide conquer 1 13 26 27 2 15 24 38 1 2 13 15 24 26 27 38

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