Chapter 1 - Introduction
Chapter 1 - Introduction
2
Analyzing Algorithms
Def: Running time = the number of primitive operations (steps) executed
before termination
– Arithmetic operations (+, -, *), data movement, control, decision making
(if, while), comparison
3
Algorithm Efficiency vs. Speed
E.g.: sorting n numbers Sort 106 numbers!
2 106 instructio ns
2
Your friend = 9
2000seconds
10 instructio ns / second
50 106 lg10 6 instructio ns
You =
7
100seconds
10 instructio ns / second
20 times better!!
4
Algorithm Analysis: Example
• Alg.: MIN (a[1], …, a[n])
m ← a[1];
for i ← 2 to n
if a[i] < m
then m ← a[i];
• Running time:
– the number of primitive operations (steps) executed
before termination
F(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
• Order (rate) of growth:
– The leading term of the formula
– Expresses the asymptotic behavior of the algorithm
5
Typical Running Time Functions
• 1 (constant running time):
– Instructions are executed once or a few times
• logN (logarithmic)
– A big problem is solved by cutting the original problem in smaller
sizes, by a constant fraction at each step
• N (linear)
– A small amount of processing is done on each input element
• N logN
– A problem is solved by dividing it into smaller problems, solving
them independently and combining the solution
6
Typical Running Time Functions
• N2 (quadratic)
– Typical for algorithms that process all pairs of data items (double
nested loops)
• N3 (cubic)
– Processing of triples of data (triple nested loops)
• NK (polynomial)
• 2N (exponential)
– Few exponential algorithms are appropriate for practical use
7
Why Faster Algorithms?
8
Computing the Complexity
• Each statement cost 1 unit of time except the iterative and recursion
statements
• Iterative Statement:
– Linear loop
• Examples:
– Nested loops
– Logarithmic loop
9
Computing the Complexity
– Nested loops
• Ex:
For(i=1;i<n;i++)
for(j=1;j<n;j++)
statement
• Complexity f(n)=O(n2)
For(i=1;i<n;i++)
for(j=1;j<i;j++)
statement
• Complexity f(n)=O(n2)
10
Computing the Complexity
– Logarithmic loop
i=1;
While(i<=n)
st1
st2
i=i*2
– Complexity: F(n)=O(log n)
1 1
2 2
3 4
N 2(I-1)
11
Asymptotic Notations - Examples
• notation
– n2/2 – n/2 = (n2)
– (6n3 + 1)lgn/(n + 1) = (n2lgn)
– n vs. n2 n ≠ (n2)
• notation • O notation
– n3 vs. n2 n3 = (n2) – 2n2 vs. n3 2n2 = O(n3)
– n vs. logn n = (logn) – n2 vs. n2 n2 = O(n2)
– n vs. n2 n (n2) – n3 vs. nlogn n3 O(nlgn)
12
Types of Analysis
• Worst case (e.g. cards reversely ordered)
– Provides an upper bound on running time
– An absolute guarantee that the algorithm would not
run longer, no matter what the inputs are
• Best case (e.g., cards already ordered)
– Input is the one for which the algorithm runs the
fastest
• Average case (general case)
– Provides a prediction about the running time
– Assumes that the input is random
13