50% found this document useful (2 votes)
1K views23 pages

PPT6 - Dynamic Programming Multistage Graph & Travelling Salesman Problem

Here are the key steps in calculating the travelling salesman problem using dynamic programming: 1. Initialize the cost of visiting a single node from the starting node 2. Calculate the cost of visiting two nodes by taking the minimum of visiting the second node from the first + cost of first node 3. Continue calculating costs of visiting increasing number of nodes 4. The overall minimum cost is the cost of visiting all nodes starting from the starting node This recursively builds up the optimal solutions by reusing previously calculated sub-solutions. It allows solving the NP-hard TSP problem in polynomial time for a fixed number of cities using dynamic programming.

Uploaded by

Aditya wahyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
1K views23 pages

PPT6 - Dynamic Programming Multistage Graph & Travelling Salesman Problem

Here are the key steps in calculating the travelling salesman problem using dynamic programming: 1. Initialize the cost of visiting a single node from the starting node 2. Calculate the cost of visiting two nodes by taking the minimum of visiting the second node from the first + cost of first node 3. Continue calculating costs of visiting increasing number of nodes 4. The overall minimum cost is the cost of visiting all nodes starting from the starting node This recursively builds up the optimal solutions by reusing previously calculated sub-solutions. It allows solving the NP-hard TSP problem in polynomial time for a fixed number of cities using dynamic programming.

Uploaded by

Aditya wahyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

COMP6127 - Algorithm Design and Analysis

Week 6
Session 10
Dynamic Programming: Multistage Graph
and Travelling Salesman Problem
The General Objectives
Multistage Graph
 Understand the concept of Multistage Graph solution using
Dynamic Programming

Travelling Salesman Problem


 Understand the concept of Travelling Salesman Problem
using Dynamic Programming.

2
Outline

Multistage Graph
 Multistage Graph Problem
 Forward Technique
 Backward Technique

Travelling Salesman Problem


Travelling Salesman Problem
 Solving of Travelling Salesman Problem

3
COMP6127 - Algorithm Design and Analysis

MULTISTAGE GRAPH
Dynamic Programming:
Multistage Graph

 Multistage Graph is a graph with special characteristics:


1. Directed Graph
2. Each edge has weight
3. Has only 1 source (called as s) and 1 sink (called as t)
4. Path from source to sink consists some stages V1 to Vk
5. All stages connect node in Vi to node Vi+1 where 1 ≤ i ≤ k
6. There are k stages, where k ≥ 2
7. Each path from s to t is consequence of choice k-2.

 Multistage Graph is a modeling that can be used to solve some real


problems.
 Example: choosing project to get maximum profit; including selecting
steps to perform each task.

5
Dynamic Programming:
Multistage Graph Problem

 Multistage Graph Problem :


 Sortest path finding problem from source to sink in Multistage Graph.
 The problem is one of good implementation of Dynamic
Programming.

6
Dynamic Programming:
Multistage Graph Problem

 Solving of Multistage Graph problem using Dynamic Programming


in shortest path from a node to another is a shortest path of
previous stage added by distance of one of an edge connects to a
stage.

 Forward Method
 Calculate distance to front (to sink)

 Backward Method
 Calculate distance to back (from source)

7
Dynamic Programming:
Forward Method

 Analysis by calculating path from a node to sink


 Formula: cost(i,j) = min{c(j,k) + cost(i+1,k)}
 Calculation starts from nodes in stage k-2
 cost(i,j) is distance of path from node j in stage i to sink(t)
 c(j,l) is distance of path from node j to node l

