Slides - faculty.ucmerced.edu

CSE 115 / ENGR 160
Spring 2015
Lecture 23
Announcements and Assignments
•
•
•
•
Check course website for reading schedule
HW #10 due today in class
Quiz #3 today in latter part of lecture
HW #11 assigned today, due Wed. May 6 in class
Today
• 5.4 Recursive Algorithms (continued)
• 6.1 The Basics of Counting
Recursive GCD Algorithm
Example: Give a recursive algorithm for computing the greatest common divisor of two nonnegative integers a and b with a < b.
Solution: Use the reduction
gcd(a,b) = gcd(b mod a, a) and the condition gcd( ,b) = b when b > .
procedure gcd(a,b: nonnegative integers with a < b)
if a = else gcd (b mod a, a)
{output is gcd(a, b)}
79
Recursive Modular Exponentiation Algorithm
Example: Devise a recursive algorithm for computing
bn mod m, where b, n, and m are integers with m
n
and
b m.
Solution: (see text for full explanation)
procedure mpower(b,n,m: integers with b > 0 and m 2,n 0
if n = 0 then
return1
else if n is even then
returnmpower(b,n/2,m 2 mod m
else
return(mpower(b, n/2 ,m 2 mod m∙b mod m) mod m
{output is bn mod m}
80
Recursive Binary Search Algorithm
Example: Construct a recursive version of a binary search algorithm. Solution: Assume we have a1,a2,…, an, an increasing sequence of integers. Initially i is and j is n. We are searching for x.
procedure binary search(i, j, x : integers, 1 i j n
m : i j /2
if x = am then
returnm
else if (x < am and i < m) then
returnbinary search(i,m 1,x
else if (x > am and j >m) then
returnbinary search(m 1,j,x
else return0
{output is location of x in a1,a2,…,an if it appears, otherwise 0}
81
Proving Recursive Algorithms Correct
 Both mathematical and str0ng induction are useful techniques to show that recursive algorithms always produce the correct output.
Example: Prove that the algorithm for computing the powers of real numbers is correct.
procedure power(a:nonzero real number, n: nonnegative integer)
if n = 0 then return1
else returna∙power (a, n 1)
{output is an}
82
Proving Recursive Algorithms Correct
2
Solution: Use mathematical induction on the exponent n.
BASIS STEP: a0 = for every nonzero real number a, and power(a, ) = .
INDUCTIVE STEP: The inductive hypothesis is that . Assuming the inductive power(a,k) = ak, for all a
hypothesis, the algorithm correctly computes ak+1, since
power(a,k + ) = a power (a, k) = a ak = ak+1 .
83
.
procedure power(a:nonzero real number, n: nonnegative integer)
if n = 0 then return1
else returna∙power (a, n 1)
{output is an}
Merge Sort
 Merge Sort works by iteratively splitting a list (with an even number of elements) into two sublists of equal length until each sublist has one element.
 Each sublist is represented by a balanced binary tree.
 At each step a pair of sublists is successively merged into a list with the elements in increasing order. The process ends when all the sublists have been merged.
 The succession of merged lists is represented by a binary tree.
84
Merge Sort
Example: Use merge sort to put the list
into increasing order.
Solution:
85
Recursive Merge Sort
Example: Construct a recursive merge sort algorithm. Solution: Begin with the list of n elements L.
procedure mergesort(L = a1 a2 an
if n > m
L1 := a1 a2 am
L2 := am 1 am 2 an
L := merge(mergesort(L1), mergesort(L2 ))
{L is now sorted in increasing order}
continued →
86
Recursive Merge Sort
 Subroutine merge, which merges two sorted lists.
procedure merge(L1, L2 :sorted lists
L := empty list
while L1 and L2 are both nonempty
remove smaller of first elements of L1 and L2 from its list; put 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 {L is the merged list in increasing order}
Complexity of Merge: Two sorted lists with m elements and n
elements can be merged into a sorted list using no more than m + n
comparisons.
87
Merging Two Lists
Example: Merge the two lists Solution:
and .
88
Complexity of Merge Sort
Complexity of Merge Sort: The number of comparisons needed to merge a list with n elements is O(n log n).
 For simplicity, assume that n is a power of , say m.
 At the end of the splitting process, we have a binary tree with m levels, and m lists with one element at level m.
 The merging process begins at level m with the pairs of m lists with one element combined into m 1 lists of two elements. Each merger takes one comparison.
 The procedure continues, at each level (k = m, m
k lists with m k elements are merged into m
k 1 lists, with m k 1 elements at level k
.
 We know (by the complexity of the merge subroutine) that each merger takes at most 2m
comparisons.
k
+ 2m
k
1 2m
k 1
1
continued →
89
Complexity of Merge Sort
 Summing over the number of comparisons at each level, shows that because m = log n and n = m.
(The expression in the formula above is evaluated as m
using the formula for the sum of the terms of a geometric progression, from Section .)
 Chapter that the fastest comparison‐based sorting algorithms have O(n log n) time complexity. So, merge sort achieves the best possible big‐O estimate of time complexity.
90
Chapter 6
With Question/Answer Animations
1
Chapter Summary
 The Basics of Counting
 The Pigeonhole Principle
 Permutations and Combinations
2
Section .
3
Section Summary
 The Product Rule
 The Sum Rule
 The Subtraction Rule
 The Division Rule
 Examples, Examples, and Examples
 Tree Diagrams
4
Basic Counting Principles: The Product Rule
The Product Rule: A procedure can be broken down into a sequence of two tasks. There are n1 ways to do the first task and n2 ways to do the second task. Then there are n1∙n2 ways to do the procedure.
Example: How many bit strings of length seven are there?
Solution: Since each of the seven bits is either a or a , the answer is 7 = .
5
The Product Rule
Example: How many different license plates can be made if each plate contains a sequence of three uppercase English letters followed by three digits?
Solution: By the product rule,
6
Counting Functions
Counting Functions: How many functions are there from a set with m elements to a set with n elements?
Solution: Since a function represents a choice of one of the n elements of the codomain for each of the m elements in the domain, the product rule tells us that there are n n
n nm such functions.
Counting One‐to‐One Functions: How many one‐to‐one functions are there from a set with m elements to one with n
elements?
Solution: Suppose the elements in the domain are a1, a2,…, am. There are n ways to choose the value of a1and ways to choose a2, etc. The product rule tells us that n
there are n(n
(n
n m
7
Telephone Numbering Plan
Example: The North American numbering plan specifies that a telephone number consists of 10 digits: a 3‐digit area code, a 3‐digit office code, and a 4‐digit station code. There are some restrictions on the digits.
 Let X denote a digit from 0 through 9.
 Let N denote a digit from 2 through 9.
 Let Y denote a digit that is 0 or 1.
 In the old plan (in use in the 1960s) the format was NYX‐NNX‐XXXX.
 In the new plan, the format is NXX‐NXX‐XXXX.
How many different #s are possible under the old and new plans?
Solution: Use the Product Rule.
 There are 8∙2∙10= 160 area codes with the format NYX.
 There are 8∙10∙10= 800 area codes with the format NXX.  There are 8∙8∙10= 640 office codes with the format NNX.  There are 10∙10∙10∙10= 10,000 station codes with the format XXXX. Number of old plan telephone numbers: 160∙640∙10,000= 1,024,000,000.
Number of new plan telephone numbers: 800∙800∙10,000= 6,400,000,000.
8