

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
During the study of discrete mathematics, I found this course very informative and applicable.The main points in these lecture slides are:Array of Numbers, Iterative Algorithms, Proof of Correctness, Reasonable Output for Function, Minimum Element of Array, Loop Counter, Running Time of Algorithm, Algorithm Complexity, Linear Search Algorithm
Typology: Slides
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Complexity is O(n)
vars x 1 x 2 x 3 x (^4) input 3 2 4 1 j = 1 3 2 4 1 j = 2 2 3 4 1 j = 3 2 3 4 1 final 2 3 1 4
Algorithm MAX Input: x 1 , x 2 , …, x (^) n, an array of numbers Output: y, the maximum of x 1 , x 2 , …, x (^) n
iteration (j = n-1) gives the result we really want.
Algorithm MAX Input: x 1 , x 2 , …, x (^) n, an array of numbers Output: x (^) n, the maximum of x 1 , x 2 , …, x (^) n
Base case (j = 0 th^ iteration): x 1 = max{x 1 }
IH: assume assertion holds for j = n th^ iteration.
On n+1 st^ iteration, x (^) n+1 is compared with x (^) n+2 and max is swapped into x (^) n+.
But x (^) n+2 = max{x (^) n+1 , x (^) n+2 } = max{ x 1 , x 2 , …, x (^) n+1, x (^) n+2} by IH.
Algorithm BUBBLE
Input: x 1 , x 2 , …, x (^) n, an array of numbers
Output: ??
The running time of this algorithm is:
A. O(n log n)
B. O(n)
C. O(n^2 )
D. None of the above.
How long does the “linear search algorithm” take?
Suppose the data is (2, 8, 3, 9, 12, 1, 6, 4, 10, 7) and we’re looking for # (^1284)
The running time of the algorithm depends on the particular input to the problem. In this case we have two different complexity measures: Worst case complexity - running time on worst input Average case - average running time among all inputs
Worst case for linear search is time n.
Average case for linear search is time (1+2+…+n)/n = n(n+1)/2n = (n+1)/2. Both are O(n)
How long does the “binary search algorithm” take?
Binary search
Input: a sorted array of numbers a 1 , a 2 , … an and a number x
Output: position i where ai = x, if x is in the list
If n is a power of 2, n = 2 k^ , then k = lg n iterations occur.
If n is not a power of 2, let k be the number so that 2k < n < 2 k+1^ , and imagine that the array has 2 k+1^ elements. Then k+1 < lg n + 1 = O(log n)
It’s fun to make comparisons about the running times of algorithms of various complexities.
Inp size
compxity
n .00001s .00002s .00003s .00004s .00005s .00006s
n^2 .0001s .0004s .0009s .0016s .0025s .0036s
n^5 .1s 3.2s 24.3s 1.7m 5.2m 13m
3 n^ .059s 58m 6.5y 3855c 2x10 8 c 1.3x^
13 c
But computers are getting faster! Maybe we can do better.
Why would we ever use “worst case complexity”?
Advantages: Easiest to analyze. Bounds the worst possible thing that could happen (conservative). Don’t have to decide or assume what typical inputs are (or find a distribution on inputs).
Disadvantage: Bizarre inputs could make worst-case running time seem horrible, when most inputs terminate in reasonable time.
Factorial (n)
Input: an integer n > 0.
Output: n!
Let T(n) denote the running time of the algorithm on input of size n.
T(n) = C + T(n-1)
T(1) = c
T(n) = C + (C + T(n-2))
= C + (C + (C + T(n-3))) … = nC = O(n)
Now let’s analyze the running time.
T(n) = 2 T(n-1) + C Unroll a bit…
= 2 (2 T(n-2) + C) + C = 4 T(n-2) + 3C
= 4 (2 T(n-3) + C) + 3C = 8 T(n-3) + 7C
… = 2k^ T(n-k) + (2 k-1)C What is k when this bottoms out at 1?
= 2 n-1^ T(1) + (2 n-1-1)C = (2n-1) C
= O(2n)
More general techniques investigated in a couple weeks
Is the algorithm correct?
Does it do the right thing for 1 disk?
Assume it does the right thing for n-1 disks. (IH)
Yes
And finally, it DOES do the right thing for n disks: move n-1 out of the way, move the biggest disk, move n-1 back.
This doesn’t feel satisfying because we think about recursion inductively to begin with (as we should!).
Write down the recurrence relation:
T(n) = 2 T(n/2) + cn
We’ll talk about techniques for solving it later.
Recursive function for computing the nth Fibonacci number:
Iterative function for computing the nth Fibonacci number:
Which of these is better?Docsity.com