Download presentation
Presentation is loading. Please wait.
Published bySamuel Hopkins Modified over 9 years ago
1
Mathematical Background and Linked Lists
2
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
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
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
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
6 Induction Example: Geometric Closed Form Prove: x 0 + x 1 + … + x n = (x n+1 – 1) / (x – 1) for all x 1 Basis: show that x 0 = (x 0+1 - 1) / (x - 1): x 0 = 1 = (x 1 – 1) / (x – 1) Inductive hypothesis: assume that x 0 + x 1 + … + x k = (x k+1 – 1) / (x – 1)
7
7 Induction Example: Geometric Closed Form Step (show true for n+1): x 0 + x 1 + … + x k+1 = (x 0 + x 1 + … + x k ) + x k+1 = (by hypothesis) (x k+1 – 1) / (x – 1) + x k+1 = (x k+1 – 1 + x k+1 (x – 1)) / (x – 1) = (x k+1 – 1 + x k+2 – x k+1 ) / (x – 1) = (x k+2 – 1) / (x – 1)
8
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
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 2 0 =1, 2 1 =2, 2 2 =4, 2 4 =16, 2 8 =256, … An n-bit wide field can hold 2 n positive integers: 0 k 2 n -1
10
10 Unsigned Binary Numbers Each bit represents a power of 2 For unsigned numbers: The minimum value is 0 The maximum value is 2 n -1, where n is the number of bits So, for example 5 bits => 32 possible values (2 5 ) 10 bits => 1024 possible values (2 10 )
11
11 Binary and Decimal 2 0 =1 2 1 =2 2 2 =4 2 3 =8 2 4 =162 5 =32 2 6 =642 7 =1282 8 =256 Decimal 10 11 3 1001 9 1010 10 1111 15 0000 16 1 1111 31 1 1111 127 1 11 1111 255 1 111
12
12 Logs and Exponents Definition: log 2 x = y means x = 2 y The log of x in base 2, is the value of y that gives x = 2 y 8 = 2 3, so log 2 8 = 3 65536= 2 16, so log 2 65536 = 16 Notice that log 2 x tells you how many bits are needed to hold x values 8 bits hold 256 numbers: 0 to 255 (2 8 -1) log 2 256 = 8
13
13 2 x and log 2 x – for Small x ’ s
14
14 2 x and log 2 x – for Large x ’ s
15
15 Floor and Ceiling – Floor function: the largest integer < X – Ceiling function: the smallest integer > X
16
16 Floor and Ceiling Properties: More examples:
17
17 Example: log 2 x and Tree Height n items in an almost full binary tree, the tree height (length of longest path) is log 2 n 4 2 6 5 1 3
18
18 Properties of logs Usually, we work in log base 2 In base 2: a = 2 log 2 a log 2 a n = n · log 2 a Similarly, in any base b: a = b log b a log b a n = n · log b a
19
19 Properties of logs Claim: log a · b = log a + log b Proof: a = 2 log 2 a and b = 2 log 2 b a · b = 2 log 2 a · 2 log 2 b = 2 log 2 a+log 2 b Therefore: log 2 a · b = log 2 a + log 2 b Note: log a · b log a · log b
20
20 Other log Properties log a/b = log a – log b Special case: log 2 1/a = – log 2 a Base change: log a n = log b n / log b a log log X 0 log log X = Y means log X grows slower than X Called a “ sub-linear ” function
21
21 Log Base Change Any base x log is equivalent to base 2 log within a constant factor Example: log 10 n = log 2 n / log 2 10 log 2 10 = 10/3 Therefore: log 10 n = 0.3 log 2 n
22
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
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)
24
24 Monotonic Functions monotonically non-decreasing monotonically non-increasing neither
25
25 Monotonic Functions – Examples f(x) = x 2 Decreasing for x 0, therefore does not fit any definition Monotonous non-decreasing
26
26 Arithmetic Series The sum is S(1) = 1 S(2) = 1 + 2 = 3 S(3) = 1 + 2 + 3 = 6
27
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
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(n 2 )
29
29 Geometric Series General geometric series: Common special case, for x = 2:
30
30 Infinite Geometric Series for |x| < 1 When |x| < 1, we can compute the sum of an infinite geometric series: Example, x = ½:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.