FUNDAMENTAL ALGORITHMIC
TECHNIQUES
N Karmakar
DYNAMIC PROGRAMMING
Matrix Chain Multiplication
Matrix_Chain(Ai..j)
Optimal(s,i,j)
n←j–i+1
if i = j
for i ← 1 to n
return Ai
set m[i,i] ← 0
else
for l ← 2 to n
return ‘(’
for i ← 1 to n – l + 1
Optimal(s,i,s[i,j])
j←i+l–1
Optimal(s,s[i,j]
set m[i,j] ← ∞
+1,j)
for k ← i to j-1
return ‘)’
q ← m[i,k] + m[k+1,j]
+ dim1(Ai).dim2(Ak).dim2(Aj)
if q < m[i,j]
m[i,j] ← q
s[i,j] ← k
return m and s
0/1 Knapsack Problem
Knapsack capacity
Item Wt/ 0 1 2 3 4 5 6 7
Value
0 0 0 0 0 0 0 0
1 w1=1/ 0 1 1 1 1 1 1 1
v1=1
2 w2=3/ 0 1 1 4 5 5 5 5
v2 =4
3 w3=4/ 0 1 1 4 5 6 6 9
v3=5
4 w4=5/ 0 1 1 4 5 7 8 9
v4=7
0/1 Knapsack Problem
Knapsack_backtracking(n,W)
{
i = n; w = W; K = φ;
do
{
if(V[i,w] = V[i-1,w])
{
i = i – 1;
K = K U {i};
Knapsack capacity
}
else Ite Wt/ 0 1 2 3 4 5 6 7
m Value
K = K U {i};
w = w – w[i]; 0 0 0 0 0 0 0 0
i = i – 1; 1 w1=1/ 0 1 1 1 1 1 1 1
}while(w > 0); v1=1
return K;
w=3-3=0 2 w2=3/ 0 1 1 4 5 5 5 5
} v2 =4
w=7-4=3 3 w3=4/ 0 1 1 4 5 6 6 9
v3=5
4 w4=5/ 0 1 1 4 5 7 8 9
K = {3,2} v4=7
GREEDY APPROACH
Fractional Knapsack Problem
Job Sequencing with Deadlines Problem
Let d[i] ≥ 1 be the deadline of job i where 1 ≤ i ≤ n, n ≥ 1.
Let p[1] ≥ p[2] ≥ … ≥ p[n].
Set J denotes the optimal solution where J[i] denotes each job.
In the resultant optimal solution, d[J[i]] ≤ d[J[i+1]].
Job_Sequence(d,J,n)
for i = 1 to n
J[i] ← EMPTY
for i = 1 to n
k← d[i]
while(k ≥ 1)
if(J[k] = EMPTY)
then J[k] ← i
break;
k=k-1
Job Sequencing with Deadlines Problem
ANOTHER
Let d[i] ≥ 1 be the deadline of job i where 1 ≤ i ≤ n, n ≥ 1. VERSION
Let p[1] ≥ p[2] ≥ … ≥ p[n].
Set J denotes the optimal solution where J[i] denotes each job.
In the resultant optimal solution, d[J[i]] ≤ d[J[i+1]].
Job_Sequence(d,J,n)
J = {1}
for i = 2 to n
if (Feasible(J U {i}) = TRUE)
then J = J U {i}
Feasible(J)
J’ = sort(J) // according to non-decreasing deadline
if J’ is feasible solution
then return TRUE
Activity Selection Problem
Rec_Act_Select(s, f, k, n)
m←k+1
while(m ≤ n and s[m] < f[k]) RECURSIVE
m←m+1 APPROACH
if(m ≤ n)
return({am} U Rec_Act_Select(s, f, m, n))
Else
return Φ
Iter_Act_Select(s, f, k, n)
k←1
A ← {ak}
ITERATIVE
for(m = 2 to n) APPROACH
if(s[m] ≥ f[k])
A ← A U {am}
k←m
return A
DIVIDE-AND-CONQUER
Binary Search
𝒉𝒊𝒈𝒉+ 𝒍𝒐𝒘
𝟐
Mergesort
Quicksort
Heapsort
BACKTRACKING
Graph Coloring Problem
Graph_Color(i) // i is index of next vertex to be colored
// Graph G is assigned to adjacency matrix G[1..n:1..n].
// Let x[i] be color possible to be assigned to vertex i.
do
{
Next_Color(i) //Assign x[i] a legal color
if(x[i] = 0)
return // No new color possible
if(i = n) // At most m colors used to color n vertices
print x[1..n]
return
else
Graph_Color(i+1)
}while(TRUE)
Graph Coloring Problem
Next_Color(i) // Determines the possible colors i can take
// Assume x[1]…x[i-1] have been assigned integer values in the range [1,m]
// following the conditions of graph coloring.
// x[i] is assigned a value in [0,m] that is the next highest number color after
the // colors used.
// If all colors are exhausted, x[i] = 0.
do
{
x[i] = (x[i] + 1)mod(m + 1) // Next highest color
if(x[i] = 0)
return // All colors have been used
for(j = 1 to n)
{
if((G[i,j] ≠ 0) and (x[i] = x[j]))
break // Check vertex i with all other vertices
If there is an edge and the colors at both
end
vertices are same then change color
}
if(j = n + 1)
return // New color found, for loop exhausted
N-Queens Problem
Example of a backtrack solution to 4-Queens Problem
N-Queens Problem
Procedure Place(k,i)
N-Queens Problem
Thank you