3 - - PowerPoint PPT Presentation

3 4
SMART_READER_LITE
LIVE PREVIEW

3 - - PowerPoint PPT Presentation

3 4


slide-1
SLIDE 1
  • 34

www.win.tue.nl/~kbuchin/teaching/2IL15/ Lecturer: Kevin Buchin (MF 6.093, k.a.buchin@tue.nl)

slide-2
SLIDE 2
  • Organization of the course

Similar to Datastructures

  • homework exercises
  • tutorials (for help in solving homework + discussing solutions)
  • minimum score needed for homework to be admitted to exam
  • registration via OASE mandatory (at the latest today),

register for group this week (Tuesday-Thursday) Literature: same as Datastructures

  • T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein:

IntroductiontoAlgorithms (3rd edition).

slide-3
SLIDE 3
  • homework set A.1

homework set A.2 homework set B.1 homework set B.2 homework set C.1 homework set C.2 Organization of the course Part I: Techniques for optimization backtracking exercises greedy algorithms exercises dynamic programming I exercises dynamic programming II exercises Part II: Graph algorithms shortest paths I exercises shortest paths II exercises max flow exercises matching exercises Part III: Selected topics NP>hardness I exercises NP>hardness II exercises approximation algorithms exercises linear programming exercises

slide-4
SLIDE 4
  • Planning

and so on …: see the course webpage

slide-5
SLIDE 5
  • )

Grading Homework (no copying, solutions submitted in pdf by email to instructor)

  • six sets: two sets for each of the three parts

(but: handed in per part, so only three deadlines)

  • best four sets count, but at least one per part
  • maximum homework score: 4 x 10 = 40 points

Exam

  • need at least 20 points for Homework
  • maximum score = 10 points

Final grade

  • need at least 5 points for exam, otherwise FAIL
  • if final exam at least 5: final grade = (homework + 4 exam) / 8

register this week for one

  • f the three tutorial groups
slide-6
SLIDE 6
  • /

Part I: Techniques for optimization

slide-7
SLIDE 7
  • 4

Optimization problems

  • for each instance there are (possibly) multiple valid solutions
  • goal is to find an optimal solution
  • minimization problem:

associate cost to every solution, find min>cost solution

  • maximization problem:

associate profit to every solution, find max>profit solution

slide-8
SLIDE 8
  • 5

17 15 33 Optimization problems: examples Traveling Salesman Problem

  • input = set of n cities with distances between them
  • valid solution = tour visiting all cities
  • cost = length of tour

A B C D E F A B C D E F A > 17 B > C > 15 33 D > E > F >

slide-9
SLIDE 9
  • 6

Optimization problems: examples Traveling Salesman Problem

  • input = set of n cities with distances between them
  • valid solution = tour visiting all cities
  • cost = length of tour

Knapsack

  • input = n items, each with a weight and a profit, and value W
  • valid solution = subset of items whose total weight is ≤ W
  • profit = total profit of all items in subset

5 14 10 4.5 7 6 profit 6 11 8 5 7 5 weight 6 5 4 3 2 1 item W = 18 solutions: 1,2,6: weight 18, profit 18

slide-10
SLIDE 10
  • 6

Optimization problems: examples Traveling Salesman Problem

  • input = set of n cities with distances between them
  • valid solution = tour visiting all cities
  • cost = length of tour

Knapsack

  • input = n items, each with a weight and a profit, and value W
  • valid solution = subset of items whose total weight is ≤ W
  • profit = total profit of all items in subset

5 14 10 4.5 7 6 profit 6 11 8 5 7 5 weight 6 5 4 3 2 1 item W = 18 solutions: 1,2,6: weight 18, profit 18 2,5: weight 18, profit 21 etcetera

slide-11
SLIDE 11
  • 7

Optimization problems: examples Traveling Salesman Problem

  • input = set of n cities with distances between them
  • valid solution = tour visiting all cities
  • cost = length of tour

Knapsack

  • input = n items, each with a weight and a profit, and value W
  • valid solution = subset of items whose total weight is ≤ W
  • profit = total profit of all items in subset

