• No results found

AlgorithmStrategies

N/A
N/A
Protected

Academic year: 2020

Share "AlgorithmStrategies"

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)

Algorithm Strategies

(2)

Greedy Algorithms

Make best choice based on some short term

criteria

• Choice is optimal at each step of the algorithm

Final solution may not be optimal

Choosing a non-optimal solution to start may lead

(3)

Greedy Algorithms

Must have

Greedy-choice property: a globally optimal solution

can be arrived at by making a locally optimal choice

Optimal substructure: an optimal solution to a

(4)

Greedy Algorithm - Examples

Restaurants using plastic cups to save money

Will initially save moneyWill turn away customers

Will lose money overall when customers are lost

• Protecting a piece in chess that will be lost no matter what

– Keep sacrificing pieces to save the queen

Will lose queen anyway

(5)

Greedy Algorithm Properties

Fast and simple • Used in heuristics

Heuristic vs. Algorithm

• May not give overall optimal solution

May find local maximum/minimum but not global

(6)

Local vs. Global (minimum)

Rolling a ball down al hill may arrive at a local

minimum

(7)

Greedy Algorithms

• Task scheduling

• Activity Selection

• Various AI algorithms

Various Graph Theory Algorithms

• Heuristics that pick the best at the time

(8)

Task Scheduling

• Given jobs j1, j2, j3, ..., jn with known running times t1, t2, t3, ..., tn – what is the best way to schedule the jobs to minimize average completion time?

Job Time

j1 15

j2 8

j3 3

(9)

Scheduling

Average completion time = (15+23+26+36)/4 = 25

Average completion time = (3+11+21+36)/4 = 17.75

Job Name J1 J2 J3 J4

Completion

Time 15 23 26 36

Job Name J3 J2 J4 J1

Completion

(10)

Scheduling

• Greedy-choice property:

– If the shortest job (j3) does not go first

• All the other jobs before the first job will complete 3 time units faster

• j3 will be postponed by time it took to complete all the other jobs that ran before j3

• Optimal substructure:

– If the shortest job is removed from optimal solution

• The remaining solution for the other jobs is optimal

(11)

Activity Selection Problem

Given a time frame and a set of mutually

exclusive activities and their start and end

times, what is the largest number of activities that can be run in the time frame?

(12)

Activity Selection Problem

Sort the activities by earliest finish time

• Choose the activities by earliest finish time

• Keep choosing non-overlapping activities

• Stop when the time frame is reached

(13)

Example

(14)

Approximate Bin Packing

• N items of sizes s1, s2, ..., sN

• 0 < si <= 1

Goal: pack into fewest number of bins of size 1NP-complete problem, but we can use greedy

algorithms to produce near optimal solutions

Knapsack problem

– Items have value

(15)

Example – Optimal Packing

0.8

0.2

0.3

0.7

0.5

0.4 0.1

Input: 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8

(16)

Online vs Offline Algorithms

Online

Process one item at a timeProcess data as it comes in

Must make a decision immediately

• Off-line

(17)

Online Bin-Packing

Look at each item one at a time in order

• Decide where to put that item before looking at the next item

(18)

Online Bin-Packing

• Online algorithms cannot guarantee optimal solution

– Problem: cannot know when input will end

– M small items ½-ε – M large items ½+ε

– Can fit into M bins with 1 large and 1 small in each bin

– If all small items come first, place in M separate bins

– If input is only M small items, we have used twice as many bins as necessary

– There are inputs that force any online bin-packing

(19)

Online Bin Packing Algorithms

Next fit • First fit

(20)

Next Fit

• Algorithm

– If the item fits in the current bin, put it there

– Otherwise place it in a new bin

– Never look back at previous bins • Running time = O(n)

• Let M be the optimal number of bins required to pack a list of items. Then next fit never

uses more than 2M bins.

(21)

Next Fit Example

(22)

First Fit

• Algorithm

– Scan all bins and place the item in first bin large enough to hold it

– If no bin is large enough, create a new bin

• Running time = O(nlog(n))

– n = number of items

– log(n) = upper bound on search time

• Assumes you store the bins in sorted order based on capacity • Let M be the optimal number of bins required to

(23)

First Fit Example

(24)

Best Fit

Algorithm

Scan all bins and place the item in the bin with

tightest fit

If no bin is large enough, create a new bin

(25)

Best Fit Example

(26)

Offline Bin Packing

Sort items in decreasing order for easier

placement of large items

• Apply first fit or best fit algorithm

Let M be the optimal number of bins required

(27)
(28)

Divide and Conquer

Divide

Divide the problem into smaller sub-problems

Conquer

Solve the sub-problems

Can be solved by breaking them up into more

sub-problems to solve • Combine

(29)

Divide And Conquer

What are some divide and conquer algorithms

(30)

Divide And Conquer

Covered

QuicksortMerge SortBinary Search

• Others

Matrix Multiplication

(31)

Quicksort

What are the steps? • Divide

?

• Conquer

?

• Combine

(32)

Quicksort

What are the steps?

• Divide

– Partition into two sub groups

One greater than the pivot

– One less than or equal to the pivot • Conquer

Recursively sort the sub-lists

• Combine

(33)

Merge Sort

What are the steps? • Divide

?

• Conquer

?

• Combine

(34)

Merge Sort

What are the steps? • Divide

Recursively split each list into two equal-sized

sub-lists • Conquer

Sort the sub-lists

• Combine

(35)

Binary Search

What are the steps? • Divide

?

• Conquer

?

• Combine

(36)

Binary Search

What are the steps? • Divide

Check the middle element

• Conquer

Search one sub-list

• Combine

References

Related documents

Intermediate Pheromone Update Global Pheromone Update Next Location Solution All Tours Local Construction Phase Global Reinforcement Phase Local Reinforcement Phase

Report and Stop Report and Stop Yes Unbounded Infeasible Integer Yes Store Solution Update Best Remove Nodes With worse Obj Yes.!. Local/Global

Standards Real time data Reporting Availability Management Global Global Global Incident Management Global Global Global Change Management Global Local Local Capacity Management

The solution to improving patient throughput time with this variability is to eliminate as much non-value added time as possible and to put the value added steps in the

Given a preferred running time and the number of solutions to evaluate at step 1 (updated each time a new best solution is found), it is possible to calculate an

Since we only guess the start time of a constant number of jobs, a polynomial number of guesses is enough and we return the best solution found.. List scheduling

The best description (global minimum measured by NRS) was found using a simplex start with dimensional constraints imposed slowly by first finding the maximum dimensional solution

In this paper TLBO is used as a global search and the best solution of TLBO is taken as initial condition for SQP method for fine tune solution which is a local