Escolar Documentos
Profissional Documentos
Cultura Documentos
Homework
Agenda: Analysis of
Algorithms
Blackboard
Issues:
Correctness
Time efficiency
Space efficiency
Optimality
Approaches:
Theoretical analysis
Empirical analysis
Time Efficiency
Time efficiency is analyzed by determining the
number of repetitions
of the basic operation
as a function of input size
Theoretical analysis of
time efficiency
input size
T(n) copC(n)
running time
execution time
for basic operation
Number of times
basic operation is
executed
Basic operation
Number of items in
list n
Key comparison
Dimensions of
matrices
Floating point
multiplication
Compute an
Floating point
multiplication
Graph problem
#vertices and/or
edges
Visiting a vertex or
traversing an edge
Best-case, average-case,
worst-case
For some algorithms efficiency depends on type of input:
Worst case:
W(n) maximum over inputs of size n
Best case:
B(n) minimum over inputs of size n
Average case:
Example: Sequential
search
Exact formula
e.g., C(n) = n(n-1)/2
Formula indicating order of growth with specific
multiplicative constant
e.g., C(n) 0.5 n2
Order of growth
Table 2.1
Day 4: Agenda
O, ,
Limits
Definitions
Examples
Code O(?)
Homework
Big-oh
Big-omega
Big-theta
Using Limits
0
t ( n)
lim
c
n g ( n)
LHopitals Rule
t ( n) t ' ( n)
lim
n g ( n)
g ' ( n)
Examples:
10n
n(n+1)/2
logb n
vs.
vs.
vs.
2n2
n2
logc n
Definition
f(n) = O( g(n) ) if there exists
a positive constant c and
a non-negative interger n0
such that
f(n) cg(n)
for every n > n0
Examples:
10n is O(2n2)
5n+20 is O(10n)
Basic Asymptotic
Efficiency classes
1
log n
n
n log n
n2
n3
2n
n!
constant
logarithmic
linear
n log n
quadratic
cubic
exponential
factorial
Non-recursive
algorithm analysis
Analysis Steps:
Decide on parameter n indicating input size
Identify algorithms basic operation
Determine worst, average, and best case for input
of size n
Set up summation for C(n) reflecting algorithms
loop structure
Simplify summation using standard formulas
Example
for (x = 0; x < n; x++)
a[x] = max(b[x],c[x]);
Example
for (x = 0; x < n; x++)
for (y = x; y < n; y++)
a[x][y] = max(b[x],c[y]);
Example
for (x = 0; x < n; x++)
for (y = 0; y < n/2; y++)
for (z = 0; z < n/3; z++)
a[z] = max(a[x],c[y]);
Example
y=n
while (y > 0)
if (a[y--] == b[y--])
break;
Day 5: Agenda
Homework
Day 6: Agenda
Homework 4
Basic operations
Single loops
Nested loops
for (x = 0; x < n; x++)
for (y = 0; y < n; y++)
O(n2)
for (x = 0; x < n; x++)
for (y = 0; y < n; y++)
for (z = 0; z < n; z++)
O(n3)
But remember: We can have c independent nested
loops, or the loops can be terminated early n/c.
Most non-recursive
algorithms reduce to one of
these efficiency classes
1
log n
n
n log n
n2
n3
2n
constant
logarithmic
linear
n log n
quadratic
cubic
exponential
What else?
Anything else?
Log2n
for (x = 0; x < n; x++) {
Basic operation;
x = x*2;
}
Log2n
Last thing: 2n
Think
Recursive way
fun(int x) {
if (x > 0) {
print *;
fun(x-1);
fun(x-1);
}
}
Then call the function fun(n);
Non-recursive way
for (x = 0; x < pow(2,n); x++)
print *;
WTF?
Be very wary of your input size.
If you input size is truly n, then this is truly
O(2n) with just one loop.
Thats it!
Enigma
Algorithm Analysis
Recursive Algorithm Toolkit
Example Recursive
evaluation of n !
Definition: n ! = 1*2**(n-1)*n
Recursive definition of n!:
if n=0 then F(n) := 1
else F(n) := F(n-1) * n
return F(n)
Important recurrence
types:
linear
T(1) = d
Important recurrence
types:
quadratic
Important recurrence
types:
logarithmic
T(1) = d
Important recurrence
types:
T(1) = d
2.
a < bk
a = bk
T(n) = (nk)
T(n) = (nk lg n )
3.
a > bk
T(n) = (nlog b a)
1.
Recursive Algorithm
Analysis
Input: an array of floats a[0n-1] and
a integer counter x
Fun(int x, float a[]) {
if (x == 0)
return a[0];
if (a[0] > a[x])
swap(a[0], a[x]);
Fun(x-1, a);
}
Example
951638
Fun(5, a)
851639
Fun(4, a)
351689
Fun(3, a)
351689
Fun(2, a)
153689
Fun(1, a)
153689
Fun(0, a)
return a[0] 1
Analysis
Analysis
Second, identify
what terminates
the recursion
Think of the
running time as
function Fun(x)
Fun(0) is the base
case
Analysis
Analysis
Analysis
Example
Fun(v[1n]) {
if size of v is 1 return 1
else
q = Fun(v[1..n-1]);
if (v[q] > v[n])
swap(v[q], v[n]);
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Fun(3 1 4 2 5):
temp = Fun(3 1 4 2)
Fun(3 1 4 2):
temp = Fun(3 1 4)
Fun(3 1 4):
temp = Fun(3 1)
Fun(3 1):
temp = Fun(3)
Fun(3):
return 1;
Fun(3 1):
v[q] = 3; v[n] = 1; v: 1 3 4 2 5; return 2
Fun(3 1 4):
v[q] = 3; v[n] = 4; v: 1 3 4 2 5; return 3
Fun(3 1 4 2):
v[q] = 4; v[n] = 2; v: 1 3 2 4 5; return 4
Fun(3 1 4 2 5):
v[q] = 4; v[n] = 5; v: 1 3 2 4 5; return 5
Example
Fun(v[1n]) {
if size of v is 1 return 1
else
q = Fun(v[1..n-1]);
if (v[q] > v[n])
swap(v[q], v[n]);
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Example
Second, when
the size of the
vector is 1 the
recursion
terminates
Fun(v[1n]) {
if size of v is 1 return 1
else
q = Fun(v[1..n-1]);
if (v[q] > v[n])
swap(v[q], v[n]);
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Fun(v[1n]) {
if size of v is 1 return 1
Example
else
q = Fun(v[1..n-1]);
Third, the basic
if (v[q] > v[n])
operations are
swap(v[q], v[n]);
not simple
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Example
There is O(1)
compare and
swap
Fun(v[1n]) {
if size of v is 1 return 1
else
q = Fun(v[1..n-1]);
if (v[q] > v[n])
swap(v[q], v[n]);
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Fun(v[1n]) {
if size of v is 1 return 1
Example
else
q = Fun(v[1..n-1]);
There is an O(n)
if (v[q] > v[n])
loop with O(3)
swap(v[q], v[n]);
operations
temp = 0;
inside
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Example
Fourth, the
recursive call
decrease the
vector size (n)
by one
Fun(v[1n]) {
if size of v is 1 return 1
else
q = Fun(v[1..n-1]);
if (v[q] > v[n])
swap(v[q], v[n]);
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Example
Fun(v[1n]) {
if size of v is 1 return 1
else
q = Fun(v[1..n-1]);
if (v[q] > v[n])
swap(v[q], v[n]);
temp = 0;
for x = 1 to n
if (v[x] > temp)
temp = v[x];
p = x;
return p;
}
Summary of simple
recursive algorithms
1.
2.
nn-1
This means the recursion is simply an O(n) loop.
This leads to two common cases:
Case #1:
fun(n) {
if (n==1) quit
O(1)
fun(n-1)
} O(n)
Case #2:
fun(n) {
if (n==1) quit
O(n)
fun(n-1)
} O(n2)
n n-1
Generic case
fun(n) {
if (n==1) quit
O(nk)
fun(n-1)
} O(nk+1)
nn/2
This means the recursion is simply an O(log n)
loop. This leads to three common cases:
Case #3:
fun(n) {
if (n==1) quit
O(1)
fun(n/2)
} O(log n)
Case #4:
fun(n) {
if (n==1) quit
O(n)
fun(n/2)
fun(n/2)
} O(n log n)
Case #5:
fun(n) {
if (n==1) quit
O(n)
fun(n/2)
} O(n)
Generic Case:
T(n) = aT(n/b) + f (n)
2.
a < bk
a = bk
T(n) = (nk)
T(n) = (nk lg n )
3.
a > bk
T(n) = (nlog b a)
1.
Day 10 Agenda
Last Enigma
fun3(n,m) {
fun1(n) {
fun2(n) {
if (n==1) quit
if (n==1) quit
if (n==1) quit
O(m)
O(1)
O(n)
fun3(n-1,m)
fun1(n-1)
fun2(n-1)
fun3(n-1,m)
fun1(n-1)
fun2(n-1)
} O(n2n) if we
} O(2n)
} O(2n)
call fun(n,n)
Last Enigma
2n
1
1
+
1
1
1
1
1
8
+
4
+
2
+
1
fun1(n) {
if (n==1) quit
O(1)
fun1(n-1)
fun1(n-1)
} O(2n)
= 1 + 2 + 4 + + 2n
=(2n - 1) + 2n
=2(2n) 1
=O(2n)
Last Enigma
1
2n(1)
+
n-2
n-2
n-2
n-1
n-1
n
n-2
8(n-3)
+
4(n-2)
+
2(n-1)
+
1(n)
fun2(n) {
if (n==1) quit
O(n)
fun2(n-1)
fun2(n-1)
} O(2n)
= ??? =O(2n)
You have to see my
program to believe it!
Last Enigma
2nn
n
n
+
n
n
n
n
n
8n
+
4n
+
2n
+
1n
fun3(n,m) {
if (n==1) quit
O(m)
fun3(n-1,m)
fun3(n-1,m)
} O(n2n) if we call
fun(n,n)
= (1 + 2 + 4 + + 2n)n
=((2n - 1) + 2n)n
=(2(2n) 1)n
=O(n2n)
Summary of Enigmas
Summary of Enigmas
Summary of Enigmas
Case #3:
fun(n) {
if (n==1) quit
O(1)
fun(n/2)
} O(log n)
Case #5:
fun(n) {
if (n==1) quit
O(n)
fun(n/2)
} O(n)
Generic case:
fun(n) {
if (n==1) quit
O(nk)
fun(n/2)
} O(nk)
Summary of Enigmas
Exception:
fun(n) {
if (n==1) quit
O(1)
fun(n/2)
fun(n/2)
} O(n)
Case #4:
fun(n) {
if (n==1) quit
O(n)
fun(n/2)
fun(n/2)
} O(n log n)
Generic case
fun(n) {
if (n==1) quit
O(nk)
fun(n/2)
fun(n/2)
} O(nk)
Fibonacci numbers
Exam 1
Homework 4