39
CS 231: Algorithmic Problem Solving Naomi Nishimura Module 8 Date of this version: July 19, 2018 WARNING: Drafts of slides are made available prior to lecture for your convenience. After lecture, slides will be updated to reflect material taught. Check the date on this page to make sure you have the correct, updated version. WARNING: Slides do not include all class material; if you have missed a lecture, make sure to find out from a classmate what material was presented verbally or on the board. CS 231 Module 8 Compromising on correctness 1 / 39

CS 231: Algorithmic Problem Solvingcs231/resources/231...Using ratio bounds to assess approximation algorithms An approximation algorithm has a ratio bound of r(n) if for C the value

Embed Size (px)

Citation preview

CS 231: Algorithmic Problem SolvingNaomi Nishimura

Module 8Date of this version: July 19, 2018

WARNING: Drafts of slides are made available prior to lecture foryour convenience. After lecture, slides will be updated to reflectmaterial taught. Check the date on this page to make sure youhave the correct, updated version.

WARNING: Slides do not include all class material; if you havemissed a lecture, make sure to find out from a classmate whatmaterial was presented verbally or on the board.

CS 231 Module 8 Compromising on correctness 1 / 39

Ways of compromising on correctness

Although correctness might not be guaranteed, one might ask for any ofthe following guarantees.

Type of guaranteeA guarantee on how close thesolution is to the optimal solution

A guarantee on the probability thatthe solution is correct

No guarantee

Type of solutionApproximation algorithm

Monte Carlo algorithm(randomized)

Heuristic (deterministic orrandomized)

CS 231 Module 8 Compromising on correctness 2 / 39

Approximation algorithms

An approximation algorithm is an algorithm with guarantees.

• It will produce a feasible solution.

• There is a provable bound on how close the output is to the optimalsolution.

Note: In comparing two polynomial-time approximation algorithms, thecloseness of the approximation is usually more important than thedifference in the running times.

CS 231 Module 8 Approximation algorithms 3 / 39

Using ratio bounds to assess approximation algorithms

An approximation algorithm has a ratio bound of ρ(n) if for C the valueof the solution obtained and C ∗ the value of the optimal solution,max{ C

C ∗ ,C ∗

C } ≤ ρ(n).

Notes:

• The first term is used for a minimization problem and the second fora maximization problem.

• Other terms used: factor ρ(n) approximation, performance ratioof ρ(n)

• APX is the class containing all problems that can be solved by apolynomial-time factor r approximation for some value of r .

CS 231 Module 8 Approximation algorithms 4 / 39

Using relative error bounds to assess approximationalgorithms

An approximation algorithm has a relative error bound of ε(n) if|C−C ∗|

C ∗ ≤ ε(n), where C is the value of the solution obtained and C ∗ is thevalue of the optimal solution.

Such an algorithm is called an ε(n)-approximation algorithm.

Relation between ratio bound and relative error bound: ε(n)≤ ρ(n)−1

CS 231 Module 8 Approximation algorithms 5 / 39

Analyzing an approximation algorithm

Recipe for analyzing an approximation algorithm:

1. Choose a new measure or problem.

2. Bound the approximate solution in terms of the new measure orproblem.

3. Bound the optimal solution in terms of the new measure or problem.

4. Combine the two results to relate the approximate and optimalsolutions.

For a maximization problem, bound the approximate solution below andthe optimal solution above.

For a minimization problem, bound the approximate solution above andthe optimal solution below.

CS 231 Module 8 Approximation algorithms 6 / 39

Scheduling jobs

For each i , 1≤ i ≤m, the total time of all the jobs assigned to machine iis load(i).The makespan is max1≤i≤n load(i).

Job Scheduling

Input: A set of jobs where job j takes time time(j)

Output: An assignment of jobs to m machines such that themakespan is minimized.

We use n to denote the number of jobs, where the jobs are 1, . . .n.

It is possible to show the decision version of the problem is NP-complete,even when there are only two machines, by using a reduction fromPartition.

CS 231 Module 8 Approximation algorithms 7 / 39

Approximation algorithm for Job Scheduling

Algorithm:

• Order the jobs arbitrarily.

• Assign the next job to a machine with the smallest load so far.

Analysis:

• Running time: Process each job in polynomial time (possibly usingdata structures seen in CS 234).

• Correctness (feasible solution): Each job is assigned.

• Ratio bound: Use the recipe

CS 231 Module 8 Approximation algorithms 8 / 39

Relating the optimal solution to other measures

For optimal makespan M∗:

• M∗ ≥max1≤j≤n time(j)

• M∗ ≥ 1m ∑1≤j≤n time(j)

CS 231 Module 8 Approximation algorithms 9 / 39

Relating the approximate solution to other measures

