Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Parallel

Dynamic Programming In Action


Knowing AI programming techniques is not sufficient if you want to do either AI research or write AI applications. Even graduate students in AI ignore the fundamentals -- data structure and algorithm designs and complexity analysis -- much more than they should. Further, it is insufficient to know that reference books with these topics are available; you need to know about the techniques to know when and where to look in these references.

One place where lack of fundamentals shows up is in performance. AI techniques are generally difficult to implement and often slow. You shouldn't be too eager to lose performance because you didn't know that available algoithmic techniques could help. In this article, I look at a technique called "dynamic programming" and show an application of it.

Divide and Conquer

One of the first problem-solving techniques that a LISP programmer learns is divide and conquer -- attacking a problem by breaking it up into independent subproblems, solving them, and combining the results into the ultimate solution. Simple recursive programs use divide and conquer. Listing One shows a slightly unusual definition of factorial that uses a more aggressive approach to divide-and-conquer.

(defun fact (n)
  (labels ((prod (start end)
     (cond ((= start end) start)
       (t (let ((h (floor (+ start end) 2)))
         (* (prod start h)
           (prod (1+ h) end)))))))
(prod 1 n)))
Listing One: Definition of a factorial.

Instead of dividing the problem of n! into n and (n-1)!, this solution divides it into:

· 1 ·...n/2

and

(n/2 + 1) · ... · n

These problems are multiplied. The inner function prod computes:

end
i · ... · n
i = start

It is interesting to compare the running time of this program with the usual recursive definition of factorial:

(defun f (n)
  (if ( < n 2)
     1
     (* n (f (1- n)))))

When computing 5000!, fact is 2.67 times faster than f. Notice that each performs the same number of multiplications as the other, but the order in which they are performed is different. The multiplications to compute 8! are the following for f: 2*1, 3*2, 4*6, 5*24, 6*120, 7*720, and 8*5040. For fact they are 1*2, 3*4, 2*12, 5*6, 7*8, 30*56, and 24*1680.

This multiplication makes a difference in the amount of storage used during computation. Factorial of any number larger than 10 is a bignum, which is the name of the Common LISP type that holds integers that are larger than can be stored as machine integers. Bignums are allocated in much the same way that cons cells are allocated. Therefore, during a long calculation like factorial, intermediate bignums are consed and discarded. If the number of bits required to represent m! is denoted by s, then the number of bits required during the computation of m! using f is on the order of sm. The number of bits required using fact is on the order of s log m.

Function f keeps an accumulated value that is repeatedly multiplied by a fixnum, which is a fixed-size quantity represented generally in 32 bits. m multiplications are mostly of a bignum by a fixnum, and the final result is of size s. If we think of the nth intermediate result being of size sn/m, the total storage required is on the order of ms.

Function fact creates a bignum of the same size but produces smaller intermediate results. Let's look at the first time through the recursive clause in prod. The midpoint between 1 and m is computed, two subproducts (p and q) are computed and are multiplied together. The resulting bignum has size s, which equals the sum of the sizes of p and q.

Suppose, for ease of argument, that m is a power of 2, say 2k. We can look at the computation tree, which is a perfectly balanced binary tree, and see that the size of bignums at the root is s, as is the sum of the sizes of the two bignums at the level just below the root. Each level contains subproducts that need to be computed to produce the products on the next higher level. So, the third level contains four subproducts -- two that are multiplied to get p and two that are multiplied to get q. Therefore, every level contains numbers in which the sizes total s, and k+1 levels exist. The total amount of storage consumed is ks or s log2 m. This argument relies on the fact that the implementation of the bignum algorithms does not create additional garbage.

Another way to appreciate this argument is to count the number of fixnums that are produced by the multiplication expressions in each function. While computing 1000!, f produces 11 fixnums and fact produces 1,033 fixnums. fact produces more small intermediate results.

Finally, when computing 3000!, f conses 5.5 megabytes, while fact conses 58 kilobytes; when computing 4000!, f conses 10 megabytes, while fact conses 78 kilobytes.

However, many optimization problems cannot be solved by using divide and conquer, which treats each subproblem independently from the others. In a typical optimization problem, the subproblems are not independent but repeated or shared, and divide and conquer will solve each shared subproblem several times, which might cause significant slowdowns.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.