Category:Memoization: Difference between revisions

added Encyclopedia tag
(Short writeup on memoization)
 
(added Encyclopedia tag)
 
(6 intermediate revisions by 3 users not shown)
Line 1:
Memoization is a method used to reduce function calls in recursive functions or other functions that are called very frequently. FunctionsThe whichbasic canidea bebehind memoizedmemoizing areis onesto thatstore give the same answerresults for anew setsets of inputs each(typically timein thosea inputskey-value arestyle) used.so [[Fibonaccithat sequence|Fibonaccithe numberfunction functions]]will arenot often memoizedhave to reducere-compute theirthose callresults treeslater andwhen calculationthe timessame overinputs time.are Theused basic(either operationin ofanother a memoizeddirect function wouldcall lookor somethinga likesubsequent this:recursive call).
 
Functions which can be memoized are ones that give the same answer for a set of inputs each time those inputs are used. [[Fibonacci sequence|Fibonacci number functions]] are often memoized to reduce their call trees and calculation times over time. The basic operation of a memoized function would look something like this:
<pre>function a with inputs
if inputs have been seen before
Line 5 ⟶ 7:
else
compute the answer for inputs
store that (inputs, answer) pair
return that answer
end function</pre>
Some programs may negate the condition in the "if" and swap the operations.
Some programs may negate the condition in the "if" and swap the operations. The overall benefit is that a function frequents called with the same set of inputs can save time by remembering the answer after computing it once -- sacrificing memory for computation time. In systems where memory (or storage depending on the implementation of storing old results) comes at a premium, memoization is not a good option. As long as memory is available and input sets are used repeatedly, memoization can save lots of computation time.
 
Some programs may negate the condition in the "if" and swap the operations. The overall benefit is that a function frequentsfrequently called with the same set of inputs can save time by remembering the answer after computing it once -- &mdash;sacrificing memory for computation time. In systems where memory (or storage depending on the implementation of storing old results) comes at a premium, memoization is not a good option. As long as memory is available and input sets are used repeatedly, memoization can save lots of computation time.
[[Category:Classic CS problems and programs]] [[Category:Encyclopedia]]
1,150

edits