Linear Programming minimize: c1 x1 + HHH + cn xn subject to: a1,1 x1 + HHH + a1,n xn ≤ b1 am,1 x1 + HHH + am,n xn ≤ bm even hard to find any solution!

slide-12
SLIDE 12
  • Techniques for optimization
  • ptimization problems typically involve making choices
  • backtracking: just try all solutions
  • can be applied to almost all problems, but gives very slow algorithms
  • try all options for first choice,

for each option, recursively make other choices

  • greedy algorithms: construct solution iteratively, always make choice that

seems best

  • can be applied to few problems, but gives fast algorithms
  • nly try option that seems best for first choice (greedy choice),

recursively make other choices

  • dynamic programming
  • in between: not as fast as greedy, but works for more problems
slide-13
SLIDE 13
  • Today: backtracking + how to (slightly?) speed it up

Example 1: Traveling Salesman Problem (TSP) given: n cities and the (non>negative) distances between them Input: matrix Dist [1..n,1..n ], where Dist[i,j ] = distance from i to j goal: find shortest tour visiting all cities and returning to starting city Output: permutation of {1,…,n} such that visiting cities in that order gives min>length tour choices: what is first city to visit ? what is second city to visit ? … what is last city to visit ? 1 2 4 5 6 3 start start w.l.o.g. in city 1 start

slide-14
SLIDE 14
  • Backtracking for TSP:
  • first city is city 1
  • try all remaining cities as next city

for each option for next city, recursively try all ways to finish the tour for each recursive call:

  • remember which choices we already made

( = part of the tour we fixed in earlier calls)

  • and which choices we still need to make

( = remaining cities, for which we need to decide visiting order)

  • when all choices have been made:

compute length of tour, compare to length of shortest tour found so far parameters of algorithm

slide-15
SLIDE 15
  • Parameters: R = sequence of already visited cities (initially: R = city 1)

S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(R,S)

  • 1. Sis empty

2. minCost ← length of the tour represented by R 3. minCost ← 4. each city iin S 5. !Remove ifrom S, and append ito R. 6. minCost ← min(minCost,TSP_BruteForce1(R,S) ) 7. Reinsert iin S, and remove ifrom R.

  • 8. minCost

We want to compute a shortest tour visiting all cities in RU S, under the condition that the tour starts by visiting the cities from Rin the given order. 8 iis next city undo choice all choices have been made recursively compute best way to make remaining choices try all remaining cities as next city

slide-16
SLIDE 16
  • )

Parameters: R = sequence of already visited cities (initially: R = city 1) S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(R,S)

  • 1. Sis empty

2. minCost ← length of the tour represented by R 3. minCost ← 4. each city iin S 5. !Remove ifrom S, and append ito R. 6. minCost ← min(minCost,TSP_BruteForce1(R,S) ) 7. Reinsert iin S, and remove ifrom R.

  • 8. minCost

We want to compute a shortest tour visiting all cities in RU S, under the condition that the tour starts by visiting the cities from Rin the given order. 8 iis next city undo choice all choices have been made recursively compute best way to make remaining choices try all remaining cities as next city Note: this algorithm computes length of optimal tour, not tour itself

slide-17
SLIDE 17
  • /

Parameters: R = sequence of already visited cities (initially: R = city 1) S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(R,S)

  • 1. Sis empty

2. minCost ← length of the tour represented by R 3. minCost ← 4. each city iin S 5. !Remove ifrom S, and append ito R. 6. minCost ← min(minCost,TSP_BruteForce1(R,S) ) 7. Reinsert iin S, and remove ifrom R.

  • 8. minCost

Possible implementation: R and S are linked lists Analysis: nR = size of R, nS = size of S

8

O(1) O(nR) O(1) T(nR,nS) =nS " ( O(1) +T(nR+1,nS$1) ) with T(nR,0) =O(nR)

slide-18
SLIDE 18
  • /

Parameters: R = sequence of already visited cities (initially: R = city 1) S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(R,S)

  • 1. Sis empty

2. minCost ← length of the tour represented by R 3. minCost ← 4. each city iin S 5. !Remove ifrom S, and append ito R. 6. minCost ← min(minCost,TSP_BruteForce1(R,S) ) 7. Reinsert iin S, and remove ifrom R.

  • 8. minCost

