Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void...

24
Presorting and Greedy Coins Class 18

Transcript of Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void...

Page 1: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

Presorting and Greedy Coins

Class 18

Page 2: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 3: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 4: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 5: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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?

Page 6: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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?

Page 7: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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?

Page 8: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 9: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 10: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 11: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 12: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 13: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 14: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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)

Page 15: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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)

Page 16: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 17: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 18: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 19: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 20: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 21: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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

Page 22: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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?

Page 23: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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?

Page 24: Presorting and Greedy Coinsborax.truman.edu/310/c18/presortcoin.pdf · An Algorithm void make_amount(amount, denom, quant) {quant = vector of 0’s index = denom.size() - 1 while

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?