Chapter Four and Five
Chapter Four and Five
DYNAMIC PROGRAMMING
OUTLINE
• General Method
• Multistage graph problem
• All pairs shortest pattern
• o/I knapsack problem
• Travelling salesperson problem
• Disconnected components
Dynamic programming
Dynamic Programming is an algorithm design technique for optimization
problems: often minimizing or maximizing.
However, solution to one sub-problem may not affect the solutions to other
sub problems of the same problem. DP reduces computation by
Solving sub problems in a bottom-up fashion.
f4 f3
F(n)
f3 f2 f2 f1
F(n-1) + F(n-2)
f2 f1 f1 f0 f1 f0
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
…
F(n-2) =
F(n-1) =
F(n) = F(n-1) + F(n-2)
1 4
S A B 5
T
5 6
• Apply the greedy method :
the shortest path from S to T :
1+2+5=8
Multistage graph problem
• A multistage graph G = (V, E) is a directed graph where
vertices are partitioned into k (where k > 1) number of
disjoint subsets S = {s1,s2,…,sk} such that edge (u, v) is in E,
then u Є si and v Є s1 + 1 for some subsets in the partition and
|s1| = |sk| = 1.
• The vertex s Є s1 is called the source and the vertex t Є sk is
called sink.
• G is usually assumed to be a weighted graph. In this graph,
cost of an edge (i, j) is represented by c(i, j). Hence, the cost
of path from source s to sink t is the sum of costs of each
edges in this path.
• The multistage graph problem is finding the path with
minimum cost from source s to sink t.
Multistage graph problem
There are two Approach's in DP
1. Forward approach and
2. backward approach:
Note that if the recurrence relations are formulated using the
forward approach then the relations are solved backwards .
i.e., beginning with the last decision
On the other hand if the relations are formulated using the
backward approach, they are solved forwards.
To solve a problem by using dynamic programming:
Find out the recurrence relations.
Represent the problem by a multistage graph.
Example of multistage graph problem
Forward approach
cost(4,I) = c(I,L) = 7
cost(4,J) = c(J,L) = 8
cost(4,K) = c(K,L) = 11
• pred(u, v) is the second-to-last vertex (if any) on the shortest path (if any) from u
to v.
• For example, for any vertex v, we have dist(v, v) = 0 and pred(v, v) = Null. If the
shortest path from u to v is only one edge long, then dist(u, v) = w(uv) and pred(u,
4 3
• S=Φ
Cost(2,Φ,1)=d(2,1)=5 Cost(2,Φ,1)=d(2,1)=5
Cost(3,Φ,1)=d(3,1)=6 Cost(3,Φ,1)=d(3,1)=6
Cost(4,Φ,1)=d(4,1)=8 Cost(4,Φ,1)=d(4,1)=8
Cont…
• S = 1
• Cost(i,s)=min{Cost(j,s–j))+d[i,j]}
• Cost(2,{3},1)=d[2,3]+Cost(3,Φ,1)=9+6=15Cost(2,
{3},1)=d[2,3]+Cost(3,Φ,1)=9+6=15
• Cost(2,{4},1)=d[2,4]+Cost(4,Φ,1)=10+8=18Cost(2,
{4},1)=d[2,4]+Cost(4,Φ,1)=10+8=18
• Cost(3,{2},1)=d[3,2]+Cost(2,Φ,1)=13+5=18Cost(3,
{2},1)=d[3,2]+Cost(2,Φ,1)=13+5=18
• Cost(3,{4},1)=d[3,4]+Cost(4,Φ,1)=12+8=20Cost(3,
{4},1)=d[3,4]+Cost(4,Φ,1)=12+8=20
• Cost(4,{3},1)=d[4,3]+Cost(3,Φ,1)=9+6=15Cost(4,
{3},1)=d[4,3]+Cost(3,Φ,1)=9+6=15
• Cost(4,{2},1)=d[4,2]+Cost(2,Φ,1)=8+5=13Cost(4,
{2},1)=d[4,2]+Cost(2,Φ,1)=8+5=13
Cont…
•S = 2
• Cost(2,{3,4},1)={d[2,3]+Cost(3,
{4},1)=9+20=29d[2,4]+Cost(4,{3},1)=10+15=25=25Cost(2,
{3,4},1)={d[2,3]+Cost(3,{4},1)=9+20=29d[2,4]+Cost(4,
{3},1)=10+15=25=25
• Cost(3,{2,4},1)={d[3,2]+Cost(2,
{4},1)=13+18=31d[3,4]+Cost(4,{2},1)=12+13=25=25Cost(3,
{2,4},1)={d[3,2]+Cost(2,{4},1)=13+18=31d[3,4]+Cost(4,
{2},1)=12+13=25=25
• Cost(4,{2,3},1)={d[4,2]+Cost(2,
{3},1)=8+15=23d[4,3]+Cost(3,{2},1)=9+18=27=23Cost(4,
Cont…
•S = 3
• Cost(1,{2,3,4},1)=((d[1,2]+Cost(2,{3,4},1)=10+25=35
d[1,3]+Cost(3,{2,4},1)=15+25=40 d[1,4]+Cost(4,
{2,3},1)=20+23=43=35Cost(1,{2,3,4},1)={ d[1,2]+Cost(2,
{3,4},1)=10+25=35 d[1,3]+Cost(3,{2,4},1)=15+25=40
d[1,4]+Cost(4,{2,3},1)=20+23=43=35
finally the minimum cost path is 35.
And the minimum path in the graph is 1—2-4-3-1.
Hamiltonian path cycle
• Let G=(V,E) be a connected graph with n vertices. A Hamiltonian
path in a given graph G that contains every vertex of G is called a
Hamiltonian Path of G.
• If the current issue cannot be resolved, the step is backtracked and the
next possible solution is applied to previous steps, and then proceeds
further. In fact, one of the key things in backtracking is recursion.
(Ceven ) = 2
(Codd ) = 3
Complete Graphs
(Kn ) = n
CONT…
Wheels
(Wodd ) = 4
(Weven ) = 3
W5
Edge colouring
• A -edge-coloring of a graph is a labeling .
• Edge coloring partitions into sets (some possibly empty) .
• An edge coloring is proper if adjacent edges have different
colors. All coloring henceforth are assumed proper.
• is -edge-colorable if it has -edge-coloring.
• The edge chromatic number is the smallest such that has -
edge-coloring. is called -edge-chromatic.
• E.g.
Reading assignment
• Optimal binary tree search
• Disconnected graph
D ! !
E N
56