8
Dynamic Programming:
Forward Method
 cost(4,I) = c(I,L) = 7
 cost(4,J) = c(J,L) = 8
 cost(4,K) = c(K,L) = 11
 cost(3,F) = min { c(F,I) + cost(4,I) | c(F,J) + cost(4,J) }
 cost(3,F) = min { 12 + 7 | 9 + 8 } = 17
 cost(3,G) = min { c(G,I) + cost(4,I) | c(G,J) + cost(4,J) }
 cost(3,G) = min { 5 + 7 | 7 + 8 } = 12
 cost(3,H) = min { c(H,J) + cost(4,J) | c(H,K) + cost(4,K) }
 cost(3,H) = min { 10 + 8 | 8 + 11 } = 18
 cost(2,B) = min { c(B,F) + cost(3,F) | c(B,G) + cost(3,G) | c(B,H) + cost(3,H) }
 cost(2,B) = min { 4 + 17 | 8 + 12 | 11 + 18 } = 20
 cost(2,C) = min { c(C,F) + cost(3,F) | c(C,G) + cost(3,G) }
 cost(2,C) = min { 10 + 17 | 3 + 12 } = 15
 cost(2,D) = min { c(D,H) + cost(3,H) }
 cost(2,D) = min { 9 + 18 } = 27
 cost(2,E) = min { c(E,G) + cost(3,G) | c(E,H) + cost(3,H) }
 cost(2,E) = min { 6 + 12 | 12 + 18 } = 18
 cost(1,A) = min { c(A,B) + cost(2,B) | c(A,C) + cost(2,C) | c(A,D) + cost(2,D) | c(A,E) + cost(2,E) }
 cost(1,A) = min { 7 + 20 | 6 + 15 | 5 + 27 | 9 + 18 } = 21

 Shortest path is A-C-G-I-L with distance 21

9
Dynamic Programming:
Backward Method

 Analysis by calculating path from source to a node


 Formula: bcost(i,j) = min{bcost(i–1,l) + c(l,j)}
 Calculation starts from nodes in stage 3
 bcost(i,j) is distance of path backward from source (s) to node j in
stage i
 c(j,l) is distance of path from node j to node l

10
Dynamic Programming:
Backward Method
 bcost(2,B) = c(A,B) = 7
 bcost(2,C) = c(A,C) = 6
 bcost(2,D) = c(A,D) = 5
 bcost(2,E) = c(A,E) = 9.
 bcost(3,F) = min { c(B,F) + bcost(2,B) | c(C,F) + bcost(2,C) }
 bcost(3,F) = min { 4 + 7 | 10 + 6 } = 11
 bcost(3,G) = min { c(B,G) + bcost(2,B) | c(C,G) + bcost(2,C) | c(E,G) + bcost(2,E) }
 bcost(3,G) = min { 8 + 7 | 3 + 6 | 6 + 9 } = 9
 bcost(3,H) = min { c(B,H) + bcost(2,B) | c(D,H) + bcost(2,D) | c(E,H) + bcost(2,E) }
 bcost(3,H) = min { 11 + 7 | 9 + 5 | 12 + 9 } = 14
 bcost(4,I) = min { c(F,I) + bcost(3,F) | c(G,I) + bcost(3,G) }
 bcost(4,I) = min { 12 + 11 | 5 + 9 } = 14
 bcost(4,J) = min { c(F,J) + bcost(3,F) | c(G,J) + bcost(3,G) | c(H,J) + bcost(3,H) }
 bcost(4,J) = min { 9 + 11 | 7 + 9 | 10 + 14 } = 16
 bcost(4,K) = min { c(H,K) + bcost(3,H) }
 bcost(4,K) = min { 8 + 14 } = 22
 bcost(5,L) = min { c(I,L) + bcost(4,I) | c(J,L) + bcost(4,J) | c(K,L) + bcost(4,K) }
 bcost(5,L) = min { 7 + 14 | 8 + 16 | 11 + 22 } = 21

 Shortest path is A-C-G-I-L with distance 21

11
Dynamic Programming:
Shortest Path in Multistage Graph

12
Exercise
1. Find shortest path from node A to node L using Dynamic
Programming (forward method and backward method) !

13
COMP6127 - Algorithm Design and Analysis

TRAVELLING SALESMAN PROBLEM


Dynamic Programming:
Travelling Salesman Problem

 A salesman is selling his products to some addresses.


 Distance inter-addresses are different.
 He find a route to visit all addresses as quick as possible (shortest
route), then go back to his home.
 This problem is called as Traveling Salesman Problem (or TSP).

15
Dynamic Programming:
Implementation of TSP

 Implementation of TSP in computer, each address is a node in


