Module 04
Module 04
Module 04
Hanumanthu
Dep. Of CSE
http://searchcreators.org/
MODULE-04
Dynamic Programming
Chapter-I
1. Multistage Graph
2. 0/1 Knapsack Problem
3. All Pairs shortest path algorithm
4. Traveling Salesman Problem
5. Reliability Design
6. Optimal Binary Search Trees
Let us solve multistage problem for both the approaches with the help of some
example.
There is single vertex in stage 1, then 3 vertices in stage 2, then 2 vertices in stage
and only one vertex in stage 4 (this is a target stage).
Backward approach d (S, T) = min {1+ d (A, T), 2 + d (B, T), 7 + d (C, T)} …. (1)
d (D, T) = 8
d (A, T) = min {3 + 8, 6 + 2}
d (A, T) = 8 A - E -T
d (B, T) = min {4 + 8, 10 + 2}
d (B, T) = 12 A - D -T
d (C, T) = 5 C-E–T
d (S, T) = 9 S-A-E-T
Search Creators... Page 4
BCS401 | Design and Analysis of Algorithm | SEARCH CREATORS.
Chapter-II
1. Digraph: The graph in which all the edges are directed then it is called digraph
or directed graph.
Example:
Example:
Example:
• Transitive closure the transitive closure can be generated with Depth First
Search (DFS) or with Breadth First Search (BFS).
• This traversing can be done any vertex. While computing transitive closure
we have to start with some vertex and have to find all the edges which are
reachable to every other vertex.
• The reachable edges for all the vertices has to be obtained.
Basic Concept
Procedure to be followed:
R (0) ← A
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
R (k) [i, j] ← R (k−1) [i, j] or (R (k−1) [i, k] and R (k−1) [k, j])
return R (n)
Fig: (a) Digraph. (b) Its weight matrix. (c) Its distance matrix.
Chapter-III
• Floyd's algorithm is for finding the shortest path between every pair of
vertices of a graph.
• This algorithm is invented by R. Floyd and hence is the name.
• Before getting introduced with this algorithm, let us revise few concepts
related with graph.
• The Floyd's algorithm is for computing shortest path between every pair of
vertices of graph.
• The graph may contain negative edges but it should not contain negative
cycles.
• The Floyd's algorithm requires a weighted graph.
• Floyd's algorithm computes the distance matrix of a weighted graph with n
vertices through a series of n-by-n matrices:
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
return D
The knapsack problem can be defined as follows : If there are n items with the
weights w 1, w 2, ….w n and values (profit associated with each item) v1, v2, …..vn
and capacity of knapsack to be W, then find the most valuable subset of the items
that fit into the knapsack.
To solve this problem using dynamic programming we will write the recurrence
relation as :
or
table [i- 1, j] if j < wi
The table [n, W] is a goal i.e., it’s gives the total items sum of all the selected items
for the knapsack.
Example: For the given instance of problem obtain the optimal solution for the
knapsack problem.
Solution: Initially, table [0, j] = 0 and table [i,0] = 0. There are 0 to n rows and 0 to
W columns in the table.
Now we will fill up the table either row by row or column by column.
Let us start filling the table row by row using following formula:
Algorithm
//Problem Description: This algorithm is for obtaining knapsack solution using dynamic
programming.
//Input: n is total number of items, W is the capacity of knapsack, w[ ] stores weights of each
item and v[ ] stores the values of each item.
//Output: Returns the total value of selected items for the knapsack.
for(i0 to n) do
for(j0 to W) do
table[i,0]=0
table[0,j]=0
}for(i0 to n) do
for(j0 to W) do
if (j<w[i]) then
return table[n,W].
The Bellman-Ford algorithm works for finding single source shortest path. works
for negative edge weights.
Example: Consider following for which the shortest path can be obtained from
source vertex v1.
Algorithm
//Problem Description : This algorithm finds the shortest path using Bellman Ford
method
if (v is source) then
v distance k 0
else
v. distance infinity
v. predeNull
U uv. source
V uv. desination
v. prede u
Search Creators... Page 24
BCS401 | Design and Analysis of Algorithm | SEARCH CREATORS.
U uv. source
v uv. destination
return False
Problem Description
• Let G directed graph denoted by (V, E) where V denotes set of vertices 17: =
denotes set of edges.
• The edges are given along with their cost Cij .
• The cost Cij >0 for all i and j. If there is no edge between i and j then Cij =
∞.
• A tour for the graph should be such that all the vertices should be visited
only and cost of the tour is sum of cost of edges on the tour.
• The traveling salesperson problem is to find the tour of minimum cost.
Step 1: Let the function C (1, V- {1}) is the total length of the tour terminating
at 1 the objective of TSP problem is that the cost of this tour should be
minimum.
Step 2: Let V1, V2...Vn, be the sequence of vertices followed in optimal tour.
Then (V1, V2...Vn) must be a shortest path from V1 to Vn., which passes through
each vertex exactly once.
Step 3: Following formula can be used to obtain the optimum cost tour.
Fig
That means eve have obtained dist (2, 1), dist (3, 1) and dist (4, 1).
Step 2:
Candidate (S) = 1
=9+6
= 10+8
=3+5
= 12+8
Search Creators... Page 28
BCS401 | Design and Analysis of Algorithm | SEARCH CREATORS.
=9 + 6
Cost (2, {3, 4}, 1) = min {[d(2, 3) + Cost (3, {4}, 1)], (d(2, 4) + Cost (4, {3}, 1)]}
Cost (3, (2, 4), 1) = min {[d(3, 2) + Cost (2, {4}, 1)], [d(3, 4) + Cost (4, {2}, 1)]}
Cost (4, (2, 31, 1) = min {[d(4, 2) + Cost (2, {3}, 1)], [d(4, 3) + Cost (3, {2}, 1)]}
Step 4: Consider candidate (S) = 3. i.e., Cost (1, {2, 3, 4}) but as we have chosen
vertex 1 initially the cycle should be completed i.e., starting and ending vertex
should be 1.
∴ We will compute,
Chapter-IV
Space-Time Tradeoffs
4.8. Introduction
• Space and time trade-offs in algorithm design are a well-known issue for
both theoreticians and practitioners of computing.
• Consider, as an example The problem of computing values of a function at
many points in its domain. If it is time that is at a premium, we can
precompute the function’s values and store them in a table.
• This is exactly what human computers had to do before the advent of
electronic computers, in the process burdening libraries with thick volumes
of mathematical tables.
• Though such tables have lost much of their appeal with the widespread use
of electronic computers, the underlying idea has proven to be quite useful in
the development of several important algorithms for other problems.
for i ← 0 to n − 1 do Count[i] ← 0
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
Count[j ] ← Count[j ] + 1
return S
for i ← n − 1 downto 0 do
j ← A[i] − l
S[D[j ] − 1] ← A[i]
D[j ] ← D[j ] − 1
return S
Horspool’s Algorithm
Case 2: If there are occurrences of character c in the pattern but it is not the last
one there—e.g., c is letter B in our example—the shift should align the rightmost
Case 3: If c happens to be the last character in the pattern but there are no c’s
is similar to that of Case 1 and 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—e.g., c is letter R in our example—
the situation is similar to that of Case 2 and 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
Step 1: For a given pattern of length m and the alphabet used in both the pattern
and text, construct the shift table as described above.
Step 3: Repeat the following until either a matching substring is found or the
pattern reaches beyond the last character of the text.
Starting with the last character in the pattern, compare the corresponding
characters in the pattern and text until either all m characters are matched
//Output: The index of the left end of the rst matching substring
while i ≤ n − 1 do
while k ≤ m − 1 and P [m − 1 − k] = T [i − k] do
k←k+1
if k = m
return i − m + 1
return −1
Thanking You
http://searchcreators.org/