Skip to main content

Tangled Dependencies and Memoization

  • Chapter
  • First Online:
Python Algorithms
  • 14k Accesses

Abstract

Twice , adv. Once too often.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 79.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 99.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    This was the year the first FORTRAN compiler was released by John Backus’s group. Many consider this the first complete compiler, although the first compiler ever was written in 1942, by Grace Hopper.

  2. 2.

    See Richard Bellman on the Birth of Dynamic Programming in the references.

  3. 3.

    Some definitions start with zero and one. If you want that, just use return i instead of return 1. The only difference is to shift the sequence indices by one.

  4. 4.

    https://pypi.python.org/pypi/functools32/3.2.3

  5. 5.

    That is memo-ized, not memorized.

  6. 6.

    The use of the wraps decorator from the functools module doesn’t affect the functionality. It just lets the decorated function (such as fib) retain its properties (such as its name) after wrapping. See the Python docs for details.

  7. 7.

    This is still just an example for illustrating the basic principles.

  8. 8.

    For example, this “In or not?” approach is used in solving the knapsack problem, later in this chapter.

  9. 9.

    This approach is also closely related to Prim’s and Dijkstra’s algorithms, as well as the Bellman-Ford algorithm (see Chapters 7 and 9).

  10. 10.

    Actually, for the longest increasing subsequence problem, we’re looking for the longest of all the paths, rather just the longest between any two given points.

  11. 11.

    This devilishly clever little algorithm was first was first described by Michael L. Fredman in 1975.

  12. 12.

    Using Skywalker here gives the slightly less interesting LCS Sar.

  13. 13.

    Normally, of course, induction works on only one integer variable, such as problem size. The technique can easily be extended to multiple variables, though, where the induction hypothesis applies wherever at least one of the variables is smaller.

  14. 14.

    You could preallocate the list, with m = [0]*(c+1), if you prefer, and then use m[r] = val instead of the append.

  15. 15.

    The object index i = k-1 is just a convenience. We might just as well write m(k,r) = v[k-1] + m(k-1,r-w[k-1]).

  16. 16.

    If parsing is completely foreign to you, feel free to skip this bullet point. Or perhaps look into it?

  17. 17.

    You can find more information about optimal search trees both in Section 15.5 in Introduction to Algorithms by Cormen et al., and in Section 6.2.2 of The Art of Computer Programming, volume 3, “Sorting and Searching,” by Donald E. Knuth (see the “References” section of Chapter 1).

  18. 18.

    You could certainly design some sort of cost function so this wasn’t the case, but then we couldn’t use dynamic programming (or, indeed, recursive decomposition) anymore. The induction wouldn’t work.

  19. 19.

    You should have a whack at the matrix chains yourself (Exercise 8-18), and perhaps even the parsing, if you’re so inclined.

  20. 20.

    www-igm.univ-mlv.fr/∼lecroq/seqcomp

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2014 Magnus Lie Hetland

About this chapter

Cite this chapter

Hetland, M.L. (2014). Tangled Dependencies and Memoization. In: Python Algorithms. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-0055-1_8

Download citation

Publish with us

Policies and ethics