Suppose that machine k has the largest load and that job j was the lastjob assigned to it; then the approximate solution MA = load(k).

We know that when j was assigned, the load of k was no greater than thatof any other machine.

Thus, for all i , load(k)− time(j)≤ load(i).

Since it is less than any one load, it is less than the average load, orload(k)− time(j)≤ 1

m ∑1≤i≤m load(i)

The sum of the loads is the the same as the sum of the times, soload(k)− time(j)≤ 1

m ∑1≤j≤n time(j)

MA = load(k) = (load(k)− time(j)) + time(j)≤ 2M∗

CS 231 Module 8 Approximation algorithms 10 / 39

Greedy algorithm for Vertex Cover

Algorithm:

• Choose a vertex with maximum degree.

• Add the vertex to the vertex cover.

• Remove the vertex and all incident edges.

• Repeat until no edges left.

CS 231 Module 8 Approximation algorithms 11 / 39

Approximation algorithm for Vertex Cover

Algorithm:

• Choose any edge.

• Add both endpoints of the edge to the vertex cover.

• Remove all edges incident on either end point.

• Repeat until no edges are left.

Analysis:

• Running time: Worst case O(m)

• Correctness (feasible solution): Edges removed only when covered

• Ratio bound: Use the recipe

CS 231 Module 8 Approximation algorithms 12 / 39

Proving a ratio bound for Vertex Cover approximation

1. Choose a new measure orproblem.

2. Bound the approximatesolution in terms of the newmeasure or problem.

3. Bound the optimal solution interms of the new measure orproblem.

4. Combine the two results torelate the approximate andoptimal solutions.

• |E |, where E is the set of edgeschosen by our algorithm.

• The size of the approximatesolution is 2|E |, since wechoose both endpoints.

• Any vertex cover must includeat least one endpoint fromeach edge in E . Since theedges in E do not share anyendpoints, any vertex cover,including the optimal vertexcover is of size at least |E |.• C

C ∗ ≤2|E ||E | = 2

CS 231 Module 8 Approximation algorithms 13 / 39

Euclidean (or Metric) TSP

Special case: The weights satisfy the triangle inequality: for w(u,v) theweight of edge (u,v), w(u,v)≤ w(u,x) +w(x ,v) for all x .

Algorithm:

• Compute a minimum spanning tree.

• Keep track of the vertices encountered.

• Remove all but the first time a vertex appears.

• Add an edge back to the first vertex to form a cycle.

CS 231 Module 8 Approximation algorithms 14 / 39

Euclidean TSP example

Sequence based on the minimum spanning tree:abacecfcadghgd

First time each vertex appears:abacecfcadghgd

Cycle formed:

a

b c d

e f g

h

CS 231 Module 8 Approximation algorithms 15 / 39

Proving a ratio bound for TSP approximation

1. Choose a new measure orproblem.

2. Bound the approximatesolution in terms of the newmeasure or problem.

3. Bound the optimal solution interms of the new measure orproblem.

4. Combine the two results torelate the approximate andoptimal solutions.

• M, the cost of a minimumspanning tree for the graph

• The cost of the approximatesolution is at most 2M(explained later).

• The cost of the optimalsolution is at least M(explained later).

• CC ∗ ≤

2MM = 2

CS 231 Module 8 Approximation algorithms 16 / 39

Details

Why is the cost of the approximate solution at most 2M?

• The cost of the “full walk” around the tree consists using each edgein the minimum spanning tree twice, for a total of 2M.

• To convert the “full walk” to a cycle, we repeatedly remove vertices.

• Each time we remove a vertex, we are replacing two edges by onethat forms a triangle. This does not increase the total cost.

Why is the cost of the optimal solution at least M?

• The cost of the optimal solution is at least the cost of the cycle minusone edge.

• The cycle minus one edge is a spanning tree, with cost at least thecost of the minimum spanning tree, M.

CS 231 Module 8 Approximation algorithms 17 / 39

Other results using approximation

Approximation schemes: Results showing the existence of a family ofapproximation algorithms, where you can specify the desired relative errorbound.

Inapproximability results: Results showing that if there is a certain type ofapproximation algorithm for a problem, then P = NP.

CS 231 Module 8 Approximation algorithms 18 / 39

Approximation schemes

There may be a tradeoff between the relative error bound and runningtime.

Consider an algorithm that takes as input an instance and an ε such thatfor any fixed ε, it performs as an approximation algorithm with relativeerror bound ε.

• The algorithm is a polytime approximation scheme if for any fixedε, the running time is polynomial in the size of the input.

• The algorithm is a fully polytime approximation scheme if therunning time is polynomial in both 1/ε and n.

CS 231 Module 8 Approximation algorithms 19 / 39

