Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.

Slides:



Advertisements
Similar presentations
Discrete Mathematics Lecture 3
Advertisements

CS1010 Programming Methodology
1 Section 2.4 The Integers and Division. 2 Number Theory Branch of mathematics that includes (among other things): –divisibility –greatest common divisor.
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.
Basic properties of the integers
CSC2110 Discrete Mathematics Tutorial 5 GCD and Modular Arithmetic
Discrete Mathematics Lecture 4 Harper Langston New York University.
6/20/2015 5:05 AMNumerical Algorithms1 x x1x
Discrete Structures Chapter 2 Part B Mathematical Induction
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Properties of the Integers: Mathematical Induction
Fall 2002CMSC Discrete Structures1 Let us get into… Number Theory.
The Integers and Division
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
9/2/2015Discrete Structures1 Let us get into… Number Theory.
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
COMP 170 L2 Page 1 L05: Inverses and GCDs l Objective: n When does have an inverse? n How to compute the inverse? n Need: Greatest common dividers (GCDs)
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
CompSci 102 Discrete Math for Computer Science
Copyright © Zeph Grunschlag, Basic Number Theory Zeph Grunschlag.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
Foundations of Discrete Mathematics Chapter 4 By Dr. Dalia M. Gil, Ph.D.
UNIT-I INTRODUCTION ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 1:
MA/CSSE 473 Day 08 Extended Euclid's Algorithm Modular Division Fermat's little theorem.
Tuesday’s lecture: Today’s lecture: One-way permutations (OWPs)
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Greatest Common Divisors & Least Common Multiples  Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and 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.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 1 Integers Department of Computer Science and Information Engineering,
Fall 2002CMSC Discrete Structures1 Chapter 3 Sequences Mathematical Induction Recursion Recursion.
Direct Proof and Counterexample III Part 2 Lecture 16 Section 3.3 Tue, Feb 13, 2007.
Number Theory. Introduction to Number Theory Number theory is about integers and their properties. We will start with the basic principles of divisibility,
Recursive Algorithms Section 5.4.
CS1010 Programming Methodology
Advanced Algorithms Analysis and Design
CMSC Discrete Structures
Applied Discrete Mathematics Week 3: Algorithms
Applied Discrete Mathematics Week 4: Number Theory
Mathematical Induction Recursion
Copyright © Cengage Learning. All rights reserved.
Foundations of Discrete Mathematics
Axiomatic semantics Points to discuss: The assignment statement
Applied Discrete Mathematics Week 9: Integer Properties
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston
Copyright © Zeph Grunschlag,
Application: Algorithms
Application: Algorithms
Divisibility and Modular Arithmetic
Applied Discrete Mathematics Week 10: Introduction to Counting
Introduction to Algorithms
Number Theory.
From the last time: gcd(a, b) can be characterized in two different ways: It is the least positive value of ax + by where x and y range over integers.
Recursion.
Presentation transcript:

Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007

Algorithms An algorithm is a step-by-step procedure that is guaranteed to stop after a finite number of steps for all legitimate inputs.

Example: Max Algorithm Describe an algorithm for finding the maximum value in a list of numbers. Special cases: What if the list is empty? Others?

An Algorithmic Language An algorithmic language is much like a computer language. Its primary purpose is to express unambiguously the steps of an algorithm, including all decisions and special cases that may arise.

Assignment Statements An assignment statement is of the form where x is a variable and expr is an expression. Examples found := true count := count + 1 x := expr

Conditional Statements One-way conditional statements: Two-way conditional statements: if condition then sequence of statements if condition then sequence of statements else sequence of statements

Iterative Statements While loops: For loops: while condition sequence of statements end while for var := init expr to final expr sequence of statements next var

For Loops A for loop can always be rewritten as a while loop. var := init expr while var  final expr sequence of statements var := var + 1 end while

A Notation for Algorithms An algorithm will be organized into three parts: Input – List all input variables and any special assumptions about them. Algorithm body – Describe the step-by-step procedure. Output – List the output variables.

Example: Finding the Max Input: a, n a is a list of numbers. n is the size of the list, n  1. Algorithm body: Output: max max := a[1] for i := 2 to n if a[i] > max then max = a[i]

Tracing an Algorithm Write a trace table for the Max algorithm and the input a = {7, 3, 8, 6, 4}, n = 5. Iterationanmaxi 0{7, 3, 8, 6, 4}