graph.
 Each path from an address to another is an edge that has weight.
 The salesman starting his travel from a node A (his house), he MUST
visit all addresses only 1 time and MUST go back to his house.

16
Dynamic Programming:
TSP - Case

 Formula of Dynamic Programming in TSP


– p(i,L) = min[c(j,i) + p(j,L–{j})]

17
Dynamic Programming:
Representation of TSP

 We use Cost Matrix, not Adjacency Matrix, why?


 If between 2 nodes has not an edge connection, means there
is no path between the nodes. If we use 0 (zero) value in
Adjacency Matrix, then Dynamic Programming algorithm will
detect as shortest distance.

18
Dynamic Programming:
Formula of TSP with DP
 p(i,L) = min[c(j,i) + p(j,L–{j})]

 p(i,S) is distance of path from initial node to node i after path L.


 c(j,i) is distance from node j to node i.
 c(j,i) does not equal to c(i,j) because the graph contains 2 directed edges
with different weight.

 L-{j} is distance of path L subtracted by node j.

 Then, the shortest path for the graph is p(A,{B,C,D}) that means distance
of path from intial node A after going through node B, C, D by any
sequences (found the shortest path).

19
Dynamic Programming:
Calculation of TSP
p(B,Ø)=c(A,B)=12
p(C,Ø)=c(A,C)=11
p(D,Ø)=c(A,D)=16
p(B,{C})=c(C,B)+p(C,Ø)=25
p(B,{D})=c(D,B)+p(D,Ø)=27
p(C,{B})=c(B,C)+p(B,Ø)=29
p(C,{D})=c(D,C)+p(D,Ø)=33
p(D,{B})=c(B,D)+p(B,Ø)=22
p(D,{C})=c(C,D)+p(C,Ø)=29
p(B,{C,D})= min[c(C,B)+p(C,{D})|c(D,B)+p(D,{C})]
= min[14+33|11+29] = 40
p(C,{B,D})= min[c(B,C)+p(B,{D})|c(D,C)+p(D,{B})]
= min[15+27|17+22] = 39
p(D,{B,C})= min[c(B,D)+p(B,{C})|c(C,D)+p(C,{B})]
= min[10+25|18+27] = 35
p(A,{B,C,D})= min[c(B,A)+p(B,{C,D}) | c(C,A)+p(C,{B,D}) | c(D,A)+p(D,{B,C})]
= min[15+40|8+39|9+35]
= 44
•Distance of the shortest path = 44

20
Dynamic Programming:
Calculation of TSP
p(B,Ø)=c(A,B)=12
p(C,Ø)=c(A,C)=11
p(D,Ø)=c(A,D)=16
p(B,{C})=c(C,B)+p(C,Ø)=25
p(B,{D})=c(D,B)+p(D,Ø)=27
p(C,{B})=c(B,C)+p(B,Ø)=29
p(C,{D})=c(D,C)+p(D,Ø)=33
p(D,{B})=c(B,D)+p(B,Ø)=22
p(D,{C})=c(C,D)+p(C,Ø)=29
p(B,{C,D})= min[c(C,B)+p(C,{D}) | c(D,B)+p(D,{C})]
= min[14+33 | 11+29] = 40
p(C,{B,D})= min[c(B,C)+p(B,{D}) | c(D,C)+p(D,{B})]
= min[15+27 | 17+22] = 39
p(D,{B,C})= min[c(B,D)+p(B,{C}) | c(C,D)+p(C,{B})]
= min[10+25 | 18+27] = 35
p(A,{B,C,D})= min[c(B,A)+p(B,{C,D}) | c(C,A)+p(C,{B,D}) | c(D,A)+p(D,{B,C})]
= min[15+40 | 8+39 | 9+35]
= 44
•Distance of the shortest path = 44

21
Exercise
1. Given a Cost Matrix of TSP as:

  17  8 12
10  15 11 14

 8 16  9 10
 
12 19 10  13
15 7 19   

2. Depict the TSP Graph!


3. Find the shortest path of TSP using Dynamic Programming !

22
Thank You

You might also like