Algorithm Analysis: Prof - Dr.Eng - Ir Taufik Djatna
Algorithm Analysis: Prof - Dr.Eng - Ir Taufik Djatna
Analysis
Prof.Dr.Eng.Ir Taufik Djatna
1
Algorithm Analysis - Class Goals
Analysis of Algorithms from both practical
(programming) and theoretical (mathematical) points of
view
Efficiency of Algorithms - Time and Space
Explore Fundamental Algorithmic Paradigms
– Divide and Conquer
– Graph Algorithms
– Greedy Algorithms
– Dynamic Programming
– Linear Programming
– Local Search
– Intelligent Search
– Probabilistic Algorithms
2
Algorithms
3
Additional Goals
Develop your general problem solving and programming
skills
Use Python
Prep for Job Interviews
4
Before we ask what is an Algorithm,
Let's ask:
What is Computation?
5
What is Computation?
6
What is Computation?
Deterministic mappings
What Features would we like for such computational machines
Humans?, Machine vs Human Intelligence?, come to CS 472
7
What is an Algorithm?
What is an Algorithm?
No single formal definition
A typical informal definition: “A finite sequence of
well defined steps for solving a problem.”
This definition has been influenced by the types of
computational machines we have used.
Is all computation done by algorithms so defined?
– Sequence of operations?
– Termination?
3 Basic Questions about any Algorithm
Start with a problem and then propose an algorithmic solution:
1. ?
10
3 Basic Questions about any Algorithm
Start with a problem and then propose an algorithmic solution:
11
3 Basic Questions about any Algorithm
Start with a problem and then propose an algorithmic solution:
12
3 Basic Questions about any Algorithm
Start with a problem and then propose an algorithmic solution:
13
3 Basic Questions about any Algorithm
Start with a problem and then propose an algorithmic solution:
14
A Bit of History
Abu Jafar Muhammad ibn Musa, c. 780 - 850
– “Al Khwarizmi” (of Kwarizm) his place of origin (Uzbekistan)
– Persian scholar in Baghdad
– Used decimal (positional system as contrasted with Roman
numerals) and proposed numerous arithmetic algorithms which
could not be done the the Roman numeral system
– Latin version of this name is Algoritmi
– From this came the word Algorithm
Leonardo of Pisa (Fibonacci) 1170-1250
– Pushed the use of decimal system in Europe
– Most remembered for the Fibonacci series of numbers
15
Fibonacci Series
16
An Algorithm - fib1
function fib1(n)
if n = 0: return 0
if n = 1: return 1
return fib1(n-1) + fib1(n-2)
Is it correct?
How much time does it take, as a function of n?
Can we do better?
17
How long does it take
Note that with each call there are two more calls
(doubling each time)
What is the depth of the calling tree?
18
How long does it take
19
How long does it take
Note that with each call there are two more calls
(doubling each time)
What is the depth of the calling tree?
Thus, exponential time!
Since tree is binary, but not full it is somewhat less than,
but close to O(2n)
How bad is that?
20
How long does it take
Note that with each call there are two more calls
(doubling each time)
What is the depth of the calling tree?
Thus exponential time
Since tree is binary, but not full it is somewhat less than,
but close to O(2n)
How bad is that? Calculating fib1(200) takes longer on
current fastest computer than the life of the sun.
Moore’s Law?
Parallelism?
Exponential is Intractable!
21
How long does it take - Bit more Careful
Assume T(n) is the time necessary to compute F(n) for
fib1. What is the recurrence relation?
T(n) = ?
function fib1(n)
if n = 0: return 0
if n = 1: return 1
return fib1(n-1) + fib1(n-2)
22
How long does it take - Bit more Careful
Assume T(n) is the time necessary to compute F(n) for
fib1. What is the recurrence relation?
T(n) = T(n-1) + T(n-2) + 3 when n > 1
function fib1(n)
if n = 0: return 0
if n = 1: return 1
return fib1(n-1) + fib1(n-2)
23
How long does it take - a Bit more Careful
Assume T(n) is the time necessary to compute F(n) for
fib1. What is the recurrence relation?
T(n) = T(n-1) + T(n-2) + 3 when n > 1
Note that T(n) > F(n)
F(n) ≈ 20.694n ≈ 1.6n, where .694 are the first 3 digits of an
irrational number
T(n) ≈ 1.6n
24
Can we do better?
25
Approximation
Sometimes we can find a closed-form solution
Fib(n) ≈ 20.694n ≈ 1.6n, where .694 are the first 3 digits of an irrational
number
This direct calculation Fib(n) ≈ 20.694n trades off correctness for speed
Approximate solution – very common for many problems as we shall
see later
26
Can we do better?
27
Dynamic Programming Approach
Store the intermediate results and avoid duplicate work
function fib2(n)
if n=0: return 0
create an array f[0..n]
f[0] = 0, f[1] = 1
for i = 2 to n:
f[i] = f[i-1] + f[i-2]
return f[n]
28
Orders of Growth – Some Complexity Classes
n log2n n nlog2n n2 n3 2n n!
n log2n n nlog2n n2 n3 2n n!
To calibrate, there are about 1057 atoms in the solar Efficient Not Efficient
system and 1080 atoms in the current known universe 30
Asymptotic Complexity - Big-O analysis
31
Asymptotic Notation
We say
32
Examples
How could you prove that 4n = (n)?
How could you prove that n/2 - 5 = (n)?
Note that represents an equivalence class
– if f(n) = (g(n)) then g(n) = (f(n))
Could we represent (n) as (3+n/2)
– Why don't we?
– (n) includes all functions which differ by only a constant factor * n
33
Asymptotic Notation
34
Asymptotic Notation
f(n)
f(n)
c1g(n) c g(n)
n0 n0 n0
36
Max Rule (do first!):
O( f(n) + g(n) ) = O( max( f(n), g(n) ) )
The Limit Rule
Some Examples
37
** Challenge Question **
38
Complexity Analysis
Best case, average case, worst case
Usually consider worst case, unless very rare
– If worst case rare, then average case becomes more interesting
39