Possible implementation: R and S are linked lists Analysis: nR = size of R, nS = size of S

8

O(1) O(nR) O(1) T(nR,nS) =nS " ( O(1) +T(nR+1,nS$1) ) with T(nR,0) =O(nR) T(nR,nS) = O( (nR+nS) H nS! ) total running time = O( n! )

slide-19
SLIDE 19
  • 4

R = sequence of already visited cities (initially: R = city 1) S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(A,k ) 1. n ← length[A] 2. k=n 3. minCost ← length of the tour represented by A[1..n] 4. minCost ← 5. i←k+1 n 6. !Swap A[i ] and A[k+1] 7.

  • 8. minCost ← min(minCost,TSP_BruteForce1(A,k+1) )

9. Swap A[k+1] and A[i ]

  • 10. minCost

Alternative implementation: ▪ A[1..n] = array of cities, parameter 1 ≤ k≤ n

▪ A[1..k] contains R, A [ k+1 .. n] contains S

8

slide-20
SLIDE 20
  • 4

R = sequence of already visited cities (initially: R = city 1) S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(A,k ) 1. n ← length[A] 2. k=n 3. minCost ← length of the tour represented by A[1..n] 4. minCost ← 5. i←k+1 n 6. !Swap A[i ] and A[k+1] 7.

newLength ← lengthSoFar +Dist[ A[k],A[k+1] ]

  • 8. minCost ← min(minCost,TSP_BruteForce1(A,k+1) )

9. Swap A[k+1] and A[i ]

  • 10. minCost

Alternative implementation: ▪ A[1..n] = array of cities, parameter 1 ≤ k≤ n

▪ A[1..k] contains R, A [ k+1 .. n] contains S

8 improvement: maintain lengthSoFar = length of initial part given by A[1] .. A[k] lengthSoFar +Dist[ A[n],A[1] ] lengthSoFar newLength

slide-21
SLIDE 21
  • 5

R = sequence of already visited cities (initially: R = city 1) S = set of remaining cities (initially: S = { 2, …, n } ) TSP_BruteForce1(A,k ) 1. n ← length[A] 2. k=n 3. minCost ← length of the tour represented by A[1..n] 4. minCost ← 5. i←k+1 n 6. !Swap A[i ] and A[k+1] 7.

newLength ← lengthSoFar +Dist[ A[k],A[k+1] ]

  • 8. minCost ← min(minCost,TSP_BruteForce1(A,k+1) )

9. Swap A[k+1] and A[i ]

  • 10. minCost

Alternative implementation: ▪ A[1..n] = array of cities, parameter 1 ≤ k≤ n

▪ A[1..k] contains R, A [ k+1 .. n] contains S

8 improvement: maintain lengthSoFar = length of initial part given by A[1] .. A[k] lengthSoFar +Dist[ A[n],A[1] ] lengthSoFar newLength Old running time: O(n) for all permutations of 2,…n, so O(n!) in total New running time: O((n>1)!) still very very slow

slide-22
SLIDE 22
  • 6

TSP_BruteForce1(A,k,lengthSoFar,) 1. n ← length[A] 2. k=n 3. minCost ← lengthSoFar +Dist[ A[n],A[1] ] 4. minCost ← 5. i←k+1 n "# !Swap A[i ] and A[k+1]

newLength ← lengthSoFar +Dist[ A[k],A[k+1] ]

7. minCost ←min(minCost,TSP_BruteForce1(A,k+1) ) 8. Swap A[k+1] and A[i ]

  • 9. minCost

A[1..n] = array of cities, parameter 1 ≤ k≤ n A[1..k] contains initial part of tour, A [ k+1 .. n] contains remaining cities 8 pruning: don’t recurse if initial part cannot lead to optimal tour newLength minCost newLength ≥ minCost $

  • minCost← min (minCost,lengthSoFar +Dist[ A[n],A[1] ] )

minCost

slide-23
SLIDE 23
  • Intermezzo: Queens Problem (on NxN board)

backtracking: not only for optimization problems parameters of algorithm? running time?

slide-24
SLIDE 24
  • 7

