ADT Implementation: Recursion, Algorithm Analysis, and Standard

ADT Implementation:
Recursion, Algorithm
Analysis, and Standard
Algorithms
Chapter 10
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
1
Chapter Contents
10.1 Recursion
10.2 Examples of Recursion: Towers of Hanoi;
Parsing
10.3 Implementing Recursion
10.4 Algorithm Efficiency
10.5 Standard Algorithms in C++
10.6 Proving Algorithms Correct (Optional)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
2
Chapter Objectives
• Review recursion by looking at examples
• Show how recursion is implemented using a
run-time stack
• Look at the important topic of algorithm
efficiency and how it is measured
• Describe some of the powerful and useful
standard C++ function templates in STL
• (Optional) Introduce briefly the topic of
algorithm verification
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
3
Recursion
A function is defined recursively if it has the
following two parts
• An anchor or base case
– The function is defined for one or more specific
values of the parameter(s)
• An inductive or recursive case
– The function's value for current parameter(s) is
defined in terms of previously defined function
values and/or parameter(s)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
4
Recursive Example
• Consider a recursive power function
double power (double x, unsigned n)
{ if ( n == 0 )
return 1.0;
else
return x * power (x, n-1); }
• Which is the anchor?
• Which is the inductive or recursive part?
• How does the anchor keep it from going forever?
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
5
Recursive Example
• Note the
results of
a call
– Recursive
calls
– Resolution
of the
calls
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
6
A Bad Use of Recursion
• Fibonacci numbers
1, 1, 2, 3, 5, 8, 13, 21, 34
f1 = 1, f2 = 1 … fn = fn -2 + fn -1
– A recursive function
double Fib (unsigned n)
{ if (n <= 2)
return 1;
else
return Fib (n – 1) + Fib (n – 2);
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
}
7
A Bad Use of Recursion
• Why is this inefficient?
– Note the recursion tree
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
8
Uses of Recursion
• Binary Search
– See source code
– Note results of
recursive call
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
9
Uses of Recursion
• Palindrome checker
– A palindrome has same value with characters reversed
1234321
racecar
• Recursive algorithm for an integer
– If numDigiths <= 1 return true
– Else check first and last digits
num/10numDigits-1 and num % 10
• if they do not match return false
– If they match, check more digits
Apply algorithm recursively to:
num % 10numDigits-1 and numDigits - 2
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
10
Recursion Example:
Towers of Hanoi
• Recursive algorithm especially appropriate for
solution by recursion
• Task
–
–
–
–
Move disks from left peg to right peg
When disk moved, must be placed on a peg
Only one disk (top disk on a peg) moved at a time
Larger disk may never be placed on a smaller disk
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
11
Recursion Example:
Towers of Hanoi
• Identify base case:
If there is one disk move from A to C
• Inductive solution for n > 1 disks
– Move topmost n – 1 disks from A to B, using C
for temporary storage
– Move final disk remaining on A to C
– Move the n – 1 disk from B to C using A for
temporary storage
• View code for solution, Fig 10.4
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
12
Recursion Example:
Towers of Hanoi
• Note the graphical steps to the solution
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
13
Recursion Example: Parsing
• Examples so far are direct recursion
– Function calls itself directly
• Indirect recursion occurs when
– A function calls other functions
– Some chain of function calls eventually results in
a call to original function again
• An example of this is the problem of
processing arithmetic expressions
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
14
Recursion Example: Parsing
• Parser is part of the compiler
• Input to a compiler is
characters
– Broken up into meaningful groups
– Identifiers, reserved words, constants, operators
• These units are called tokens
– Recognized by lexical analyzer
– Syntax rules applied
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
15
Recursion Example: Parsing
• Parser generates a
parse tree using the
tokens according to
rules below:
• An expression:
term + term | term – term | term
• A term:
factor * factor | factor / factor | factor
• A factor:
( expression ) | letter | digit
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
Note the indirect
recursion
16
Implementing Recursion
• Recall section 7.4, activation record created
for function call
• Activation records placed on run-time stack
• Recursive calls generate stack of similar
activation records
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
17
Implementing Recursion
• When base case reached and successive
calls resolved
– Activation records are popped off the stack
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
18
Algorithm Efficiency
• How do we measure efficiency
– Space utilization – amount of memory required
– Time required to accomplish the task
• Time efficiency depends on :
– size of input
– speed of machine
– quality of source code
– quality of compiler
These vary from one
platform to another
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
19
Algorithm Efficiency
• We can count the number of times
instructions are executed
– This gives us a measure of efficiency of an
algorithm
• So we measure computing time as:
T(n)= computing time of an algorithm for input of size n
= number of times the instructions are executed
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
20
Example: Calculating the Mean
Task
1.
2.
3.
4.
5.
6.
# times executed
Initialize the sum to 0
Initialize index i to 0
While i < n do following
a) Add x[i] to sum
b) Increment i by 1
Return mean = sum/n
Total
1
1
n+1
n
n
1
3n + 4
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
21
Computing Time Order of
Magnitude
• As number of inputs increases
 T(n) = 3n + 4 grows at a rate proportional to n
