mathematical background and linked lists. 2 iterative algorithm for sum find the sum of the first n...
TRANSCRIPT
2
Iterative Algorithm for Sum
Find the sum of the first n integers stored in an array v:
sum (v[], n)
temp_sum 0
for i 0 to n – 1 do
temp_sum temp_sum + v[i]
return temp_sum
3
Programming Using Recursion
Write a recursive function that solves the same problem:
sum (v[], n)
if (n = 0) then
return 0
else
return (v[n-1] + sum (v, n-1))
4
Review: Induction
Suppose S(c) is true for a fixed constant c
Often c = 0
S(k) implies S(k+1) for all k >= c
Then S(n) is true for all n >= c
5
Proof By Induction
Claim: S(n) is true for all n >= c
Basis:Show S(n) is true for n = c
Inductive hypothesis:Assume S(n) is true for n = k
Step:Show that S(n) is then true for n = k + 1
6
Induction Example:Geometric Closed Form
Prove:x0 + x1 + … + xn = (xn+1 – 1) / (x – 1)
for all x 1
Basis: show that x0 = (x0+1 - 1) / (x - 1):x0 = 1 = (x1 – 1) / (x – 1)
Inductive hypothesis: assume thatx0 + x1 + … + xk = (xk+1 – 1) / (x – 1)
7
Induction Example:Geometric Closed Form
Step (show true for n+1):x0 + x1 + … + xk+1 =
(x0 + x1 + … + xk) + xk+1 = (by hypothesis)
(xk+1 – 1) / (x – 1) + xk+1 =
(xk+1 – 1 + xk+1 (x – 1)) / (x – 1) =
(xk+1 – 1 + xk+2 – xk+1) / (x – 1) =
(xk+2 – 1) / (x – 1)
8
Proving Program Correctness Using Induction
Basis Step: sum (v, 0) = 0
Inductive Hypothesis (n = k):Assume that sum (v, k) correctly returns the sum of the first k elements of v, i.e. v[0] + v[1] + … + v[k-1]
Inductive Step (n = k + 1): sum (v, n) returns v[k] + sum (v, k) which is the sum of the first k+1 elements of v
9
Powers of 2
Many of the numbers we use will be powers of 2
Binary numbers (base 2) are easily represented in digital computers
Each "bit" is a 0 or a 1
20=1, 21=2, 22=4, 24=16, 28=256, …
An n-bit wide field can hold 2n positive integers: 0 k 2n-1
10
Unsigned Binary NumbersEach bit represents a power of 2
For unsigned numbers:The minimum value is 0The maximum value is 2n-1, where n is the number of bits
So, for example5 bits => 32 possible values (25)10 bits => 1024 possible values (210)
11
Binary and Decimal
20=1
21=2
22=4
23=8
24=16
25=32
26=64
27=128
28=256
Decimal10
1 1 3
1 0 0 1 9
1 0 1 0 10
1 1 1 1 15
0 0 0 0 161
1 1 1 1 311
1 1 1 1 127111
1 1 1 1 2551111
12
Logs and ExponentsDefinition: log2x = y means x = 2y
The log of x in base 2, is the value of y that gives x = 2y
8 = 23, so log28 = 3
65536= 216, so log265536 = 16
Notice that log2x tells you how many bits are needed to hold x values
8 bits hold 256 numbers: 0 to 255 (28-1)
log2256 = 8
15
Floor and Ceiling
X
X
– Floor function: the largest integer < X
– Ceiling function: the smallest integer > X
2232.722.7
2222.332.3
17
Example:log2x and Tree Height
n items in an almost full binary tree,
the tree height (length of longest path) is log2n
4
2 6
51 3
18
Properties of logs
Usually, we work in log base 2
In base 2:a = 2log
2a
log2an = n·log2a
Similarly, in any base b:a = blog
ba
logban = n·logba
19
Properties of logs
Claim:log a·b = log a + log b
Proof:a = 2log
2a and b = 2log
2b
a·b = 2log2a · 2log
2b = 2log
2a+log
2b
Therefore: log2a·b = log2a + log2b
Note: log a·b log a·log b
20
Other log Properties
log a/b = log a – log bSpecial case: log 2 1/a = – log 2 a
Base change: logan = logbn / logba
log log X < log X < X for all X > 0 log log X = Y means
log X grows slower than X Called a “sub-linear” function
X2Y2
21
Log Base Change
Any base x log is equivalent to base 2 log within a constant factor
Example:log10n = log2n / log210
log210 = 10/3
Therefore:log10n = 0.3 log2n
22
Monotonic Functions
A function is called monotonically increasing if for all x, y such that x > y:f(x) > f(y)
Example: f(x) = x
A function is called monotonically non-decreasing if for all x, y such that x > y:f(x) ≥ f(y)
Example: f(x) = 5
23
Monotonic Functions
A function is called monotonically decreasing if for all x, y such that x > y:f(x) < f(y)
Example: f(x) = -x
A function is called monotonically non-increasing if for all x, y such that x > y:f(x) ≤ f(y)
25
Monotonic Functions – Examples
f(x) = x2
Decreasing for x < 0, increasing for x > 0, therefore does not fit any definition
Monotonous non-decreasing
1xx
1x01
0xx
f(x)
26
Arithmetic Series
The sum isS(1) = 1
S(2) = 1 + 2 = 3
S(3) = 1 + 2 + 3 = 6
n
1i
in21S(n)
n
1i 2
1)n(ni
27
Algorithm Analysis
Consider the following program segment:x 0
for i 1 to n do
for j 1 to i do
x x + 1
What is the value of x at the end?
28
Analyzing the Loop
Total number of times x is incremented:
The running time of the program is proportional to n(n+1)/2 for all n
O(n2)
2
nnin21S(n)
n
1i
)1(
29
Geometric Series
General geometric series:
Common special case, for x = 2:
n+1n
k 2 n
k=0
x -1x =1+ x + x +...+ x =
x -1
n
k 2 n n+1
k=0
2 =1+2+2 +...+2 = 2 -1