Amortized Analysis
Pedro Ribeiro
DCC/FCUP
2018/2019
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 1 / 30
Amortized Analysis Pedro Ribeiro DCC/FCUP 2018/2019 Pedro Ribeiro - - PowerPoint PPT Presentation
Amortized Analysis Pedro Ribeiro DCC/FCUP 2018/2019 Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 1 / 30 Amortized Analysis In amortized analysis we are concerned about the average over a sequence of operations Some operations
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 1 / 30
◮ Some operations may be costly, but others may be quicker, and in the
◮ Typically applied to the analysis of a data structure
◮ There might be correlations between operations and worst case per
◮ Amortized analysis still gives us a view of the worst possible scenario Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 2 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 3 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 4 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 5 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 6 / 30
◮ When I earn more than expenditures, I save some money for the future ◮ When I earn less than expenditures, I use the money I saved Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 7 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 8 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 9 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 10 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 11 / 30
◮ What would a (non amortized) regular worst case analysis say? ◮ ”Increase size by 100 when full” would not be a good strategy. Why? ◮ What about a strategy for saving (unused) space when popping?
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 12 / 30
◮ We spend that $1 for inserting (the ”insert-dollar”) ◮ When we need to copy this element to a new array (when resizing), we
◮ $1 for inserting (the ”insert-dollar”) ◮ $1 for copying to a new array (the ”copy-dollar”) ◮ When if we need to copy it again for a second time (during a new
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 13 / 30
◮ $1 for inserting (the ”insert-dollar”) ◮ $1 for copying to a new array (the ”copy-dollar”) ◮ $1 for recharging old elements that have spent their copy-dollars to a
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 14 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 15 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 16 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 17 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 18 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 19 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 20 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 21 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 22 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 23 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 24 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 25 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 26 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 27 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 28 / 30
◮ Some operations may be costly, but others may be quicker, and in the
◮ Typically applied to the analysis of a data structure
◮ There might be correlations between operations and worst case per
◮ Amortized analysis still gives us a view of the worst possible scenario Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 29 / 30
Pedro Ribeiro (DCC/FCUP) Amortized Analysis 2018/2019 30 / 30