• Thus T(n) has the "order of magnitude" n
• The computing time of an algorithm on input
of size n,
 T(n) said to have order of magnitude f(n),
 written T(n) is O(f(n))
if … there is some constant C such that
 T(n) < Cf(n) for all sufficiently large values of n
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
22
Big Oh Notation
Another way of saying this:
• The complexity of the algorithm is O(f(n)).
•
Example: For the Mean-Calculation
Algorithm:
T(n) is O(n)
•
Note that constants and multiplicative
factors are ignored.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
23
Big Oh Notation
• f(n) is usually simple:
n, n2, n3, ...
2n
1, log2n
n log2n
log2log2n
• Note graph
of common
computing times
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
24
Big Oh Notation
• Graphs of common computing times
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
25
Common Computing Time
Functions
log2n
n
n log2n
n2
n3
2n
0
1
0
1
1
2
0.00
1
2
2
4
8
4
1.00
2
4
8
16
64
16
1.58
3
8
24
64
512
256
2.00
4
16
64
256
4096
65536
2.32
5
32
160
1024
32768
4294967296
2.58
6
64
384
4096
262144
1.84467E+19
3.00
8
256
2048
65536
16777216
1.15792E+77
3.32
10
1024
10240
1048576
1.07E+09 1.8E+308
4.32
20
1048576
20971520
1.1E+12
1.15E+18 6.7E+315652
log2log2n
---
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
26
Computing in Real Time
• Suppose each instruction can be done in 1
microsecond
• For n = 256 inputs how long for various f(n)
Function
Time
log2log2n
3 microseconds
Log2n
8 microseconds
n
.25 milliseconds
n log2n
2 milliseconds
n2
65 milliseconds
n3
17 seconds
2n
3.7+E64 centuries!!
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
27
STL's Algorithms
• STL has a collection of more than 80 generic
algorithms.
– not member functions of STL's container classes
– do not access containers directly.
• They are stand-alone functions
– operate on data by means of iterators .
• Makes it possible to work with regular C-style
arrays as well as containers.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
28
Example of sort Algorithm
• Comes in several different forms
• One uses the < operator to compare elements
– Requires that operator<() be defined on the data
type being sorted
– Example program, Fig 10.7
• The sort algorithm can be used with arrays
– See example, Fig. 10.8
• Sort algorithm can have a third parameter
– The name of a boolean function to be used for a less
than
– See Fig. 10.9
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
29
A Sample of STL Algorithms
• Designed to operate on a sequence of
elements
• Designate a sequence by using two iterators
– One positioned at the first element of the
sequence
– One positioned after the last element in the
sequence
• Note Table 10-4, Page 571 of text
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
30
Algorithms from <numeric> Library
• Contains function templates that operate on
sequential containers
• Intended for use with numeric sequences
– Such as valarrays
• See Table 10-5, page 572
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
31
Using Algorithms
• Consider the judging of figure skaters
– When judges give scores the high and low score
are discarded
– Then the remaining scores are averaged
• Given the vector of scores shown below
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
32
Using Algorithms
• Use min_element algorithms to find and
discard minimum score
scores.erase(min_element(scores.begin(),
scores.end())));
• Discard maximum element similarly
• Now use accumulate() algorithm
double avg = accumulate(scores.begin(),
scores.end(),0.0)/scores.size();
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
33
Proving Algorithms Correct
• Deductive proof of correctness may be
required
– In safety-critical systems where lives at risk
• Must specify
– The "given" or preconditions
– The "to show" or post conditions
Pre and Algorithm => Post
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
34
Deduction (演繹法; indicator, “⇨” )
• Deduction, a kind of inference form
– by reasoning from the general to the specific.
• Deduction (in logic) form of inference such that the
conclusion must be true if the premises are true.
– The famous Aristotelian syllogism is one species of
deductive reasoning.
– For example, if we know that all men have two legs
and that John is a man, it is then logical to deduce
that John has two legs.
All men have two legs.
John is a man.
⇨ John has two legs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
35
Induction ( 歸納法 )
• Induction (logic):
– The process of deriving general principles (i.e.,
rules) from particular facts or instances.
– A conclusion reached by this process.
• induction, in logic, a form of argument in which
the premises give grounds for the conclusion but
do not necessitate it.
– An important form of induction is the process of
reasoning from the particular to the general.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
36
Example: Recursive Power
Function
• Function:
double power (double x, unsigned n)
{ if ( n == 0 )
return 1.0;
else
return x * power (x, n-1); }
• Precondition:
– Input consists of a real number x and a nonnegative
integer n
• Postcondition:
– Execution of function terminates
– When it terminates value returned is xn
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
37
Example: Recursive Power
Function
• Use mathematical induction on n
– Show postcondition follows if n = 0
• Assume for n = k, execution terminates and
returns correct value
– When called with n = k + 1, inductive case
return x * power (x, n – 1) is
executed
– Value of n – 1 is k
– It follows that with n = k + 1, returns x * xk
which equals xk+1
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
38
Proving Algorithms Correct
• Notation could be used to state assertions
Pre
S
{ Post = Pre (v, e ) }
"If precondition Pre holds before an assignment
statement S of the form v = e is executed, then the
postcondition Post is obtained from Pre by replacing
each occurrence of variable v by expression e"
• Formal deductive system can be used to reason
from one assertion to next
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
39
Proving Algorithms Correct (cont.)
• A Very Simple case
Pre  A  Post
• More complex case
Ai
S
Ai+1
Pre  A1  A2  …  An  Post
• General cases might even contain many
selection paths (e.g., even more complex)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
40
Logic – validity vs. truth
• Logic, concerned with reasoning and validity of arguments
– in general, in logic, we are not concerned with the truth of
statements, but rather are concerned with their validity.
– That is, although the following arguments (syllogism; 三段
論) is clearly logically, it is not something that we would
considered to be true.
All lemons are blue.
Mary is lemon.
Therefore, Mary is blue.
– This set of statements is considered valid because the
conclusions (Mary is blue) follows logically from the other
two statements, which we often call the premises.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
41
Logic – validity vs. truth (cont.)
 inference is not directly related to truth
– i.e. we can infer a sentence provided we have rules of
inference that produce the sentence from the original sentences.
For example,
• Black list: blocking all SMTP relays without reverse DNS
( heuristic; false positive).
• While list: accepting all messages with known envelope From
address (heuristic; false negative)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
42
Deduction vs. Induction
• Deduction has to do with necessity; induction has to do
with probability.
– Logicians contrast deduction with induction, in which the
conclusion might be false even when the premises are true.
• Facts: Lots of spam mails are originated from Internet
sites without reverse DNS mappings.
– Induction rule (valid but not necessarily true):
• E-mail messages relayed through incoming MTAs/MUAs, without
reverse DNS mappings, will be considered as SPAM messages.
• Specific-to-general; maybe false positive
– Deduction rule (general principle, or policy):
• E-mail messages relayed through incoming MTAs (or from NCTU
MUAs), without reverse DNS mappings, should be blocked (i.e.,
considered as SPAM messages. )
• General-to-specific
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson
Education, Inc. All rights reserved. 0-13-140909-3
43