backtracking + pruning (gives branch$and$bound algorithm) Example 2: 1>dimensional clustering given: X = set of n numbers (points in 1D), parameter k goal: find partitioning of S into k clusters of minimum cost

cluster 1 cluster 2 cluster 3 cost of single cluster: sum of distance to cluster average cost of total clustering: sum of costs of its clusters 1,3,4,6: average = 3.5, cost = 6

slide-25
SLIDE 25
  • backtracking + pruning (gives branch$and$bound algorithm)

Example 2: 1>dimensional clustering given: X = set of n numbers (points in 1D), parameter k assume points are sorted from left to right goal: find partitioning of S into k clusters of minimum cost

cluster 1 cluster 2 cluster 3 choices to be made

  • cluster 1 always starts at point 1
  • we have a choice where to start clusters 2, …, k
slide-26
SLIDE 26
  • Backtracking for 1>dimensional clustering:
  • first cluster starts at point 1
  • try all remaining points as starting point for next cluster

for each option, recursively try all ways to finish the clustering for each recursive call:

  • remember which choices we already made

( = starting points we fixed in earlier calls)

  • and which choices we still need to make

( = starting points for remaining clusters)

  • when all choices have been made:

compute cost of clustering, compare to best clustering found so far parameters of algorithm

slide-27
SLIDE 27
  • Parameters: X[1..n] = sorted set of numbers (points in 1>dimensional space)

k = desired number of clusters, A[1..k] = starting points m = number of starting points that have already been fixed Clustering(X,k,A,m)

  • 1. m=k

2. minCost ← total cost of clustering represented by A[1..k] 3. minCost ← 4. i ← A[m]+1 n– (k$m$1) 5. !A [m+1] ← i 6. minCost ← min(minCost,Clustering(X,k,A,m+1) ) 7. A [m+1] ←

  • 8. minCost

We want to compute an optimal partitioning of X into k clusters, under the condition that the starting points of clusters 1,..,m are as given by A[1..m]. 8 number of clusterings: ( ) = O( nk) running time: O( nk+1)

n k

slide-28
SLIDE 28
  • Parameters: X[1..n] = sorted set of numbers (points in 1>dimensional space)

k = desired number of clusters, A[1..k] = starting points m = number of starting points that have already been fixed Clustering(X,k,A,m,minCost,costSoFar)

  • 1. m=k

2. minCost ← min (minCost,costSoFar +cost last cluster ) 3. minCost ← 4. i ← A[m]+1 n– (k$m$1) # !A [m+1] ← i newCost ← costSoFar +cost m$th cluster newCost ≥ minCost $ 6. minCost ← min(minCost,Clustering(X,k,A,m+1) ) 7. A [m+1] ←

  • 8. minCost

Pruning: do not recurse if solution cannot get better than current best 8 minCost,newCost

slide-29
SLIDE 29
  • )

Parameters: X[1..n] = sorted set of numbers (points in 1>dimensional space) k = desired number of clusters, A[1..k] = starting points m = number of starting points that have already been fixed Clustering(X,k,A,m,minCost,costSoFar)

  • 1. m=k

2. minCost ← min (minCost,costSoFar +cost last cluster ) 3. minCost ← 4. i ← A[m]+1 n– (k$m$1) # !A [m+1] ← i newCost ← costSoFar +cost m$th cluster newCost ≥ minCost $ 6. minCost ← min(minCost,Clustering(X,k,A,m+1) ) 7. A [m+1] ←

  • 8. minCost

Pruning: do not recurse if solution cannot get better than current best 8 minCost,newCost NOTE: This problem can actually be solved much more efficiently, for example using dynamic programming (as we will see later).

slide-30
SLIDE 30
  • Intermezzo: Map Coloring problem

adjacent regions should have different colors parameters of algorithm? running time? what if we want to check if 2 colors are enough?

slide-31
SLIDE 31
  • /

Summary

  • backtracking solves optimization problem by generating all solutions
  • this is done best using a recursive algorithm
  • backtracking is typically very slow
  • speed can be improved using pruning, but it is usually hard to prove

anything about how much the running time improves (and algorithm usually remains slow)

  • there is a trade>off: more complicated pruning tests may result in fewer

recursive calls, but pruning itself becomes more expensive