Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void...
Transcript of Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void...
Presorting and Greedy Coins
Class 18
Search a List
• search an unsorted list for an element• analyze two strategies:
1. search the unsorted list to see if the element exists2. sort the list first, then search to see if the element exists
Strategy 1 Strategy 2(unsorted) (sort first)
worst case
T (n) ∈ O(n) T (n) ∈ O(n lg n)
best case
T (n) ∈ Ω(1) T (n) ∈ Ω(n lg n)
• clearly, strategy 1 is superior
Search a List
• search an unsorted list for an element• analyze two strategies:
1. search the unsorted list to see if the element exists2. sort the list first, then search to see if the element exists
Strategy 1 Strategy 2(unsorted) (sort first)
worst case T (n) ∈ O(n) T (n) ∈ O(n lg n)best case T (n) ∈ Ω(1) T (n) ∈ Ω(n lg n)
• clearly, strategy 1 is superior
Search a List
• search an unsorted list for an element• analyze two strategies:
1. search the unsorted list to see if the element exists2. sort the list first, then search to see if the element exists
Strategy 1 Strategy 2(unsorted) (sort first)
worst case T (n) ∈ O(n) T (n) ∈ O(n lg n)best case T (n) ∈ Ω(1) T (n) ∈ Ω(n lg n)
• clearly, strategy 1 is superior
Multiple Runs
• now assume we are not going to do just one search• assume we are going to do many (m) searches• what is the analysis over m searches?
Strategy 1 Strategy 2(unsorted) (sort first)
worst case
T (m, n) ∈ O(mn) T (m, n) ∈ O(m lg n + n lg n)
best case
T (m, n) ∈ Ω(m) T (m, n) ∈ Ω(m + n lg n)
• what value of m is the break-even point?
Multiple Runs
• now assume we are not going to do just one search• assume we are going to do many (m) searches• what is the analysis over m searches?
Strategy 1 Strategy 2(unsorted) (sort first)
worst case T (m, n) ∈ O(mn) T (m, n) ∈ O(m lg n + n lg n)best case T (m, n) ∈ Ω(m) T (m, n) ∈ Ω(m + n lg n)
• what value of m is the break-even point?
Multiple Runs
• now assume we are not going to do just one search• assume we are going to do many (m) searches• what is the analysis over m searches?
Strategy 1 Strategy 2(unsorted) (sort first)
worst case T (m, n) ∈ O(mn) T (m, n) ∈ O(m lg n + n lg n)best case T (m, n) ∈ Ω(m) T (m, n) ∈ Ω(m + n lg n)
• what value of m is the break-even point?
Presorting
• sometimes a job is easier if the values are known to be sorted• check if a value is unique• find the mode of a list of values• find the median of a list of values
Transform and Conquer
• a name for this strategy transform and conquer• modify the arrangement of the data in the data structure to
improve either the ease or speed of solution
Purchase with Change
• you go into a store and buy a small candy bar• the clerk asks you for 57¢• you hand the clerk a $1 bill• what does the clerk do next?
• there are 31 different ways to make 43¢using U.S. coinsQuarter Dime Nickel Penny
0 0 0 430 3 1 80 3 2 30 4 0 31 0 0 181 1 1 31 0 2 81 0 3 3
Purchase with Change
• you go into a store and buy a small candy bar• the clerk asks you for 57¢• you hand the clerk a $1 bill• what does the clerk do next?
• there are 31 different ways to make 43¢using U.S. coinsQuarter Dime Nickel Penny
0 0 0 430 3 1 80 3 2 30 4 0 31 0 0 181 1 1 31 0 2 81 0 3 3
Coins
• assume we have an array with the coin amounts
1¢ 5¢ 10¢ 25¢denom
• and we wish to fill an array with the quantities of eachdenomination
quant
• write a function for making an amount of money with coins
Coins
• assume we have an array with the coin amounts
1¢ 5¢ 10¢ 25¢denom
• and we wish to fill an array with the quantities of eachdenomination
quant
• write a function for making an amount of money with coins
An Algorithmunsigned make_amount(amount, denom, quant)
unsigned total_coins = 0;size_t index = denom.size() - 1;while (amount > 0)
while (amount >= denom.at(index))
quant.at(index)++;amount -= denom.at(index);total_coins++;
index--;
return total_coins;
analysis:
T (n) ∈ Θ(n)
An Algorithmunsigned make_amount(amount, denom, quant)
unsigned total_coins = 0;size_t index = denom.size() - 1;while (amount > 0)
while (amount >= denom.at(index))
quant.at(index)++;amount -= denom.at(index);total_coins++;
index--;
return total_coins;
analysis: T (n) ∈ Θ(n)
Greedy Coins
• the coin algorithm is an example of a greedy algorithm
• a greedy algorithm builds a solution in successive identicalsteps• at each step, an action is done to make progress toward the
goal without considering future consequences• the action taken makes the greatest amount of progress
currently possible (this is why it’s called greedy)• at each step, the same greedy choice rule is used to decide the
action• when the algorithm terminates, if the locally optimal choices
combine to form a globally optimal solution, then thealgorithm is valid
Greedy Coins
• the coin algorithm is an example of a greedy algorithm• a greedy algorithm builds a solution in successive identical
steps
• at each step, an action is done to make progress toward thegoal without considering future consequences• the action taken makes the greatest amount of progress
currently possible (this is why it’s called greedy)• at each step, the same greedy choice rule is used to decide the
action• when the algorithm terminates, if the locally optimal choices
combine to form a globally optimal solution, then thealgorithm is valid
Greedy Coins
• the coin algorithm is an example of a greedy algorithm• a greedy algorithm builds a solution in successive identical
steps• at each step, an action is done to make progress toward the
goal without considering future consequences
• the action taken makes the greatest amount of progresscurrently possible (this is why it’s called greedy)• at each step, the same greedy choice rule is used to decide the
action• when the algorithm terminates, if the locally optimal choices
combine to form a globally optimal solution, then thealgorithm is valid
Greedy Coins
• the coin algorithm is an example of a greedy algorithm• a greedy algorithm builds a solution in successive identical
steps• at each step, an action is done to make progress toward the
goal without considering future consequences• the action taken makes the greatest amount of progress
currently possible (this is why it’s called greedy)
• at each step, the same greedy choice rule is used to decide theaction• when the algorithm terminates, if the locally optimal choices
combine to form a globally optimal solution, then thealgorithm is valid
Greedy Coins
• the coin algorithm is an example of a greedy algorithm• a greedy algorithm builds a solution in successive identical
steps• at each step, an action is done to make progress toward the
goal without considering future consequences• the action taken makes the greatest amount of progress
currently possible (this is why it’s called greedy)• at each step, the same greedy choice rule is used to decide the
action
• when the algorithm terminates, if the locally optimal choicescombine to form a globally optimal solution, then thealgorithm is valid
Greedy Coins
• the coin algorithm is an example of a greedy algorithm• a greedy algorithm builds a solution in successive identical
steps• at each step, an action is done to make progress toward the
goal without considering future consequences• the action taken makes the greatest amount of progress
currently possible (this is why it’s called greedy)• at each step, the same greedy choice rule is used to decide the
action• when the algorithm terminates, if the locally optimal choices
combine to form a globally optimal solution, then thealgorithm is valid
Greedy Coins
make 12¢ with these denominations
1¢ 6¢ 10¢denom
• we use the same greedy algorithm• but we do not get the optimal solution• validity depends on both the algorithm and the input
• how would you make amounts optimally with thesedenominations?
Greedy Coins
make 12¢ with these denominations
1¢ 6¢ 10¢denom
• we use the same greedy algorithm• but we do not get the optimal solution• validity depends on both the algorithm and the input
• how would you make amounts optimally with thesedenominations?
Greedy Coins
make 12¢ with these denominations
1¢ 6¢ 10¢denom
• we use the same greedy algorithm• but we do not get the optimal solution• validity depends on both the algorithm and the input
• how would you make amounts optimally with thesedenominations?