Download presentation
Presentation is loading. Please wait.
Published byPatrick Melton Modified over 8 years ago
1
Honors Track: Competitive Programming & Problem Solving Optimization Problems Kevin Verbeek
2
Optimization Problems Optimization problem Optimize the “cost function” over all “solutions” Valid solution A solution that satisfies the constraints There can be many! (for a single instance) Example: shortest s-t path ➨ solutions: paths from s to t Cost function Function that measures “quality” of solution Minimization problem: minimize the cost function Maximization problem: maximize the cost function
3
Traveling Salesman Problem Input: n cities with distances between them Valid solution: tour visiting all cities Cost function: length of tour Goal: minimize cost A B C D E F ABCDEF A06 B09 C60 D90 E04 F40 4 6 9 This is a hard problem! (In fact, NP-hard)
4
0/1 Knapsack Problem Input: n items, each with a weight and a profit, and a value W Valid solution: A subset of the items with total weight ≤ W Cost function: Total profit of items in subset Goal: Maximize cost (profit) item123456 weight5758116 profit67410145 W = 18 Solutions: {1, 2, 6} ➨ profit = 18 {2, 5} ➨ profit = 21 A very common problem in programming contests
5
Solution strategies Building solutions typically involves making choices Brute force Try all (valid) solutions Always works, but typically very slow Greedy algorithms Construct solution iteratively, always make choice that seems best Often doesn’t work, but typically very fast Dynamic programming Try all choices in a clever way Requires optimal substructure and subproblem encoding
6
Brute force Brute force strategies Brute force Generate all solutions Check validity (if necessary) Compute cost and keep min/max Backtracking Building solutions involves choices Make choices one at a time Use recursion Discard partial solutions if cannot be valid Branch and bound Like backtracking Check if partial solution can become optimal
7
TSP Backtracking Choices Assume we start at A What is 2 nd city we visit? 3 rd ? Algorithm TSP(A, i) 1. if i = n then return d(A[n], A[1]) 2. else c = ∞ 3. for j = i to n 4. do “swap A[i] with A[j]” 5. c = min(c, TSP(A, i+1) + d(A[i-1], A[i])) 6. “swap A[i] with A[j]” 7. return c A B C D E F ABCDEF A06 B09 C60 D90 E04 F40 Initial call: TSP(A, 2) Only returns length of optimal tour!
8
Knapsack Backtracking Choices For each item: in or out? Alternative: which item is next? Algorithm KS(A, i, w) 1. if i = n+1 then return 0 2. else p = KS(A, i+1, w) 3. if w + A[i].weight ≤ W 4. then r = KS(A, i+1, w + A[i].weight) 5. p = max(p, r + A[i].profit) 6. return p item123456 weight5758116 profit67410145 W = 18 Initial call: KS(A, 1, 0) Only returns optimal profit!
9
Optimal Substructure Faster than brute force? Generally need optimal substructure Optimal substructure Optimal solution can be obtained by extending optimal solution of a subproblem Subproblems Generally: Problems remaining after making a choice One subproblem for each option of the choice Try extending solution of subproblem with choice Optimal substructure ➨ optimal solution is one of the extensions Sometimes problem must be generalized to describe subproblems
10
Greedy Algorithms Greedy algorithms For each choice, only consider “best” option Only one subproblem to consider No exponential growth ➨ fast! To keep in mind Requires optimal substructure Must prove correctness of greedy choice Usually doesn’t work! Hint Try to discover structure of optimal solutions What properties do optimal solutions have?
11
Greedy Pitfall Greedy choice? Most value per weight first? Doesn’t work! Greedy choice: item 1 ➨ profit = 21 Better: items 2 & 3 ➨ profit = 23 item123456 weight5758116 profit67410145 W = 18 item123456 weight7634118 profit211588149 W = 9
12
Proving greedy Greedy algorithms Can seem correct Usually are not Must prove correctness! Correctness greedy algorithm Prove optimal substructure (often true) Prove greedy choice (often not true) Show there exists an optimal solution that agrees with greedy choice Assume an optimal solution without greedy choice … … show how to adapt it to include greedy choice (and still optimal) Don’t need to write down proof! Just be properly convinced.
13
Fractional knapsack Can take part of an item Solution: how much of each item? Greedy strategy Take as much of item with highest profit to weight ratio as still fits Correctness Assume item i has max. profit/weight = x Assume OPT uses y weight less of item i then greedy choice Take OPT and arbitrarily remove y weight Replace with y weight of item i Removed ≤ y * x, added y * x ➨ optimal item123456 weight5758116 profit67410145 W = 18
14
Dynamic programming Terrible name… Also requires optimal substructure Avoid recomputing subproblems by storing solution Running time depends on number of subproblems To keep in mind Must make choices so that subproblems are easily encoded Tries all choices, so is often correct May have to generalize problem Sometimes requires clever tricks Very common in programming contests!
15
Dynamic programming steps General Approach 1. Determine which choices need to be made (many options!) 2. Find an efficient way to encode all subproblems 3. Let F describe the optimal cost for each subproblem 4. Come up with recursive relation for F (based on choices) 5. Determine order of subproblems (subproblems < problem) 6. Compute solutions for problems without subproblems 7. Use recursive relation to solve all problems in order Implementation Store optimal costs of subproblems in table (multi-dim array) Running time: (size of table) x (time to compute one cell) To obtain actual solution: use additional table with choices
16
Knapsack DP Choices? Item is in or out (start with last item) Subproblems? F(i, w) = optimal profit with first i items and capacity w Recurrence? F(i, w) = a) F(i-1, w)if w < w i b) max(F(i-1, w), F(i-1, w-w i ) + p i ) if w i ≤ w Order? Increasing in i Small subproblems F(0, w) = 0 for all 0 ≤ w ≤ W item123456 weight5758116 profit67410145 W = 18 Optimal cost in F(n, W) For 0 ≤ i ≤ n and 0 ≤ w ≤ W
17
Memoization If you don’t know order Or order is hard to compute Use memoization Note: there IS an order (or it won’t work) How does it work? Initially table is filled with -1 (or a value that cannot occur) F is computed using recursive algorithm For every call, first check if value in table is -1 … If not, return value in table Otherwise, compute value with recurrence and recursive calls Note: can be problem with recursion depth! Try to avoid…
18
TSP DP? Choices Assume we start at A What is 2 nd city we visit? 3 rd ? Subproblems? We are left with a subset of the cities F(i, S) = shortest tour from city i to city 1 visiting all cities in S Representing subset with bitstring Advanced technique… A B C D E F ABCDEF A06 B09 C60 D90 E04 F40
19
Decision Problems Decision Problem Determine if a valid solution exists No cost function Optimization Problem ➨ Decision Problem Consider an optimization problem with cost function f Decision problem: Is there a (valid) solution x with f(x) ≤ k? Perform binary search on k to find optimum Hint Decision problems are often easier Always try if a binary search on the cost makes problem easier!
20
Practice Brute force / Backtracking NWERC 2005 – Bowl Stack BAPC 2011 D – Bad Wiring EAPC 2012 B – Bad Scientist Greedy EAPC 2014 H – Talent selection Dynamic Programming EAPC 2005 H – Venus Rover EAPC 2014 D – Lift problems EAPC 2012 E – Extreme shopping EAPC 2011 J – Shuriken Game EAPC 2011 D – Polly wants a cracker
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.