12-memoization_ordered_lists_trees

12-memoization_ordered_lists_trees - Computer Science 136:...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 12 – Memoization and Ordered Lists We examine more closely the idea of accumulating multiple values and examine the costs of some algorithms. We also consider memoization , where computed properties of a data structure are stored within. Finally we look ordered lists, and basic operations for working with them and sorting them, CS 136 – Winter 2012 12 – Memoization, Ordered Lists 1/26 Review: Destructive vs. non-destructive Non-destructive list operations: An operation is non-destructive if it promises not to mutate its arguments Arguments passed to non-destructive operations remain valid Consider this specification of iappend : ilist iappend(ilist il1, ilist il2); // builds a new list with the elements // of il1 followed by the elements of il2 Builds a new list means: New memory may be allocated for the appended list Memory allocated to il1 and il2 is left intact CS 136 – Winter 2012 12 – Memoization, Ordered Lists 2/26 Review: Destructive vs. non-destructive (2) Destructive list operations: Destructive operations may mutate arguments in a precisely prescribed way Destructive operations may completely invalidate arguments arguments may point to freed memory upon completion – don’t use after calling! Destructive operations promise to recycle or free all memory which is left unreachable after the modifications Destructive operations generally pass back an address to the structure resulting from the operation CS 136 – Winter 2012 12 – Memoization, Ordered Lists 3/26 Summing elements of a list int sumlist(ilist lst) { if (iempty_huh(lst)) return 0; else return ifirst(lst) + sumlist(irest(lst)); } From what we now know about how local variables we can see that this allocates n + 1 stack frames for a list of length n ! This is not a tail recursion, so there is no obvious optimization the compiler can make. Accumulative recursion might avoid this: int sumlist_helper(ilist lst, int sumsofar) { if (iempty_huh(lst)) return sumsofar; else return sumlist_helper(irest(lst), ifirst(lst)+sumsofar); } Tail recursion: C “might” optimize to one stack frame CS 136 – Winter 2012 12 – Memoization, Ordered Lists 4/26 Play to C’s strengths Make a loop: int sumlist(ilist lst) { int sum=0; while (lst != NULL) { sum += ifirst(lst); } return sum; } Space required is O ( 1 ) Summary: Remembering previous computation saves resources In this case memory CS 136 – Winter 2012 12 – Memoization, Ordered Lists 5/26 Don’t forget our Racket skills ( define ( sumlist lst ) ( foldl + 0 lst )) Same result, but result is computed in a different way....
View Full Document

This note was uploaded on 02/22/2012 for the course CS cs136 taught by Professor Cormack during the Winter '10 term at Waterloo.

Page1 / 26

12-memoization_ordered_lists_trees - Computer Science 136:...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online