Algorithm Strategies
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
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
Greedy Algorithm - Examples
• Restaurants using plastic cups to save money
– Will initially save money – Will 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
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
Local vs. Global (minimum)
• Rolling a ball down al hill may arrive at a local
minimum
Greedy Algorithms
• Task scheduling
• Activity Selection
• Various AI algorithms
• Various Graph Theory Algorithms
• Heuristics that pick the best at the time
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
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
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
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?
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
Example
Approximate Bin Packing
• N items of sizes s1, s2, ..., sN
• 0 < si <= 1
• Goal: pack into fewest number of bins of size 1 • NP-complete problem, but we can use greedy
algorithms to produce near optimal solutions
• Knapsack problem
– Items have value
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
Online vs Offline Algorithms
• Online
– Process one item at a time – Process data as it comes in
– Must make a decision immediately
• Off-line
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
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
Online Bin Packing Algorithms
• Next fit • First fit
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.
Next Fit Example
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
First Fit Example
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
Best Fit Example
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
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
Divide And Conquer
• What are some divide and conquer algorithms
Divide And Conquer
• Covered
– Quicksort – Merge Sort – Binary Search
• Others
– Matrix Multiplication
Quicksort
• What are the steps? • Divide
– ?
• Conquer
– ?
• Combine
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
Merge Sort
• What are the steps? • Divide
– ?
• Conquer
– ?
• Combine
Merge Sort
• What are the steps? • Divide
– Recursively split each list into two equal-sized
sub-lists • Conquer
– Sort the sub-lists
• Combine
Binary Search
• What are the steps? • Divide
– ?
• Conquer
– ?
• Combine
Binary Search
• What are the steps? • Divide
– Check the middle element
• Conquer
– Search one sub-list
• Combine