Docsity

Optimal Solution to Traveling Salesperson Problem with Dynamic Programming - Prof. Sugih J, Study notes of Algorithms and Programming

Partial preview of the text

Download Optimal Solution to Traveling Salesperson Problem with Dynamic Programming - Prof. Sugih J and more Study notes Algorithms and Programming in PDF only on Docsity!

Outline

Today:

  • Review of P/NP and 2-approximate solution to TSP
  • Dynamic Programming: o 0/1 Knapsack o Travelling Salesperson

P, NP, and NP-Complete

If there’s an algorithm to solve a problem that runs in polynomial time, the problem is said to be in the set P

If the outcome of an algorithm to solve a problem can be verified in polynomial time, the problem is said to be in the set NP (non-deterministic polynomial, the “non-determinism” refers to the outcome of the algorithm, not the verification)

There is a set of problems in NP for which if there’s a polynomial solution to one there will be a polynomial solution to all The set is called NP-Complete

Examples of NP-Complete Problems

Hamiltonian Cycle Problem

Traveling Salesman Problem

0/1 Knapsack Problem

Graph Coloring Problem : can you color a graph using k ≥ 3 colors such that no adjacent vertices have the same color?

A 2-Approximation for a Special Case of TSP

If distances satisfy the triangle inequality , i.e., for edges (u, v), (v, w), (u, w) in G : C(u, v) + C(v, w) ≥ C(u, w), and G is a complete graph , we can build a 2-approximate TSP

k-approximate means that the result is off by a factor of k from the optimal

2-approximate TSP:

  1. construct an MST of the graph (using Prim’s for example)
  2. construct an Euler-cycle of the MST
  3. replace all edges (u, v) and (v, w) for which v is visited more than once with edge (u, w)

Running time: O(|E| log |V |) for MST + O(|V |) for Euler = O(|E| log |V |) as opposed to O(|V | ∗ |V |!) by brute force

2-Approximate Proof

Let:

  • Topt the optimal TSP minus the last edge, which must be a spanning tree
  • M be an MST
  • E the Euler tour, visit every edge in M twice (once per direction)
  • T the non-optimal TSP obtained from E

Claim: T is at most 2 times longer than Topt

Proof:

  • C(T ) ≤ C(E) by triangle inequality C(u, w) ≤ C(u, v) + C(v, w)
  • C(E) = 2C(M )
  • C(M ) ≤ C(Topt) by definition of MST

So C(T ) ≤ 2 C(Topt)

Dynamic Programming

  • used when a problem can be divided into subproblems that overlap
  • solve each subproblem once and store the solution in a table
  • if run across the subproblem again, simply look up its solution in the table
  • reconstruct the solution to the original problem from the solutions to the subproblems
  • the more overlap the better, as this reduces the number of subproblems

Origin of name (Bellman 1957): programming : planning, decision making by a tabular method dynamic : multi-stage, time-varying process

Computing the Fibonacci Sequence (contd)

What is a Fibonacci sequence? f 0 = 0; f 1 = 1; fn = fn− 1 + fn− 2 , n ≥ 2

Recursive implementation:

int rfib(int n) { // assume n >= 0 return (n <= 1? n : rfib(n-1)+rfib(n-2)); } Running time: Ω((^32 )n) [Preiss 3.4.3]

Iterative version: int ifib(int n) { // assume n >= 2 int i, f[n]; f[0] = 0; f[1] = 1; for (i = 2 to n) { f[i] = f[i-1]+f[i-2]; } return f[n]; } Running time: Θ(n) [Preiss 14.3.2, 14.4.1]

Computing the Fibonacci Sequence (contd)

Why is the recursive version so slow? The number of computations grows exponentially! Each rfib(i), i < n − 1 computed more than once Tree size grows almost 2 n Actually the number of base case computations in computing fn is fn Since fn > (^32 )n−^1 (see Preiss Thm. 3.9), complexity is Ω((^32 )n)

Why is the iterative version so fast? Instead of recomputing duplicated subproblems, it saves their results in an array and simply looks them up as needed

Can we design a recursive algorithm that similary look up results of duplicated subproblem?

Example of Problems Solved Using Dynamic Programming

  • All-Pair Shortest Path (Floyd’s algorithm)
  • 0/1 Knapsack
  • Travelling Salesperson

0/1 Knapsack Problem (Preiss 14.1.2, 14.2.5, handouts)

You are a poor Mayan trader, carying wares on your back, in a tattered knapsack, back and forth between Chichen Itza and Palenque

Each item has both a weight and a profit

The tattered knapsack cannot carry more than its capacity or it will rip

Choose a set of items that doesn’t rip the knapsack while maximizing your profit

How to Solve It?

Brute-Force:

Branch-and-Bound: what are the bounding factor(s)?

Greedy:

Dynamic-Programming:

  • can the problem be decomposed into overlapping subproblems?
  • is optimal solution to problem guaranteed to consist only of optimal solutions to subproblems?

0/1 Knapsack Brute Force Solution

Exhaustively enumerate all feasible solution and pick one with the highest total profit

Running time:

0/1 Knapsack Greedy Solution

Put item into knapsack one by one, no removal of item already chosen

Possible greedy criterion:

  • by weight: lowest first
  • by profit: highest first
  • by value (profit/weight): highest first

Optimal solution not guaranteed (see Preiss Tbl 14.1)

0/1 Knapsack Dynamic Programming Solution

Let n = 3, w = { 5 , 10 , 20 }, and p = { 50 , 60 , 140 }, C = 30

Solution table P [i][cj], cj step increments to C: pi wi i 0 5 10 15 20 25 30 50 5 1 0 50 50 50 50 50 50 60 10 2 0 50 110 110 110 110 140 20 3 0 50

Can the problem be decomposed into overlapping subproblems?

Is optimal solution to problem guaranteed to consist only of optimal solutions to subproblems?