Logs and Exponents: CSE 373 Lecture 2: Mathematical Background
Logs and Exponents: CSE 373 Lecture 2: Mathematical Background
Logs and Exponents: CSE 373 Lecture 2: Mathematical Background
✦ Today, we will review: ✦ We will be dealing mostly with binary numbers (base 2)
µ Logs and exponents
✦ Definition: logX B = A means X A = B
µ Series
µ Recursion ✦ Any base is equivalent to base 2 within a constant factor:
µ Big-Oh notation for analysis of algorithms
log 2 B
✦ Covered in Chapters 1 and 2 of the text log X B =
log 2 X
✦ Why?
K+ N = ∑i = ?
N
✦ S (N ) = 1+ 2 + ✦ Consider the following program segment:
i =1 for (i = 1; i <= N; i++)
✦ The sum is: S(1) = 1, S(2) = 3, S(3) = 6, S(4) = 10, … for (j = 1; j <= i; j++)
printf(“Hello\n”);
✦ Is S(N) = N(N+1)/2 ?
µ Prove by induction (base case: N = 1, S(N) = 1(2)/2 = 1) ✦ How many times is “printf” executed?
µ Assume true for N = k: S(k) = k(k+1)/2 µ Or, How many Hello’s will you see?
µ Suppose N = k+1.
µ S(k+1) = 1 + 2 + …+ k + (k+1) = S(k) + (k+1)
= k(k+1)/2 + (k+1) = (k+1)(k/2 + 1) = (k+1)(k+2)/2. ✔
∑ i = N ( N2 + 1)
N
✦ Why is this formula useful?
i =1
A Sneak Preview of Algorithm Analysis Other Important Series (know them well!)
∑i
✦ The program segment being analyzed: ✦ Sum of squares: N
N ( N + 1)(2 N + 1) N 3
for (i = 1; i <= N; i++)
2
= ≈ for large N
i =1 6 3
for (j = 1; j <= i; j++)
∑i N k +1
N
printf(“Hello\n”);
✦ Sum of exponents:
k
≈ for large N and k ≠ -1
i =1 | k +1|
✦ Inner loop executes “printf” i times in the ith iteration
∑ i = N ( N2 + 1)
N
∑ A = AA −−1 1
i =1 N N +1
i
✦ Congratulations - You’ve just analyzed your first program! ✦ Geometric series:
µ Running time of the program is proportional to N(N+1)/2 for all N.
i =0
✦ Classic example: Fibonacci numbers Fn ✦ Easy to write: looks like the definition of Fn
1, 1, 2, 3, 5, 8, 13, 21, 34, … ✦ But, can you spot a big problem?
fib0 = fib1;
fib1 = fibj;
return fibj;
Run Time
✦ You may use induction to prove your program is correct Which algorithm
µ See example in previous lecture
would you choose?
TB
R. Rao, CSE 373 Lecture 1 13 R. Rao, CSE 373 Lecture 1 Input Size N 14
Motivation for Big-Oh Notation (cont.) Motivation for Big-Oh: Asymptotic Behavior
✦ For large N, the running time of A and B is: ✦ In general, what really matters is the “asymptotic”
performance as N → ∞, regardless of what happens for
small input sizes N.
Now which ✦ Performance for small input sizes may matter in practice,
Run Time
✦ Example 2: Suppose T(N) = 50N+11. Then, T(N) = O(N) ✦ Example 2: Suppose T(N) = 50N+11. Then, T(N) = O(N)
µ T(N) ≤ 50N+11N = 61N for N ≥ 1. So, c = 61 and n0 = 1 works µ T(N) ≤ 50N+11N = 61N for N ≥ 1. So, c = 61 and n0 = 1 works
✦ Example 3: TA(N) = 50N, TB(N) = N2. ✦ Example 3: TA(N) = 50N, TB(N) = N2.
Show that TA(N) = O(TB(N)): what works for c and n0 ? TA(N) = O(TB(N)): choose c = 1 and n0 = 50
Name Big-Oh
Constant O(1)
Log log O(log log N) Next Lecture: Using Big-Oh for Algorithm Analysis
Increasing cost
Logarithmic O(log N)
Log squared O((log N)2)
To do:
}
Linear O(N)
N log N O(N log N)
Polynomial time Finish reading Chapters 1 and 2
Quadratic O(N2)
Cubic O(N3)
Set up your account at MSCC lab
Exponential O(2N)