Inapproximability of TSP

Claim: If TSP is in APX, then P=NP.

We assume that there is a polynomial-time factor r approximation of TSPand show that there exists a polynomial-time algorithm for HamiltonianCycle (HC).

Given an instance G of instance of HC, for any r ≥ 1, we create aninstance G ′ of TSP such that if there exists a polynomial-timer -approximation of TSP, then we can solve HC on G in polynomial time.(Details to follow.)

Because HC is NP-complete, if HC is in P, then P = NP.

CS 231 Module 8 Approximation algorithms 20 / 39

Details of the proof

We create a complete graph G ′ such that V (G ′) = V (G ) and each edgehas weight either 1, if it is in E (G ), or 1 + rn otherwise.

Then:

• If G has a Hamiltonian cycle, then the smallest tour in G ′ has cost n.

• The next-smallest solution will have cost at least(n−1) + 1 + rn = (r + 1)n, and hence the approximation ratio will beat least r + 1.

• The smallest solution for a no-instance will also be of size at least(r + 1)n.

Thus, a factor r approximation algorithm will return a solution of size atmost rn if and only if G is a yes-instance of HC.

Because the approximation algorithm runs in polynomial time, we nowhave a polynomial-time algorithm that solves HC.

CS 231 Module 8 Approximation algorithms 21 / 39

Monte Carlo algorithms

A Monte Carlo algorithm is guaranteed to run in time polynomial in thesize of the input, and gives the correct answer with high probability. (Weare not going to go into the specifics of the meaning of “high probability”in this course.)

For a decision problem, there may be two-sided error (both false positivesand false negatives are possible) or one-sided error (just false positives orjust false negatives, not both).

• It is possible to convert a Las Vegas algorithm to a Monte Carloalgorithm by stopping after polynomial time.

• There is no known general way to convert a Monte Carlo algorithm toa Las Vegas algorithm.

CS 231 Module 8 Monte Carlo algorithms 22 / 39

Pros and cons of randomized algorithms

Pros:

• With luck, it can be faster than a deterministic algorithm.

• A randomized algorithm may be simpler than a deterministicalgorithm.

• Different answers on different runs may allow repeated runs toincrease confidence in an answer.

Cons:

• You may give up guarantees on worst-case time or correctness.

• Debugging is difficult, as there may be different outputs on differentruns.

• Analysis is often difficult.

CS 231 Module 8 Monte Carlo algorithms 23 / 39

Heuristic methods for optimization problems

Heuristics and approximation algorithms both come up with approximatesolutions.

A heuristic is a procedure without guarantees.

• It may or may not produce an optimal solution.

• It may or may not be fast.

We considered many greedy algorithms that are heuristics.

CS 231 Module 8 Heuristics 24 / 39

Hill climbing

Idea:

• Start with an arbitrary solution or build up a solution.

• Make small changes that improve the solution.

Note: No guarantees on running time or correctness.

This is a local search heuristic.

CS 231 Module 8 Heuristics 25 / 39

Hill climbing examples

TSP example (improving a solution):

• Start with an arbitrary ordering of vertices.

• Repeatedly: If changing the order of a pair of vertices makes the costsmaller, make that change.

Partition example (building up a solution):

• Put the first item arbitrarily in one of the sets.

• Repeatedly: Put the next item in the set that minimizes thedifference in the sums of values.

Partition example (improving a solution):

• Start with an arbitrary partition.

• Repeatedly: If changing an item from one set to another makes thedifference of sum of values smaller, make that change.

CS 231 Module 8 Heuristics 26 / 39

Improvements on hill climbing

• Use steepest ascent, which chooses the option that gives the bestimprovement.

• Sort the items before placing them in building up a solution (e.g. innonincreasing order of weights).

Problem: Good for finding a local optimum, but not necessarily a globalone.

CS 231 Module 8 Heuristics 27 / 39

Randomness in heuristics

Ways to use randomness:

• Randomly construct a solution

• Randomly choose an initial solution to be improved

• Randomly change a solution to form another solution

CS 231 Module 8 Heuristics 28 / 39

Simulated annealingWhen cooling molten materials, slow cooling forms a better end result:

• The energy of the system depends on the energy of all the particles inthe system.

• The state of each particle moves around randomly, based on thetemperature.

• The probability of moving to a state with lower energy is very high.

• The higher the temperature, the higher probability of moving into astate with higher energy.

Simulated annealing:

• Each solution to the problem has a cost that can be easily computed.

• There is a way of making small changes to a solution (e.g. insertingor deleting an item or swapping the order of a pair of items).

• Here “slow cooling” is a slow decrease in the probability of acceptingworse solutions (changes to the solution are initially large and thenbecome smaller and smaller).

