300 likes | 325 Views
Explore iterative algorithms, induction proofs, geometric closed form, binary numbers, logs, and algorithm analysis using recursion. Understand monotonous functions & arithmetic series.
E N D
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
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))
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
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
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 that x0 + x1 + … + xk = (xk+1– 1) / (x – 1)
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)
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
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
Unsigned Binary Numbers • Each bit represents a power of 2 • For unsigned numbers: • The minimum value is 0 • The maximum value is 2n-1, where n is the number of bits • So, for example • 5 bits => 32 possible values (25) • 10 bits => 1024 possible values (210)
Binary and Decimal 28=256 27=128 26=64 25=32 24=16 21=2 22=4 23=8 20=1 Decimal10 3 1 1 9 1 0 0 1 10 1 0 1 0 15 1 1 1 1 16 1 0 0 0 0 31 1 1 1 1 1 127 1 1 1 1 1 1 1 255 1 1 1 1 1 1 1 1
Logs and Exponents • Definition: 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
Floor and Ceiling – Floor function: the largest integer < X – Ceiling function: the smallest integer > X
Floor and Ceiling • Properties: • More examples:
Example:log2x and Tree Height • n items in an almost full binary tree,the tree height (length of longest path) is log2n 4 6 2 5 1 3
Properties of logs • Usually, we work in log base 2 • In base 2: • a = 2log2a • log2an = n·log2a • Similarly, in any base b: • a = blogba • logban = n·logba
Properties of logs • Claim: • log a·b = log a + log b • Proof: • a = 2log2a and b = 2log2b • a·b = 2log2a ·2log2b = 2log2a+log2b • Therefore: log2a·b = log2a + log2b Note: log a·blog a·log b
Other log Properties • log a/b = log a – log b • Special 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
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
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
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)
Monotonic Functions monotonically non-increasing monotonically non-decreasing neither
Monotonic Functions – Examples • f(x) = x2 • Decreasing for x < 0, increasing for x > 0, therefore does not fit any definition • Monotonous non-decreasing
Arithmetic Series • The sum is • S(1) = 1 • S(2) = 1 + 2 = 3 • S(3) = 1 + 2 + 3 = 6
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?
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)
Geometric Series • General geometric series: • Common special case, for x = 2:
Infinite Geometric Seriesfor |x| < 1 • When |x| < 1, we can compute the sum of an infinite geometric series: • Example, x = ½: