You are on page 1of 22

# h

t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
31
DYNAMIC PROGRAMMING
Dynamic Programming is one of commonly used general
approach to solving an optimization problem. The basic strategy of dynamic
programming is to solve the problem with small sizes first, and then use the results for
small sizes to solve the problem with a larger sizes. Step by step, this method can solve
the problem with any large size. Thus,
Unlike the divide-and-conquer approach, the dynamic programming proposes the
bottom-up approach. Dynamic programming is an optimization procedure that is
particularly applicable to problems requiring a sequence of interrelated decisions.
GENERAL METHOD
The art of dynamic programming is how to discover the recursive relationship and
define the sub-problem. There are two key points of dynamic programming:
(1) Optimal substructure:
An optimal solution to a problem (instance) contains optimal solutions to sub-
problem. This step defines a set of values which are optimal solutions for smallest
problems, or define a set of values that can serve as boundary values upon which
solutions for larger problems can be built. For example, if we want to build an optimal
binary search tree with n leaves, we start from n trees, with each having a single node.
Let us call this set S
0
.
(2) Overlapping sub problems:
A recursive solution contains a small number of distinct sub problems repeated
many times. This step repeatedly compute set S
i+1
from the results in S
i
, S
i-1
, S
i-2
, ,
S
0
, until an optimal solution for the original problem is obtained. A problem that can be
solved by dynamic programming must satisfy the principle of optimality. Every
solution in set S
i
, S
i-1
, , S
0
must be optimal in order to produce solutions in S
i+1
which must be also optimal of course.
Over view:
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
32
- Designed to solve optimization problem.
- Dynamic programming also divides a problem into several sub problems. However,
some sub problems may share sub problems.
- Dynamic programming saves time by computing each sub problem only once.
- To do so, it must be conducted in a bottom-up fashion.
MULTISTAGE GRAPHS
A multistage graph is a graph
o G=(V,E) with V partitioned into K >= 2 disjoint subsets such that if (a,b) is in E,
then a is in V
i
, and b is in V
i+1
for some subsets in the partition;
o and | V
1
| = | V
K
| = 1.
The vertex s in V
1
is called the source; the vertex t in V
K
is called the sink.
G is usually assumed to be a weighted graph.
The cost of a path from node v to node w is sum of the costs of edges in the path.
The "multistage graph problem" is to find the minimum cost path from s to t.
[Cf. the "network flow problem".]
Each set V
i
is called a stage in the graph.
Consider the resource allocation problem:
Given n units of resources to be allocated to k projects.
For 1 <= i <= k, 0 <= j <= n,
P(i,j) = profit obtained by allocating "j" units of
the resource to project i.
Transform this to instance of "multistage graph problem".
Create a multistage graph:
V = {s} and denote s = V(1,0) -- read, we are at node 1 having
0 allocated 0 units of resource
Stages 1 to k are such that stage i consists of a set:
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
33
{ V(i+1,j) } j=0 .. n
[we could denote the vertices in this set as: v
i+1j
[or could instead call them v
j
of set V
i
]
The edges are weighted with C(i,j) = -P(i,j) [the negative of the profit] to make it a
minimization problem.
Dynamic Programming solution:
Let path(i,j) be some specification of the minimal path from vertex j in set i to vertex t;
C(i,j) is the cost of this path; c(j,t) is the weight of the edge from j to t.
C(i,j) = min { c(j,l) + C(i+1,l) }
l in V
i+1
(j,l) in E
To write a simple algorithm, assign numbers to the vertices so those in stage V
i
have
lower number those in stage V
i+1
.
int[] MStageForward(Graph G)
{
// returns vector of vertices to follow through the graph
// let c[i][j] be the cost matrix of G
int n = G.n (number of nodes);
int k = G.k (number of stages);
float[] C = new float[n];
int[] D = new int[n];
int[] P = new int[k];
for (i = 1 to n) C[i] = 0.0;
for j = n-1 to 1 by -1 {
r = vertex such that (j,r) in G.E and c(j,r)+C(r) is minimum
C[j] = c(j,r)+C(r);
D[j] = r;
}
P = 1; P[k] = n;
for j = 2 to k-1 {
P[j] = D[P[j-1]];
}
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
34
return P;
}
ALL PAIRS SHORTEST PATH PROBLEM
A weighted graph is a collection of points(vertices) connected by lines(edges), where
each edge has a weight(some real number) associated with it. One of the most common
examples of a graph in the real world is a road map. Each location is a vertex and each
road connecting locations is an edge. We can think of the distance travelled on a road
from one location to another as the weight of that edge.
Given a weighted graph, it is often of interest to know the shortest path from one vertex
in the graph to another. The Floyd-Warshall algorithm algorithm determines the shortest
path between all pairs of vertices in a graph.
Although I don't expect you all to understand the full derivation of this algorithm, I will
go through some of the intuition as to how it works and then the algorithm itself.
The the vertices in a graph be numbered from 1 to n. Consider the subset {1,2...,k} of
these n vertices.
Imagine finding the shortest path from vertex i to vertex j that uses vertices in the set
{1,2, ...,k} only. There are two situations:
1) k is an intermediate vertex on the shortest path.
2) k is not an intermediate vertex on the shortest path.
In the first situation, we can break down our shortest path into two paths: i to k and then
k to j. Note that all the intermediate vertices from i to k are from the set {1,2,...,k-1} and
that all the intermediate vertices from k to j are from the set {1,2,...,k-1} also.
In the second situation, we simply have that all intermediate vertices are from the set
{1,2,...,k-1}.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
35
Now, define the function D for a weighted graph with the vetrtices {1,2,...n} as follows:
D(i,j,k) = the shortest distance from vertex i to vertex j using the intermediate vertices in
the set {1,2,...,k}
Now, using the ideas from above, we can actually recursively define the function D:
D(i,j,k) = w(i,j), if k=0
min( D(i,j,k-1), D(i,k,k-1)+D(k,j,k-1) ) if k > 0
In English, the first line says that if we do not allow intermediate vertices, then the
shortest path between two vertices is the weight of the edge that connects them. If no
such weight exists, we usually define this shortest path to be of length infinity.
The second line pertains to allowing intermediate vertices. It says that the minimum
path from i to j through vertices {1,2,...,k} is either the minimum path from i to j
through vertices {1,2,...,k-1} OR the sum of the minimum path from vertex i to k
through {1,2,...,k-1} plus the minimum path from vertex k to j through {1,2,...k-1}.
Since this is the case, we compute both and choose the smaller of these.
All of this points to storing a 2-dimensional table of shortest distances and using
dynamic programming for a solution.
Here is the basic idea:
1) Set up a 2D array that stores all the weights between one vertex and another.
Here is an example:
0 3 8 inf -4
inf 0 inf 1 7
inf 4 0 inf inf
2 inf -5 0 inf
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
36
inf inf inf 6 0
Notice that the diagonal is all zeros. Why?
Now, for each entry in this array, we will "add in" intermediate vertices one by one,
(first with k=1, then k=2, etc.) and update each entry once for each value of k.
After adding vertex 1, here is what our matrix will look like:
0 3 8 inf -4
inf 0 inf 1 7
inf 4 0 inf inf
2 5 -5 0 -2
inf inf inf 6 0
After adding vertex 2, we get:
0 3 8 4 -4
inf 0 inf 1 7
inf 4 0 5 11
2 5 -5 0 -2
inf inf inf 6 0
After adding vertex 3, we get:
0 3 8 4 -4
inf 0 inf 1 7
inf 4 0 5 11
2 -1 -5 0 -2
inf inf inf 6 0
After adding vertex 4, we get:
0 3 -1 4 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
37
Finally, after adding in the last vertex:
0 1 -3 2 -4
3 0 -4 1 -1
7 4 0 5 3
2 -1 -5 0 -2
8 5 1 6 0
Looking at this example, we can come up with the following algorithm:
Let D1 store the matrix with the initial graph edge information. D2 will stored
calculated information look at D1.
For k=1 to n {
For i=1 to n {
For j=1 to n
D2[i,j] = min(D1[i,j], D1[i,k]+D1[k,j])
}
Copy matrix D2 into D1
}
Last D2 matrix will store all the shortest paths.
In order to code this up, we could do so in a static method. We need the adjacency
matrix of the graph passed into the method as a two dimensional double matrix. Then
we need the auxiliary min and copy methods.
As it turns out, you do NOT need to use 2 separate matrices, even though we traced
through the algorithm in that manner. The reason for this is that when we look up values
in the "current matrix", we know that those values will be at least as good as the values
we would have looked up in the "previous matrix." Thus, in essence, we will not be
overlooking any possible shortest path.
Here is the code for these methods as well as a main that runs this example given above.
public class floyd {
// Runs Floyd Warshall algorithm. Returns the matrix of
// shortest paths.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
38
public static int[][] shortestpath(int[][] adj) {
int n = adj.length;
int[][] m = new int[n][n];
// Initialize m to be graph adjacency matrix
copy(m, adj);
// Add in each vertex as intermediate point.
for (int k=0; k<n;k++) {
// Recalculate estimate for distance from vertex i to j.
for (int i=0; i<n; i++) {
for (int j=0; j<n;j++)
m[i][j] = min(m[i][j], m[i][k]+m[k][j]);
}
}
return m;
}
public static void copy(int[][] a, int[][] b) {
for (int i=0;i<a.length;i++)
for (int j=0;j<a.length;j++)
a[i][j] = b[i][j];
}
public static int min(int a, int b) {
if (a < b)
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
39
return a;
else
return b;
}
public static void main(String[] args) {
// Hard code in example adjacency matrix.
int[][] m = new int;
m = 0; m = 3; m = 8; m = 10000;
m = -4;
m = 10000; m = 0; m = 10000; m = 1;
m=7;
m = 10000; m = 4; m = 0; m = 10000;
m = 10000;
m = 2; m = 10000; m = -5; m = 0;
m = 10000;
m = 10000; m = 10000; m = 10000;
m = 6; m =0;
int[][] shortpath;
shortpath = shortestpath(m);
for (int i=0; i<5;i++) {
for (int j=0; j<5;j++)
System.out.print(shortpath[i][j]+" ");
System.out.println();
}
}
}
Example
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
40
Floyd's All Pairs Shortest Path
| 1 2 3 4
----------------
1| 0 U 4 1
2| 7 0 2 3 Distance Matrix
3| 5 U 0 1
4| 8 U 3 0
| 1 2 3 4
----------------
1|-1 -1 4 -1
2| 3 -1 -1 3 Path Matrix
3|-1 -1 -1 -1
4| 3 -1 -1 -1
Shortest Paths
==============
1
3
4
2
2
3
1
1
10
5
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
41
4 from 1 to 3 = 1->3 = 1->4->3
7 from 2 to 1 = 2->1 = 2->3->1
3 from 2 to 4 = 2->4 = 2->3->4
8 from 4 to 1 = 4->1 = 4->3->1
all other shortest paths are edges
OPTIMAL BINARY SEARCH TREE
Binary search trees
A binary search tree is a binary tree of items that come from an ordered set, such that
1. Each node contains one key.
2. The keys in the left sub-tree of a given node are less than or equal to the key in
that node.
3. The keys in the right sub-tree of a given node are greater than or equal to the key
in that node.
A tree is a graph with a specialized structure. The depth or the level of a node in a tree
is the number of edges in the unique path from the root to the node. The depth of a tree
is the maximum depth of all nodes in the tree. A tree is said to be balanced if the depth
of the two sub-trees of every node never differ by more than 1.
A binary search tree that is organized in a way such that the average time it takes to
locate (search for) a key is minimized is called optimal. Constructing the optimal binary
search tree is an optimization problem and the principle of optimality must apply; more
about this later.
Searching the Tree
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
42
We will use a procedure search to locate a key in a binary search tree. The number of
comparisons done by the procedure search to locate a key is called the search time. Our
goal is to determine a tree for which the average search time is minimal.
void search (node-pointer tree, keytype keyin, node-pointer & p)
{
bool found;
p = false;
while (! found)
if (p->key == keyin)
found = true;
else if ( keyin < p->key);
p = p->left
else
p = p->right;
}
Assume a branch statement is used to implement the nested if-else statement and only
one comparison is performed per each iteration of the while loop. The search time (i.e.,
the number of comparisons) for a given key is:
depth(key) + 1
Let Key
1
, Key
2
, . . . , Key
n
be the n keys in order, and let p
i
be the probability that
Key
i
is the search key. If c
i
is the number of comparisons needed to find Key
i
in a given
tree, the average search time for that tree is: c
i
p
i
for i = 1 to i = n where c
i
=
depth(key
i
) + 1
Example:
Consider these trees with n = 3. (Values of keys are not important; the only requirement
is that they be ordered). We have p
1
= 0.7, p
2
= 0.2 and p
3
= 0.1
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
43
The average search time for the above trees is:
1. 3 (0.7) + 2 (0.2) + 1 (0.1) = 2.6
2. 2 (0.7) + 1 (0.2) + 2 (0.1) = 1.8
3. 1 (0.7) + 2 (0.2) + 3 (0.1) = 1.4
The last tree is optimal.
Constructing the optimal binary search tree
Let A[i][j] represent the average number of comparisons for searching an optimal tree
containing keys i through j.
A[i][j] = p
k
c
k
= p
k
(depth
k
+1) for k=i to j
This is the value that we seek to minimize in an optimal search tree.
Because it takes one comparison to locate a key in a tree containing one node, A[i][i]
=1.
Criterion for an optimal tree (principle of optimality):
Each optimal binary search tree is composed of a root and (at most) two optimal
subtrees, the left and the right. Or, stated in another way: in an optimal tree, all the
subtrees must also be optimal with respect to the keys that they contain.
Let m
ij
= p
k
for k=i to j and
A[i][j] be the average number of comparisons
carried out in an optimal subtree containing the keys key
i
, key
i+1
, key
i+2
, , key
j (as
defined before). One of the keys, say k, must occupy the root of the subtree, as shown
below.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
44
When we look for a key in the main tree, the probability that it is in the sequence
key
i
,
key
i+1
, key
i+2
, , key
j
is m
ij
. In this case, one comparison is made with cost c
k and the
others may then be made in L or R. The average number of comparisons carried out is
therefore:
A[i][j] = m
ij
+ A[i][k-1] + A[k+1][j]
To obtain a dynamic programming scheme, the root, k, must be chosen to minimize
A[i][j]:
A[i][j] = m
ij
+ min
i<=k<=j
(A[i][k-1] + A[k+1][j])
Optimal Binary Search Tree Algorithm
Input:
n, the number of keys and an array of real numbers, p, indexed from 1 to n, where
p[i] is the probability of searching for the ith key. (Provided the keys are sorted, we do
not need their exact values.)
Output:
A variable minavg, whose value is the average search time for an optimal binary
search tree, and a 2-D array R where the row space of R is indexed from 1 to n+1, and
key
k
key
i
key
k-
1
key
k+1
key
j
L
R
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
45
the column space is indexed from 0 to n. R[i][j] is the index of the key in the root of the
tree containing keys i through j.
void optsearchtree (int n, const float p[], float & minavg, index R[][])
{
index i, j, k, diagonal;
float A[1, , n+1][0 n];
for (i = 1; i <= n; i++) {
A[i][i-1] = 0;
A[i][i]= p[i];
R[i][i] = i;
R[i][i-1]=0;
}
A[n+1][n] = 0;
R[n+1][n] = 0;
for (diagonal = 1; diagonal <= n-1; diagonal++)
for (i = 1; i <= n diagonal; i++) {
j = i + diagonal;
A[i][j] = min
i<=k<=j
(A[i][k-1]+A[k+1][j]) + p
m
for m=i to j
R[i][j] = k ; // the value of k that gave the minimum
}
minavg = A[n];
}
Work problem 20 to illustrate this algorithm.
Time Complexity
In the above algorithm, we calculate the value of A[i][j] first for j-i=1, then for j-i=2,
and so on. When j-i=m, there are n-m values of A[i][j] to caluculate, each involving a
choice among m+1 possibilities. The required computation time is therefore:
O (n-m)(m+1)
for m=1 to n-1 =
O(n
3
)
Building the Optimal Search Tree
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
46
Inputs: n, the number of keys, and array, Key containing the n keys in order, and the
array R produced previously.
Outputs: a pointer to an optimal binary search containing the n keys.
node_pointer tree(index i, j)
{
index k;
node_pointer p;
k = R[i][j];
if (k==0)
return NULL;
else {
p = new nodetype;
p->key = Key[k];
p->left = tree(i, k-1);
p->right = tree(k+1, j);
return p;
}
}
0/1 KNAPSACK
The 0-1 Knapsack problem is posed as follows: A thief robbing a store finds n items;
the i-th item is worth c
i
dollars and weights w
i
pounds (or kilograms), where c
i
and w
i
are integers. He wants to take as valuable a load as possible, but he can carry at most W
pounds in his knapsack for some integer W. Which item he should take?
The problem is re-stated formally: given some set N of n items, where the i-th item is
worth c
i
dollars and weights w
i
pounds. Find the subset S of n items to maximize
i
i S
c
e
_
such that i
i S
w W
e
s
_ .
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
47
We have to make decision that pick an item or drop it. Suppose that W is a small
integer. The 0-1 Knapsack problem could be decomposed into O(NW) number of sub
problems.
({w
i
, c
i
}
i=1
n
, W)
= c
n
+ MAX({w
i
, c
i
}
i=1
n-1
, W-w
n
) if pick (w
n
, c
n
),
= ({w
i
, c
i
}
i=1
n-1
, W) otherwise
Example of 0-1 Knapsack problem: W = 4
c
i
6 4 4
w
i
3 2 2
Compute the table bottom-up
4 0 6 6 8
3 0 6 6 6
2 0 4 4 4
1 0 0 0 0
0 0 0 0 0
Weight/
{N items}
0 1 2 3
Each cell in the above table represent the value V(i, j) that is the maximum total value if
we can pick i objects from the set and put them in the knapsack of weight j.
V(i, j) = MAX {c
i
+ V(i -1, j w
i
) if w
i
j; V(i-1, j)}
V(i, j) = 0 if i = 0 or j = 0.
Example of 0-1 Knapsack problem with W = 10
c
i
3 4 2 10 3
w
i
2 3 5 1 4
The best value is 20, computed in the following figure:
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
48
0 0 0 0 0 0
0 10 10 10 10 10
0 10
0 5 4 3 2 1
0
6
5
4
3
2
1
7
0 13 13 13 10 13
0 14 14 14 10 14
0 14 14 14 10 14
0 17 17 14 10 17
0 17 17 14 10 17
9
8
10
0 17 17 14 10 17
0 17 17 14 10 17
0 20 17 14 10 20
Total
weight
Bag of
size i
10 10 10 10
Figure 3 0-1 Knapsack computation
The algorithm takes as input the maximum weight W, the number of items n, and the
two sequences v = <v
1
, v
2
, . . . , v
n
> and w = <w
1
, w
2,
. . . , w
n
>. It stores the c[i, j] values
in the table, that is, a two dimensional array, c[0 . . n, 0 . . w] whose entries are
computed in a row-major order. That is, the first row of c is filled in from left to right,
then the second row, and so on. At the end of the computation, c[n, w] contains the
maximum value that can be picked into the knapsack.
Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W
do c[0, w] = 0
for i = 1 to n
do c[i, 0] = 0
for w = 1 to W
do if w
i
w
then if v
i
+ c[i-1, w-w
i
]
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
49
then c[i, w] = v
i
+ c[i-1, w-w
i
]
else c[i, w] = c[i-1, w]
else
c[i, w] = c[i-1, w]
The set of items to take can be deduced from the table, starting at c[n. w] and tracing
backwards where the optimal values came from. If c[i, w] = c[i-1, w] item i is not part of
the solution, and we are continue tracing with c[i-1, w]. Otherwise item i is part of the
solution, and we continue tracing with c[i-1, w-W].
Analysis
This dynamic-0-1-kanpsack algorithm takes (nw) times, broken up as follows:
(nw) times to fill the c-table, which has (n+1).(w+1) entries, each requiring (1) time to
compute. O(n) time to trace the solution, because the tracing process starts in row n of
the table and moves up 1 row at each step.
THE TRAVELING SALESPERSON PROBLEM
The Traveling Sales Person problem is an optimization problem: find the shortest
route through a set of cities visiting each city only once. The set of cities is represented
as a graph and the roads between them as edges. The objective of the TSP problem is to
determine the shortest path from one vertex in a graph, going through every other vertex
in the graph exactly once, and ending up back at the starting vertex
e.g. a directed graph :
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
50
1
2
3
2
4
4
2
5 6
7
10
4
8
3 9
cost matrix :
1 2 3 4
1

2 10 5
2 2

9

3 4 3

4
4 6 8 7

multi-stage graph:
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
51
(1) (1,3)
(1,2)
(1,4)
2
5
10
(1,2,3)
(1,2,4)
(1,3,2)
(1,3,4)
(1,4,2)
(1,4,3)
9
3
4
8
7

(1,2,3,4)
(1,2,4,3)
(1,3,2,4)
(1,3,4,2)
(1,4,2,3)
(1,4,3,2)

4
7
8
9
3
1
4
6
6
2
4
2
Fig. A Multi-Stage Graph Describing All Possible Tours of a Directed graph.
find the shortest path:
1, 4, 3, 2, 1 5+7+3+2=17
Suppose that we have 6 vertices in the graph.
We can combine {1, 2, 3, 4} and {1, 3, 2, 4} into one node.
combine
(2), (4,5,6)
(b)
(3), (4,5,6)
(4), (5,6)
(1,3,2)
(1,2,3) (1,2,3,4)
(1,3,2,4)
(a)
the last vertex visited is 3
the remaining vertices to be visited are 4, 5, 6
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
GKMCET
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis of Algorithm
Unit No.: III Prepared by: Purushothaman.R
__________________________________________________________________________________
52
Let g(i, S) be the length of a shortest path starting at vertex i, going through all
vertices in S and terminating at vertex 1.
The length of an optimal tour :
g(1, V-{1}) = min {c
1k
+ g(k, V-{1, k})}
2sksn
The general form:
g(i, S) = min {c
ij
+ g(j, S-{j})}
jeS
time complexity:
n+ ( )( )( ) n n k
n k
n
k
n

_

=
1
2
2
= O(n
2
2
n
)
( ),( ) (n-k)
| |
(n-1) ( )
n k
n

2
http://csetube.co.nr/