Divide & Conquer Themes –Reasoning about code (correctness and cost) –iterative code, loop invariants, and sums –recursion, induction, and recurrence relations.

Slides:



Advertisements
Similar presentations
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Advertisements

Euclidean Algorithm Applied Symbolic Computation CS 567 Jeremy Johnson.
A simple example finding the maximum of a set S of n numbers.
Analysis of Algorithms
Chapter Primes and Greatest Common Divisors ‒Primes ‒Greatest common divisors and least common multiples 1.
February 19, 2015Applied Discrete Mathematics Week 4: Number Theory 1 The Growth of Functions Question: If f(x) is O(x 2 ), is it also O(x 3 )? Yes. x.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Part 5. Computational Complexity (3)
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Discrete Structures Chapter 2 Part B Mathematical Induction
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Fundamental in Computer Science Recursive algorithms 1.
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
Asymptotic Growth Rates Themes –Analyzing the cost of programs –Ignoring constants and Big-Oh –Recurrence Relations & Sums –Divide and Conquer Examples.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
1 Properties of Integers Objectives At the end of this unit, students should be able to: State the division algorithm Apply the division algorithm Find.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
Mathematics Review and Asymptotic Notation
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Mathematical Background and Linked Lists. 2 Iterative Algorithm for Sum Find the sum of the first n integers stored in an array v : sum (v[], n) temp_sum.
1 Section 2.1 Functions: Definitions and Examples A function ƒ from A to B associates each element of A with exactly one element of B. Write ƒ : A  B.
Cpt S 223 – Advanced Data Structures Math Review 2
Asymptotic Growth Rates  Themes  Analyzing the cost of programs  Ignoring constants and Big-Oh  Recurrence Relations & Sums  Divide and Conquer 
7.3 Divide-and-Conquer Algorithms and Recurrence Relations If f(n) represents the number of operations required to solve the problem of size n, it follow.
ADVANCED ALGORITHMS REVIEW OF ANALYSIS TECHNIQUES (UNIT-1)
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Ch04-Number Theory and Cryptography 1. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic.
Number Theory Lecture 1 Text book: Discrete Mathematics and its Applications, 7 th Edition.
Agenda Review:  Relation Properties Lecture Content:  Divisor and Prime Number  Binary, Octal, Hexadecimal Review & Exercise.
Number Theory. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic principles of divisibility,
Lecture 2 Algorithm Analysis
Introduction to Algorithms: Divide-n-Conquer Algorithms
Recursive Algorithms Section 5.4.
Growth of Functions & Algorithms
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
Sorting by Tammy Bailey
Chapter 2: Getting Started
Applied Symbolic Computation (CS 300) Modular Arithmetic
CS 3343: Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Algorithmic Number Theory and Cryptography (CS 303) Modular Arithmetic
Divide and Conquer Algorithms Part I
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 30 CSE 331 Nov 12, 2012.
David Kauchak cs161 Summer 2009
Presentation transcript:

Divide & Conquer Themes –Reasoning about code (correctness and cost) –iterative code, loop invariants, and sums –recursion, induction, and recurrence relations –Divide and Conquer Examples –sorting (insertion sort & merge sort) –computing powers –Euclidean algorithm (computing gcds)

Arithmetic Series = = b + 2b + 3b + … + (n-1)b = bn(n-1)/2

Arithmetic Series (Proof)

Geometric Series = 1 + x + x 2 + … + x n-1 =

Geometric Series (Proof)

Floor and Ceiling Let x be a real number The floor of x,  x , is the largest integer less than or equal to x –If an integer k satisfies k  x < k+1, k =  x  –E.G.  3.7  = 3,  3  = 3 The ceiling of x,  x , is the smallest integer greater than or equal to x –If an integer k satisfies k-1 < x  k, k =  x  –E.G.  3.7  = 4,  3  = 3