Tracing an Algorithm Write a trace table for the Max algorithm and the input a = {7, 3, 8, 6, 4}, n = 5. Iterationanmaxi 0{7, 3, 8, 6, 4}

Tracing an Algorithm Write a trace table for the Max algorithm and the input a = {7, 3, 8, 6, 4}, n = 5. Iterationanmaxi 0{7, 3, 8, 6, 4}

Tracing an Algorithm Write a trace table for the Max algorithm and the input a = {7, 3, 8, 6, 4}, n = 5. Iterationanmaxi 0{7, 3, 8, 6, 4}

Tracing an Algorithm Write a trace table for the Max algorithm and the input a = {7, 3, 8, 6, 4}, n = 5. Iterationanmaxi 0{7, 3, 8, 6, 4}

Greatest Common Divisors Let a and b be integers that are not both 0. The greatest common divisor of a and b, denoted gcd(a, b), is the unique positive integer d with the following properties: d | a and d | b. For every integer c, if c | a and c | b, then c | d.

Least Common Multiples Let a and b be nonzero integers. The least common multiple of a and b, denoted lcm(a, b), is the unique positive integer m with the following properties: a | m and b | m. For every integer c, if a | c and b | c, then m | c.

The High-school gcd Algorithm The high-school method is very inefficient. Factor each number into standard form. For each prime that appears in both factorizations, use it as a factor of the gcd along with the smaller of the two exponents.

Example Find the gcd of and We factor them as = 2 2  3 2  5 2  7 1  13 1  = 2 1  3 3  5 1  7 1  13 0  29 1 Therefore, the gcd is 2  3 2  5  7 = 630

The Euclidean Algorithm Factoring is inefficient; therefore, this algorithm is inefficient. The run time of this algorithm is O(  10 d ), where d is the number of digits in the number. Euclid had a much better idea. The run time of the Euclidean Algorithm is O(d), where d is the number of digits in the number.

The Euclidean Algorithm Input: A, B (positive integers, not both 0)

The Euclidean Algorithm Algorithm body: Output: b a := A b := B if b = 0 then swap a and b r := a mod b while r > 0 a := b b := r r := a mod b end while

Example Apply the Euclidean Algorithm to A = and B = IterationABabr

Example Apply the Euclidean Algorithm to A = and B = IterationABabr

Example Apply the Euclidean Algorithm to A = and B = IterationABabr

Least Common Multiples What is the efficient way to find lcm’s? What is the lcm of and 54810?

Proof of the Euclidean Algorithm Theorem: The Euclidean Algorithm terminates for all legitimate inputs A and B. Proof: We may assume that B > 0. After the first iteration of the while loop, 0  b < B since b is the remainder of A divided by B.

Proof of the Euclidean Algorithm Each iteration produces a nonnegative remainder that is smaller than the previous remainder. This cannot happen more than B times before the remainder is 0.

Proof of the Euclidean Algorithm Lemma 1: If b > 0, then gcd(b, 0) = b. Proof: b | b and b | 0. For all integers c, if c | 0 and c | b, then c | b. Therefore, b = gcd(b, 0).

Proof of the Euclidean Algorithm Lemma 2: If a and b are integers, with b  0, and q and r are integers such that a = qb + r then gcd(a, b) = gcd(b, r). Proof: Let d = gcd(b, r). Then d | b and d | r and any integer that divides b and r must also divide d.

Proof of the Euclidean Algorithm We must show that d | a and d | b and any integer that divides a and b must also divide d. We already know that d | b. Since a = qb + r, it follows that d | a. Let c be an integer such that c | a and c | b. Since r = a – qb, it follows that c | r and so c | d. Therefore, d = gcd(a, b).

Proof of the Euclidean Algorithm Theorem: The Euclidean Algorithm produces the gcd of A and B. Proof: After the final iteration of the while loop, r = 0. By Lemma 1, the output b is the gcd of b and r. By Lemma 2, that is equal to the gcd of “a” and “b” in the final iteration.

Proof of the Euclidean Algorithm But “a” and “b” on the last iteration were “b” and “r” on the previous iteration. Therefore, gcd(a, b) on the last iteration equals gcd(b, r) on the previous iteration, which equals gcd(a, b) on the previous iteration, and so on. Following this argument all the back to the first iteration, we see that the output is gcd(A, B).

Proof of the Euclidean Algorithm In the next chapter, we will study mathematical induction. At that point, we will be able to make this argument more rigorous.