m4 Aad
m4 Aad
• Dynamic Programming
o Dynamic programming is an algorithm design method that can be used when the solution to a
problem can be viewed as the result of a sequence of decisions.
o Dynamic Programming is mainly an optimization over plain recursion.
o Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize
it using Dynamic Programming.
o The idea is to simply store the results of subproblems, so that we do not have to re-compute
them when needed later.
o This simple optimization reduces time complexities from exponential to polynomial.
o For example, if we write simple recursive solution for Fibonacci Numbers, we get exponential
time complexity and if we optimize it by storing solutions of subproblems, time complexity
reduces to linear.
o In dynamic programming an optimal sequence of decisions is obtained by making explicit
appeal to the principle of optimality.
1
2. Optimal Substructure
• A given problem has Optimal Substructure Property, if the optimal solution of the given
problem can be obtained using optimal solutions of its sub-problems.
• For example, the Shortest Path problem has the following optimal substructure
property: If a node x lies in the shortest path from a source node u to destination
node v, then the shortest path from u to v is the combination of the shortest path
from u to x, and the shortest path from x to v.
▪ We can multiply two matrices A and B if and only if they are compatible: The number of
columns of A must be equal to the number of rows of B.
2
Example:
Consider 3 matrices A1, A2 and A3. Its dimensions are 10X100, 100X5, 5X50 respectively.
Compute the product (A1 A2 A3).
Thus, computing the product according to the first parenthesization is 10 times faster.
………………………………………………………………………………………………
• Given a chain (A1, A2, . . . An ) of n matrices, where for i = 1, 2, . . . n, matrix Ai has dimension
pi-1 X pi , fully parenthesize the product A1A2. . . An in a way that minimizes the number of scalar
multiplications
• In the matrix-chain multiplication problem, we are not actually multiplying matrices.
• Our goal is only to determine an order for multiplying matrices that has the lowest cost
Algorithm Matrix_Chain_Order(p)
p- size of matrix
{ n = p.length – 1
Let m[1..n, 1..n] and s[1..n-1 , 2..n] be new tables n-total no. of matrices
for i=1 to n do m[ ]-min no. of scalar multiplications
m[i,i] = 0 //diagonal elements
for x=2 to n do s[ ] - from where to split
{ for i=1 to n-x+1 do
{ j=i+x-1
m[i,j] = α x=2 means it is 2 matrix combination
for k=i to j-1 do x=2, i=1, j=2
x- matrix combination x=2, i=2, j=3
(i,j) - array index { q = m[i,k] + m[k+1,j] + pi-1 pk pj
k- split point if q<m[i,j] then x=3, i=1, j=3
{ m[i,j] = q x=3, i=2, j=4
p - dimensions - scalar s[i,j] = k x=4, i=1, j=4
} x=4, i=2, j=5
}
}
}
return m[][] and s[][]
}
4
• Step 4: Constructing an optimal solution
Algorithm Print_Optimal_Parens(s,i,j)
{
if i==j then
print “A”i
else
print “(“
print_Optimal_Parens(s,i,s[i,j]) //recursive call at split point
print_Optimal_Parens(s,s[i,j]+1,j)
print “)”
}
4x4 matrix
take 2 combination
m(2,3)=48
A3 x A4 take 2 combination
cut at 3, [A3xA4]
6x2x7=84 therefore place 3 in s(3,4)
m(3,4)=84
cut at A1 cut at A2
(scalar) min
cut at 3 in s(2,4)
cut at 2 cut at 3
140+144 120+84+210
5x4x7
scalar 5x6x7=210
cut at 3 in s(1,4)
optimal
5x2x7= 88+70
7
All Pairs Shortest Path Algorithm
The path originates from vertex i and goes through some intermediate vertices and terminates
at vertex j. A(i,j) = cost(i,j) and we can obtain a recurrence for Ak (i,j) as,
Ak (i,j) = min {(Ak-1 (i,j), Ak-1 (i,k) + Ak-1 (k,j)}, k>=1
• Find the shortest distances between every pair of vertices in a given weighted directed
Graph
• The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem.
Negative edge weights are also allowed.
• As a result of this algorithm, it will generate a matrix, which will represent the minimum
distance from any node to all other nodes in the graph.
Time Complexity
• Floyd Warshall Algorithm consists of three loops over all the nodes. Each loop has
constant complexities.
• Hence, the time complexity of Floyd Warshall algorithm = O(n3), where n is the number
of nodes in the given graph.
1 2 3 4 D(i,i)=0 //diagonal
1
2 D(i,j)=length/cost of edge (i,j)
k=0
3 D(i,j)=infinity //if no edge
4
k=1
k=2
k=3
k=4
Example
• Back Tracking
o Backtracking method expressed the solution as n-tuple (x1, x2.,…… xn), where xi‟s are chosen
from some finite set Si.
o The problem to be solved calls for finding one vector that maximizes (or minimizes or
satisfies) a criterion function P(x1, x2.,…… xn).
o Examples: Sorting the array of integers in a[l: n]
▪ The solution to the problem is expressed an n-tuple, where xi is the index of the ith
smallest element.
▪ The criterion function P is: a[xi] ≤ a[xi+1], for 1≤ i < n.
▪ The set Si ={1,2, ......... n}
▪ Different methods for solving this problem
• Brute Force approach
o Suppose mi is the size of set Si.
o The number of tuples (with size n) that are possible candidates for satisfying the
function P is: m = m1 x m2 x m3...................x mn
o Brute Force approach evaluates each one with P, and save those which yield the
optimum.
• Backtracking algorithm
o It yields the same answer with far fewer than m trials.
o Its basic idea is to build up the solution vector one component at a time and to use
modified criterion functions (bounding functions) Pi(x1, x2.,…… xi ) to test
whether the vector being formed has any chance of success.
o The major advantage of this method is that if it is realized that the partial vector
(x1, x2.,…… xi) can in no way lead to an optimal solution, then mi+1 x mi+2. . . . .
. x mn possible test vectors can be ignored entirely.
o Backtracking method require that all the solutions satisfy a complex set of constraints. These
constraints can be divided into two categories:
▪ Explicit Constraints
• Explicit constraints are rules that restrict each xi to take on values only from a given
set
• The explicit constraints depend on the particular instance I of the problem being
solved. All tuples that satisfy the explicit constraints define a possible solution space
for I. explicit - values that are possible for xi
• Example:
▪ Implicit Constraints
• These are rules that determine which of the tuples in the solution space of I satisfy the
criterion function (Bounding Function). implicit - the answers in solution space
o N-Queens Problem
▪ n queens are to be placed on a n x n chessboard so that no two attack. That is, no two
queens are on the same row, column, or diagonal.
▪ Number the rows and columns of the chessboard 1through n.
▪ The queens can also be numbered 1through n.
1
▪ Since each queen must be on a different row, we can assume that queen i is to be placed
on row i.
▪ All solutions to the n-queens problem can therefore be represented as n-tuples(x1,
x2.,…… xn), where xi is the column on which queen i is placed.
▪ Explicit constraint: Si = {1,2,3, ......... n }, 1≤ i ≤n size of s is n n
• The solution space contains |S1| x |S2| x. . . . . . . x |Sn| = n tuples.
n therefore, nxnxn...= n
▪ Implicit constraints:
• No two xi‟s can be the same(i.e. all queens must be on different columns)
o The solution space contains |S1| x |S2| x . . . . . . . x |Sn| = n(n-1)........... 1 = n! tuples
o It reduces the solution space from nn to n!. first Q is placed in n options
• No two queens can be on the same diagonal. second Q can be placed in (n-1) options
etc upto 1, therefore n!
4 Queen’s solution
(2,4,1,3) (3,1,4,2)
1 2 3 4
1 2 3 4
1 Q1 1 Q1
2 Q2 2 Q2
3 Q3
3 Q3
4 Q4
4 Q4
▪ Following is a tree organization (permutation tree/State Space Tree) for 4-queen
problem without considering diagonal collision.
first Q(x1) has 4 options,
1/2/3/4th column
(2,4,1,3) (3,1,4,2)
Fig: Tree organization of the 4-queen’s solution space. Nodes are numbered in depth first search
• The edges are labeled by possible values of xi.
• Edges from level 1 to level 2 nodes specify the values for x1
• Edges from level i to level i +1 are labeled with the values of xi
• Thus, the leftmost sub-tree contains all solutions with x1 = 1; its leftmost sub-tree
contains all solutions with x1 = 1 and x2 = 2, and soon.
• The solution space is defined by all paths from the root node to a leaf node. There are
n! = 4!= 24 leaf nodes in the above tree.
▪ Breadth First Generation Method: Each new node is placed into a queue.
When all the children of the current-E-node have been generated, the next
node at the front of the queue becomes the new E-node.
• D-search(depth search): Each new node is placed into a stack. When all the children of
the current-E-node have been generated, the next node at the top of the stack becomes the
new E-node.
▪ At the conclusion of the process at least one answer node is always generated or all
answer nodes are generated if the problem requires us to find all solutions.
3
▪ Live Node: A node which has been generated and all of whose children have not yet been
generated is called a live node.
▪ E-node : The live node whose children are currently being generated is called the E-node
▪ Dead node: It is a generated node which is not to be expanded further or all of whose
children have been generated.
▪ Bounding functions are used to kill live nodes without generating all their children.
▪ If (x1, x2.,…… xi) is the path to the current E-node, then all children nodes with parent-
child labeling xi+1 are such that (x1, x2.,…… xi+1) represents a chessboard configuration in
which no two queens are attacking.
▪ We start with the root node as the only live node. This becomes the E-node and the path is
(). We generate one child(node 2) and the path is now (1). This corresponds to placing
queen1on column1.
▪ Node2 becomes the E-node. Node3 is generated and immediately killed.
▪ The next node generated is node8 and the path becomes(1,3). Node8 becomes the E-node.
However, it gets killed as all its children represent board configurations that cannot lead
to an answer node.
▪ We back track to node2 and generate another child node13.The path is now (1,4). This
process continues until it will generate a proper arrangement.
4
Backtracking - N Queens Problem
5
o Backtracking Control Abstraction
▪ (x1, x2.,…… xi) be a path from the root to a node in a state space tree.
▪ Generating Function T(x1, x2.,…… xi) be the set of all possible values for xi+1 such that
(x1, x2.,…… xi+1) is also a path to a problem state.
▪ T(x1, x2.,…… xn) = φ
▪ Bounding function Bi+1(x1, x2.,…… xi+1) is false for a path (x1, x2.,…… xi+1) from the
root node to a problem state, then the path cannot be extended to reach an answer node.
▪ Thus the candidates for position i+1of the solution vector (x1, x2.,…… xn) are those
values which are generated by T and satisfy Bi+1.
▪ The recursive version is initially invoked by Backtrack(1).
o All the possible elements for the kth position of the tuple that satisfy Bk are generated one by
one, and adjoined to the current vector (x1, x2.,…… xk-1 ).
o Each time xk is attached, a check is made to determine whether a solution has been found.
Then the algorithm is recursively invoked.
o When the for loop is exited, no more values for xk exist and the current copy of Backtrack
ends. The last unresolved call now resumes.
o This algorithm causes all solutions to be printed. If only a single solution is desired, then a flag
can be added as a parameter to indicate the first occurrence of success.
• N-Queens Problem(Cont…)
o Consider an n x n chessboard and try to find all possible way to place n non-attacking
queens.
o (x1, x2.,…… xn ) be the solution vector. Queen i is placed in ith row and x ith column. xi will
all be distinct since no two queens can be placed in the same column.
o There are 2 type of diagonals
▪ Positive Diagonal
• Diagonal from upper left to lower right
• Every element on the same diagonal has the same row-column value
• Suppose 2 queens are place at position (i,j) and (k,l), then i-j = k-l
▪ Negative Diagonal
• Diagonal from upper right to lower left
• Every element on the same diagonal has the same row+column value
• Suppose 2 queens are place at position (i,j) and (k,l), then i+j = k+l
▪ The 1st equation implies: i-k = j-l
▪ The 2 equation implies:
nd
i-k = l - j
6
▪ Combining these two, we will get : | i-k | = | j-l | // diagonal attack condition
Absolute value of column difference is equal to the absolute value of row difference.
Algorithm NQueens(k,n)
{ k - queen
for i=1 to n do n - columns
{ if Place(k,i) then
Place(k,i) means Qk in column i
{ x[k] = i
if(k==n) then write(x[1:n])
else NQueens(k+1, n)
}
}
}
Algorithm Place(k,i)
{
for j=1 to k-1 do
{
if( (x[j]==i) or ( Abs(j-k)==Abs(x[j]-i) ) then // check column and diagonal attack
Return false
}
Return true
}
7
• Branch and Bound Technique
o During state space tree generation E-node remains E-node until it is dead.
o Two strategies:
▪ Breadth First Search(BFS)
• It is called FIFO(First In First Out). Here the live nodes are placed in a
queue.
▪ Depth Search(D-Search)
• It is called LIFO(Last In First Out). Here the live nodes are placed in a
stack.
o Least Cost Search(LC Search)
▪ To improve the searching speed, we can use a ranking function ĉ(.) for live
nodes.
▪ ĉ(.) value of each live node is calculated. The next E-node is the live node with
least ĉ(.). Such a search strategy is called LC Search.
▪ BFS and D-Search are the special cases of LC-Search.
▪ LC-Search coupled with bounding function is called LC Branch and Bound
Search.
o LC-Search Control Abstraction
Algorithm LCSearch(t) t -node
{ if t is an answer node then output t and return
E = t // set t as E node
Initialize the list of live nodes to be empty
repeat x-> child
{ for each child x of E do
{
if x is an answer node then output the path from x to t and return
Add(x) // if x is not answer node, add it to live node list
x → parent = E
}
if there are no more live nodes then
{ Write “no answer node”
return
}
E = Least() //finds the node with min cost and set it as next E
}until(false)
}
• Least() finds a live node with least ĉ. This node is deleted from the list of live nodes and returned.
▪ Add(x) adds the new live node x to the list of live nodes.
▪ LCSearch outputs the path from the answer node to the root t.
▪ LCSearch terminates only when either an answer node is found or the entire
state space tree has been generated and searched.
▪ The control abstraction for LC, FIFO and LIFO are same. The only difference is
the implementation of the list of live nodes.
• FIFO Search scheme:
o The list of live nodes is implemented as queue.
o Least() and Add(x) being algorithms to delete an element from
and add an element to the queue.
• LIFO Search scheme:
8
o The list of live nodes is implemented as stack.
o Least() and Add(x) being algorithms to delete an element from
and add an element to the stack.
• LC-Search Scheme:
o Add(x) is an algorithm to add elements to the list. Least() returns
a live node with least ĉ(.) from the list.
• Solution
• The adjacency matrix is
no edge = infinity
r =1+4
Cost reduced = r = 5
M2 is the matrix for node 2 parent just parent node
Cost of node 2 = Cost of node 1 + M1[a, b] + r = 6 + 0 + 5 = 11
• Find the matrix and cost of node 3
o Set row a and column c elements are α
o Set M1[c, a]= α //path to root node a 3 is generated from 1
o The resultant matrix is so take matrix of 1 ie; M1
a to c is the path
so set row a and column c as infinity
M1[c,a]=infinity
Cost reduced = r = 2
M3 is the matrix for node 3parent just parent node
Cost of node 3 = Cost of node 1 + M1[a, c] + r = 6 + 3 + 2 = 11
• Find the matrix and cost of node 4
o Set row a and column d elements are α
o Set M1[d, a]= α //path to root node a
o The resultant matrix is
10
Cost reduced = r = 6
M4 is the matrix for node 4 just parent node
parent
Cost of node 4 = Cost of node 1 + M1[a, d] + r = 6 + 5 + 6 = 17
Now the live nodes are 2, 3 and 4. Minimum cost is for node 2 and 3. Choose
one node(say node 2) as the next E-node.
o Generate the child node of node 2
find cost and matrix of 5,6
From a path to b,
From b , generate child nodes c,d ie; 5,6
Cost reduced = r = 2
M5 is the matrix for node 5parent just parent
Cost of node 5 = Cost of node 2 + M21[b, c] + r = 11 + 5 + 2 = 18
• Find the matrix and cost of node 6
o Set row b and column d elements are α
o Set M2[d, a]= α //path to root node a
11
o The resultant matrix is
Cost reduced = r = 0
M6 is the matrix for node 6parent just parent
Cost of node 6 = Cost of node 2 + M21[b, d] + r = 11 + 0 + 0 = 11
Now the live nodes are 5, 6, 3 and 4. Choose one node which having minimum
cost(say node 6) as the next E-node.
o Generate the child node of node 6
a -> b -> d
therefore d's child is c
Cost reduced = r = 0
M7 is the matrix for node 7 just parent
Cost of node 7 = Cost of node 6 + M6[d, c] + r = 11 + 0 + 0 = 11
Now the live nodes are 5, 7, 3 and 4. Choose one node which having minimum
cost(say node 7) as the next E-node. Node 7 having no child node. because a - b - d - c
Now we can say that node 1 to node 7 path is the Traveling salesperson path.
13