Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Algorithm Design approaches

Dr. Jey Veerasamy


Petrol cost minimization problem
S T

• You need to go from S to T by car, spending


the minimum for petrol.

2
Quick-sort
Merge-sort
Divide & Conquer - characteristics
# of stops minimization problem
S T

• Similar to petrol cost minimization problem,


but we are minimizing # of stops.
Activity selection problem
Fractional knapsack problem
• Gold powder, silver powder, …
• Thief has limited weight capacity
Greedy approach - characteristics
Fibonacci number problem
• Definition of Fibonacci number:
F(1) = 1
F(2) = 2
F(n) = F(n-1) + F(n-2), when n > 2

• Recursive solution using D&C approach:


int f(int n)
{
if (n == 1)
return 1;
else if (n == 2)
return 2;
else
return f(n-1) + f(n-2);
}

10
D&C Solution for F(6)
F(6)

F(5) F(4)

F(4) F(3) F(3) F(2)

F(3) F(2) F(2) F(1) F(2) F(1)

F(2) F(1) Several subproblems are solved


repeatedly - Not an efficient
approach.
11
DP solution for F(6)
 DP uses a table and bottom-up approach (note that
D&C used top-down approach):

int fib(int n)
{ Computations are NOT repeated!
int f[n+1];
f[1] = 1;
f[2] = 2;
for (i=3 ; i<= n ; i++)
f[i] = f[i-1]+f[i-2];
return f[n];
}
F(1) F(2) F(3) F(4) F(5) F(6)
12
Knapsack problem
• Individual pieces with specific weights, thief
wants to know whether there is an exact
match for his weight limit.
Dynamic programming - characteristics

You might also like