18CS42 - Module 4
18CS42 - Module 4
18CS42 - Module 4
ALGORITHMS
MODULE 4
Topics
Dynamic Programming:
Multistage Graphs.
Knapsack problem,
It was invented by U.S. mathematician, Richard Bellman, in the 1950s. It is a general method for
The word “programming” in the name of this technique stands for “planning” and does not refer to
computer programming.
Dynamic programming is a technique for solving problems with overlapping sub-problems. These sub-
problems arise from a recurrence relating a given problem’s solution to solutions of its smaller sub-
problems.
Introduction to Dynamic Programming
Dynamic programming suggests, rather than solving overlapping sub-problems again and again, each of
the smaller sub-problems are solved only once and recording the results in a table from which a solution
to the original problem can then be obtained.
Ex: Fibonacci numbers. The Fibonacci numbers are the elements of the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
...,
F(0) = 0, F(1) = 1.
Introduction to Dynamic Programming
Dynamic programming is an algorithm design method that can be used when the solution to a problem can
That is we have to decide the values of Xi, where 1 <= i <= n. First we make a decision on X1, then on
X2, then on X3, and soon. An optimal sequence of decisions maximizes the objective function
Ex: 2. To find shortest path from vertex i to vertex j in a directed graph G. We have to decide which
vertex should be the second vertex, which should be the third, which should be the fourth, and soon,
until vertex j is reached. An optimal sequence of decisions is the path of least length.
Introduction to Dynamic Programming
Dynamic programming is an algorithm design method that can be used when the solution to a problem can
That is we have to decide the values of Xi, where 1 <= i <= n. First we make a decision on X1, then on
X2, then on X3, and soon. An optimal sequence of decisions maximizes the objective function
Ex: 2. To find shortest path from vertex i to vertex j in a directed graph G. We have to decide which
vertex should be the second vertex, which should be the third, which should be the fourth, and soon,
until vertex j is reached. An optimal sequence of decisions is the path of least length.
Greedy Technique Dynamic Programming
It involves making a series of choices without It involves solving problems by combining the
considering the overall future consequences. The solutions of sub-problems, rather than solving each
algorithm makes the best choice at each step based sub-problem independently.
on the current information available.
Greedy algorithms usually work well for problems Dynamic programming ensures that each sub-problem
where a locally optimal choice leads to a globally is solved only once, avoiding redundant computations
optimal solution. and significantly reducing the time complexity of the
overall solution.
Greedy approach does not always guarantee an Dynamic programming guarantees an optimal solution
optimal solution for all problems, and in some for problems that can be solved using this technique.
cases, it may lead to suboptimal results or
incorrect solutions.
DYNAMIC PROGRAMMING
Steps:
Re use the results so that same problems is not calculated more than once.
The sets V1 and Vk are such that | V1|= |Vk|= 1. Let s and t, respectively, be the vertices in V1 and Vk . The
vertex s is the source, and t is the sink.
Let c (i, j) be the cost of edge (i, j). The cost of a path from s to t is the sum of the costs of the edges on
the path. The multi stage graph problem is to find a minimum-cost path from s to t.
MULTISTAGE GRAPHS
FORWARD APPROACH
Let cost (i, j) be the cost of the path. Then, using the forward approach,
where cost (i, j) is the cost of path from vertex j to the sink node and i is the stage number of vertex j.
Let bcost (i, j) be the cost of the path. Then, using the forward approach,
where bcost (i-1, j) is the cost of path from (i-1)th stage from vertex j to the source vertex.
Transitive closure of a directed graph: Transitive Closure is the reachability matrix to reach from vertex u to vertex
v of a graph. Given a graph, find a vertex v is reachable from another vertex u, for all vertex pairs (u, v). The final
matrix is the Boolean type.
Ex,
Transitive Closure
DEFINITION
The transitive closure of a directed graph with n vertices can be defined as the n × n boolean matrix T =
{ tij }, in which the element in the ith row and the jth column is 1 if there exists a nontrivial path (i.e., directed
path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0.
Warshall’s and Floyd’s Algorithms
Warshall’s algorithm for computing the transitive closure of a directed graph. It is called Warshall’s
Warshall’s algorithm constructs the transitive closure through a series of n × n boolean matrices:
R(1) contains the information about paths that can use the first vertex as intermediate and soon
The last matrix in the series, R(n) , reflects paths that can use all n vertices of the digraph as intermediate and is
immediate predecessor R(k-1) in series. Let r(k)ij , the element in the ith row and jth column of matrix R(k), be
equal to 1.
Warshall’s and Floyd’s Algorithms
The following formula is used for generating the elements of matrix R(k) from the elements of matrix R(k-1) :
This formula implies the following rule for generating elements of matrix R(k) from elements of matrix R(k-1),
If an element rij is 0 in R(k-1), it has to be changed to 1 in R(k) if and only if the element in its row i and column k and
the element in its column j and row k are both 1’s in R(k-1).
Application of Warshall’s Algorithms
Warshall’s Algorithms
Warshall’s Algorithms
Floyd’s Algorithms
Floyd’s Algorithm for the All-Pairs Shortest-Paths Problem
Given a weighted connected graph (undirected or directed), the all-pairs shortest paths problem finds the
distances—i.e., the lengths of the shortest paths— from each vertex to all other vertices.
It is called Floyd’s algorithm after its co-inventor Robert W. Floyd. It is applicable to both undirected and
directed weighted graphs provided that they do not contain a cycle of a negative length. (The distance
between any two vertices in such a cycle can be made small by repeating the cycle enough times).
Floyd’s Algorithms
Floyd’s Algorithm for the All-Pairs Shortest-Paths Problem
Its important applications to communications, transportation networks. Recent applications of the all-pairs
The lengths of shortest paths in an n × n matrix is recorded in matrix D called the distance matrix: the
element dij in the ith row and the jth column of this matrix indicates the length of the shortest path from the ith
vertex to the jth vertex.
Floyd’s Algorithms
Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through a series of n
× n matrices:
The element d(k) ij in the ith row and the jth column of matrix D(k) (i, j = 1, 2, . . . , n, k = 0, 1, . . . , n) is equal to the
length of the shortest path among all paths from the ith vertex to the jth vertex with each intermediate vertex, if any
numbered not higher than k.
D(0), does not allow any intermediate vertices in its paths; hence, D(0) is simply the weight matrix of the graph.
The last matrix in the series, D(n), contains the lengths of the shortest paths among all paths that use all n vertices as
intermediate and hence is the distance matrix.
Floyd’s Algorithms
As in Warshall’s algorithm, we can compute all the elements of each matrix D(k) from its immediate
predecessor D(k-1).
The d(k) ij be the element in the ith row and the ijh column of matrix D(k). Each of the paths is made up two
disjoint subsets:
those that do not use the kth vertex vk as intermediate vertex (i.e) This means that d(k) ij is equal to the length of the
shortest path among all paths from the ith vertex vi to the jth vertex vj and
those that do (i.e., paths is made up of a path from vi to vk with each intermediate vertex numbered not higher than k −
1 and a path from vk to vj with each intermediate vertex numbered not higher than k − 1).
Floyd’s Algorithms
Taking into account the lengths of the shortest paths in both subsets leads to the following recurrence:
Application of Floyd’s Algorithms
Floyd’s Algorithms
1.
2.
Knapsack Problem and Memory Functions
Knapsack problem using Dynamic Programming
M – Capacity of Knapsack
n – Number of objects
relation that expresses a solution to an instance of the knapsack problem in terms of solutions to its smaller
sub-instances.
Let us consider an instance defined by the first i items, 1 ≤ i ≤ n,
knapsack capacity j, 1 ≤ j ≤ W.
Let F(i, j) be the value of an optimal solution to this instance, i.e., the value of the most valuable subset of the first i
Divide all the subsets of the first i items that fit the knapsack of capacity j into two categories:
1. Among the subsets that do not include the ith item ( wi > j ), the value of an optimal subset is, by definition, F
( i, j) = F ( i − 1, j).
2. Among the subsets that do include the ith item (hence, j − wi ≥ 0 or wi < j ), an optimal subset is made up of this
item and an optimal subset of the first i − 1 item that fits into the knapsack of capacity j − wi .
Goal is to find F(n, W), the maximal value of a subset of the n given items
Knapsack Problem
Knapsack Problem
The maximal value is F(4, 5) = $37.
Find the composition of an optimal subset by back-tracing the computations of this entry in the table.
Since F(4, 5) > F(3, 5), item 4 has to be included in an optimal solution {5 − 2 = 3 remaining units of the
knapsack capacity }.
The value of the latter is F(3, 3). Since F(3, 3) = F(2, 3), item 3 not in an optimal subset.
Since F(2, 3) > F(1, 3), item 2 is a part of an optimal selection, { 3 − 1 = 2 remaining units of the knapsack
capacity }.
Since F(1, 2) > F(0, 2), item 1 is the final part of the optimal solution, { 2 − 2 = 0 remaining units of the
knapsack capacity }.
The goal is to get a method that solves only sub-problems that are necessary and does so only once. Such
Using Memory Functions method, it fills a table with solutions to all smaller sub-problems, but each of
been calculated.
the method checks the corresponding entry in the table first: if this entry is not “null,” it is simply retrieved from
the table;
otherwise, it is computed by the recursive call whose result is then recorded in the table.
Memory Functions
The following algorithm implements this idea for the knapsack problem. After initializing the table, the recursive function
needs to be called with i = n (the number of items) and j = W (the knapsack capacity).
Memory Functions
Note: The table in Figure 8.6 gives the results. Only 11 out of 20 nontrivial values (i.e., not those in row 0 or in column
postal van to pick up mail from boxes located at n different sites. An n + 1 vertex graph can be used to
represent the situation. One vertex represents the post office from which the postal van starts and to which it
must return. Edge(i,j) is assigned a cost equal to the distance from site i to site j. The route taken by the
postal van is a tour, and we are interested in finding a tour of minimum length.
Let G = (V,E) be a directed graph with edge costs c ij. The variable cij is defined such that
cij = ∞ if (i,j) ∉ E.
Let V = n and n > 1. A tour of G is a directed simple cycle that includes every vertex in V.
The cost of a tour is the sum of the cost of the edges on the tour. The traveling sales person problem is to
terminating at vertex 1.The function g (l, V - {1}) is the length of an optimal sales person tour. From the
principal of optimality it follows that
Generalizing
THE TRAVELING SALES PERSON PROBLEM
THE TRAVELING SALES PERSON PROBLEM
THE TRAVELING SALES PERSON PROBLEM
OPTIMAL BINARY SEARCH TREES
A binary search tree is one of the most important data structures in computer science. One of its principal
applications is to implement a dictionary, a set of elements with the operations of searching, insertion, and
deletion.
Given 3 keys, 10, 20, and 30 the possible binary trees are,
The total number of binary search trees with n keys is equal to the nth Catalan number,
OPTIMAL BINARY SEARCH TREES
Example 1, Given four keys A, B, C, and D with probabilities 0.1, 0.2, 0.4, and 0.3, respectively. What is the
average number of comparisons to search a key for optimal binary search tree.
In the first of these trees is 0.1 * 1+ 0.2 *2 + 0.4 * 3+ 0.3 *4 = 2.9, and
probabilities of searching for them. Let C(i, j) be the smallest average number of comparisons made in a
successful search in a binary search tree Ti j made up of keys ai, . . . , aj , where i, j are some integer indices, 1
≤ i ≤ j ≤ n.
Using dynamic programming approach find values of C(i, j) for all smaller instances of the problem, and get
Using dynamic programming we derive a recurrence relation considering all possible ways to choose a root a
the left sub-tree Tik - 1 contains keys a i, . . . , a k-1 optimally arranged, and
probabilities of searching for them. Let C(i, j) be the smallest average number of comparisons made in a
successful search in a binary search tree Ti j made up of keys ai, . . . , aj , where i, j are some integer indices, 1
≤ i ≤ j ≤ n.
Using dynamic programming approach find values of C(i, j) for all smaller instances of the problem, and get
Using dynamic programming we derive a recurrence relation considering all possible ways to choose a root a
the left sub-tree Tik - 1 contains keys a i, . . . , a k-1 optimally arranged, and
the left sub-tree Tik - 1 contains keys a i, . . . , a k-1 optimally arranged, and
The algorithm’s space efficiency is quadratic; the time efficiency of this algorithm is cubic.
OPTIMAL BINARY SEARCH TREES
Construct an optimal Binary search tree for the following four-key set.
a)
b)
Input Enhancement in String Matching: Horspool’s Algorithm
Brute-Force String Matching
Input Enhancement in String Matching: Horspool’s Algorithm
Brute-Force String Matching
Input Enhancement in String Matching: Horspool’s Algorithm
Input Enhancement in String Matching: Horspool’s Algorithm
There several faster algorithms,
Knuth-Morris-Pratt algorithm
Boyer-Moore algorithm
Horspool’s algorithm
These algorithms exploit the idea of input-enhancement: preprocess the pattern to get some information
about it, store this information in a table, and then use this information during an actual search for the
pattern in a given text.
Knuth- Morris-Pratt algorithm does it left to right, whereas the Boyer-Moore algorithm does it right to
left.
Horspool’s Algorithm
Consider, as an example, searching for the pattern BARBER in some text:
Compare the corresponding pairs of characters in the pattern and the text starting with the last R of the
If a mismatch occurs, shift the pattern to the right, make as large a shift as possible without risking the possibility of
Horspool’s algorithm determines the size of such a shift by looking at the character c of the text that is
Case 2 If there are occurrences of character c in the pattern but it is not the last one there — Ex, c is letter B —
the shift should align the rightmost occurrence of c in the pattern with the c in the text:
Horspool’s Algorithm
There are four possibilities can occur,
Case 3 If c happens to be the last character in the pattern but there are no c’s among its other m − 1
characters— Ex, c is letter R—the pattern should be shifted by the entire pattern’s length m:
Case 4 Finally, if c happens to be the last character in the pattern and there are other c’s among its first m − 1
characters— Ex, c is letter R in our example—the rightmost occurrence of c among the first m − 1 characters in
the pattern should be aligned with the text’s c:
Horspool’s Algorithm
The table’s entries will indicate the shift sizes computed by the formula
Horspool’s Algorithm
Horspool’s Algorithm
Horspool’s Algorithm