CS 231 Module 8 Heuristics 29 / 39

Simulated annealing examples

TSP example:

• Cost of a solution is the cost of a tour

• Swap positions of two randomly chosen vertices

• Alternative: swap edges by randomly choosing two edges andreplacing them with ones with the same endpoints

Independent Set example:

• Cost of a solution is the size of the set or 0 if not independent

• Randomly add or delete a vertex

• Alternative cost: Subtract from the size of the set the number ofedges multiplied by the constant and divided by the temperature

Initially allows more edges so that graph may be nonempty, later makeedges less likely.

CS 231 Module 8 Heuristics 30 / 39

Other heuristicsTabu search

• If improvements are possible, allow some steps that make the solutionworse (this helps avoid getting stuck at a local maximum)

• Prohibitions are used to help prevent returning to a previous solution

Neural networks

• Simulate a brain by a graph in which vertices correspond to neuronsand weights indicate the strength of connections

• Train the network with positive and negative examples, adjustingweights as needed

Genetic algorithms

• Simulate the role of random mutations in evolution and naturalselection

• Random solutions from the population can reproduce, creating newsolutions, based on fitness

• Eventually unfit solutions die off

CS 231 Module 8 Heuristics 31 / 39

Algorithms and analysis for special situations

Different ways of viewing computation:

• Online algorithms (input is not available all at once)

• Parallel computation (multiple machines coordinate to split thework)

• Distributed computation (multiple machines pass messages)

Different ways of analyzing algorithms:

An output sensitive algorithm has a running time measured in terms ofnot only the size of the input but also the size of the output.

To determine the amortized cost of an algorithm, the worst case cost isdetermined for a worst-case sequence of steps rather than the worst casefor a single step.

CS 231 Module 8 Other types of problems and analysis 32 / 39

Complexity classes

• Polynomial-time hierarchy

• Fixed-parameter: W-hierarchy (FPT versus W[1]-hard)

• Approximation

• Randomized

• Parallel: NC (Nick’s class)

For each situation problems belong to various complexity classes, and foreach type of reduction, there are different notions of what is “easy” andwhat is “hard”.

Example:RP consists of decision problems that can be solved with one-sided errorin polynomial time. (Uses Monte Carlo algorithms.)

ZPP consists of decision problems that can be solved in expectedpolynomial time.

P ⊆ ZPP ⊆ RP ⊆ NP

CS 231 Module 8 Other types of problems and analysis 33 / 39

Online algorithms

An algorithm that has all the input before starting computation is offline;otherwise, it is online.

Idea: Compare an online algorithm to the results off the best offlinealgorithm for the same problem.

An online algorithm has competitive ratio c if the cost is at most c timesthe cost of the best offline algorithm for any input. The cost can berunning time or, in our example, other costs such as dollars spent.

The offline algorithm for the equipment question is rent if you need it 9times, and otherwise buy it.

CS 231 Module 8 Online algorithms 34 / 39

Determining the competitive ratio

Rent for the first five times, then buy

Competitive ratio: 3

Uses:35710

Offline:305070100

Online:30150150150

On/Off:1315/71.5

CS 231 Module 8 Online algorithms 35 / 39

Determining the competitive ratio

Rent for the first ten times, then buy

Competitive ratio: 2

Uses:35710

Offline:305070100

Online:305070200

On/Off:1112

CS 231 Module 8 Online algorithms 36 / 39

Bin Packing

Bin Packing

Input: A group of n objects, where object i has size si ≤ 1

Output: An assignment of objects to bins, where the sum of the sizesof the objects in a bin is at most 1, and the total number ofbins is as small as possible

Offline version: All objects are known in advance.

Online version: Objects appear one at a time and must be assigned asthey appear. No changes can be made to assignments.

CS 231 Module 8 Online algorithms 37 / 39

Next Fit algorithm for Bin Packing

Algorithm:

• Keep only one bin open at a time.

• If the next item fits, put it in the open bin.

• If the next item does not fit, close the bin, open a new one, and putthe next item in the open bin.

Claim: Next Fit has a competitive ratio of at most 2.

Idea: The best offline algorithm can match up big and little pieces to useup the space efficiently.

In Next Fit, big pieces will each get their own bin, but little pieces can stillbe packed together.

Example: There are 2N items of size alternating between 1/2 and 1/2N.

Offline: Pack N bins with two each of size 1/2 and 1 bin with the rest.

Online: Pack 2N bins with one of each size.

CS 231 Module 8 Online algorithms 38 / 39

Module summary

Topics covered:

• Approximation algorithms

• Monte Carlo algorithms

• Heuristics

• Other types of problems and analysis

• Online algorithms

CS 231 Module 8 Online algorithms 39 / 39