logarithm y = log b (x)  b y = x Two important cases –ln(x) = log e (x) –lg(x) = log 2 (x) [frequently occurs in CS] Properties –log(cd) = log(c) + log(d) –log(c x ) = xlog(c) –log b (b x ) = x = b logb(x) –d ln(x)/dx = 1/x

logarithm 2 k  x < 2 k+1  k =  lg(x)  –E.G. 16  25 < 32  4  lg(25) < 5 –lg(25)  4.64 Change of base –log c (x) = log b (x) / log b (c) Proof. y = log c (x)  c y = x  ylog b (c) = log b (x)  y = log b (x) / log b (c)

Insertion Sort To sort x 0,…,x n-1, –recursively sort x 1,…,x n –insert x 0 into x 1,…,x n-1 –(see code for details) Loop invariant –x 0,…, x i-1,t and x i+1,…, x n-1 sorted –initialize t = x 0

Insertion Sort (Example) (7,6,5,4,3,2,1,0) after recursive call (7,0,1,2,3,4,5,6) Number of comparisons to sort inputs that are in reverse sorted order (worst case) C(n) = C(n-1) + (n-1) C(1) = 0

Merge Sort To sort x 0,…,x n-1, –recursively sort x 0,…,x a-1 and x a,…,x n-1, where a = n/2 –merge two sorted lists Insertion sort is a special case where a=1 –loop invariant for merge similar to insert (depends on implementation)

Merge Sort (Example) (7,6,5,4,3,2,1,0) after recursive calls (4,5,6,7) and (0,1,2,3) Number of comparisons to sort inputs that are in reverse sorted order (worst case) M(n) = 2M(n/2) + n/2 M(1) = 0 Is this the worst case?

Comparison of Insertion and Merge Sort Count the number of comparisons for different n=2 k (see and run sort.cpp) M(n)/C(n)  as n increases C(2n)/C(n)  4 M(2n)/M(n)  2 as n increases nM(n)C(n)M(n)/C(n)

Solve Recurrence for C(n)

Solve Recurrence for M(n)

Computing Powers Recursive definition –a n = a  a n-1, n > 0 –a 0 = 1 Number of multiplications –M(n) = M(n-1) + 1, M(0) = 0 –M(n) = n

Binary Powering (Recursive) Binary powering –x 16 = (((x 2 ) 2 ) 2 ) 2, 16 = (10000) 2 –x 23 = (((x 2 ) 2 x) 2 x) 2 x, 23 = (10111) 2 Recursive (right-left) x n = (x  n/2  ) 2  x (n % 2) M(n) = M(  n/2  ) + [n % 2]

Binary Powering (Iterative) Loop invariant – x n = y  z N N = n; y = 1; z = x; while (N != 0) { if (N % 2 == 1) y = z*y; z = z*z; N = N/2; } Example N y z 23 1 x 11 x x 2 5 x 3 x 4 2 x 7 x 8 1 x 7 x 16 0 x 23 x 32

Binary Powering Number of multiplications Let (n) = number of 1bits in binary representation of n M(n) =  lg(n)  + (n)

Greatest Common Divisors g = gcd(a,b)  g|a and g|b if e|a and e|b  e|g gcd(a,0) = 0 gcd(a,b) = gcd(b,a%b) –since if g|a and g|b then g|a%b and if g|b and g|a%b then g|a

Euclidean Algorithm (Iterative) a 0 = a, a 1 = b a i = q i * a i+1 + a i+2, 0  a i+2 < a i+1 … a n = q n * a n+1 g = a n+1

Number of Divisions a i = q i * a i+1 + a i+2, 0  a i+2 < a i+1 a i  (q i + 1)*a i+2  2a i+2 a n  2a n+1 a 1 / a 3  a 2 / a 4  …  a n-1 / a n-2  a n / a n+1  2 n  n  lg(a 1 a 2 /g 2 )  if a,b  N, # of divisions  2lg(N)