Showing posts with label dynamic programming. Show all posts
Showing posts with label dynamic programming. Show all posts

Problem 018: Maximum Path Sum I

From Project Euler:
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

We could brute force this one or even solve it by hand, but since Problem 67 is much more complex we might as well solve this one now. An easy dynamic programming solution is just to start from the bottom and work our way up. Starting from the second-to-last row, for each index $i$ determine whether a left or a right is best by taking the max of the values in spots $i$ and $i + 1$ in the next row, and adding that to our current element.

The code is pretty straight-forward:
(defn collapse [row1 row2]
    (assert (== (+ 1 (count row1)) (count row2)))
    (->> (range (count row1))
         (map #(+ (nth row1 %)
                  (max (nth row2 %)
                       (nth row2 (+ % 1)))))))

(defn solve [rows]
  (assert (> (count rows) 0))
  (loop [current-row (- (count rows) 2)
         _rows rows]
    (if (== current-row -1)
      (first (first _rows))
      (recur (- current-row 1)
             (assoc _rows
                    current-row
                    (collapse (nth _rows current-row)
                              (nth _rows (+ current-row 1))))))))
And it runs really fast:
rob@alien ~/code/euler/clj-euler $ lein run
Processing...
1074
"Elapsed time: 4.771813 msecs"

Problem 015: Lattice paths

From Project Euler:

Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.


How many such routes are there through a 20×20 grid?

There is a very fast solution for this problem using combinatorics. It's a very interesting approach, but since I didn't think of it and swiped it from somewhere else, I'll leave it as an exercise to you to figure that one out.

What I did was approach this as a dynamic programming problem. For each point in the lattice, the only ways to get there are from the left or from the top. An edge case (pun intended) is along the left and top edges, for every point there is only one possible way to get there. So for each point the number of paths $P(x, y)$ is:
$$
P(x, y) = \left\{
\begin{array}{ll}
1 & x = 0\ or\ y = 0 \\
P(x - 1, y) + P(x, y - 1) & otherwise
\end{array}
\right.
$$
At first glance this might look like a recursive solution, however that will be very inefficient since we'll end up calculating several values many times. Instead, we'll use a dynamic programming approach to build up a grid of values, starting from one edge and working our way across. Once we've filled the grid, we return the value in the bottom right corner.
(defn count-routes [width height]
  (loop [x 0
         y 0
         grid {}]
    (if (and (== x 0) (== y height))
      (grid [(- width 1) (- height 1)])
      (recur (mod (+ x 1) width)
             (if (== x (- width 1))
               (+ y 1)
               y)
             (assoc grid
                    [x y]
                    (+ (if (> x 0) (grid [(- x 1) y]) 1)
                       (if (> y 0) (grid [x (- y 1)]) 1)))))))
This runs super fast:
$ lein run
Processing...
137846528820
"Elapsed time: 7.410219 msecs"

Problem 014: Longest Collatz Sequence

From Project Euler:
The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.
This problem has a brute force method, which is simple: just loop through and calculate the length of each chain.
(defn get-next-val [n]
  (if (even? n)
    (quot n 2)
    (+ (* 3 n) 1)))

(defn get-chain-length [n]
  (loop [_n n
         total 1]
    (if (== _n 1)
      total
      (recur (get-next-val _n)
             (+ 1 total)))))

(defn find-longest-chain-brute-force [max-start-value]
  (loop [current 2
         best 1
         best-length 1]
    (if (== current max-start-value)
      best
      (let [current-length (get-chain-length current)]
        (recur (+ 1 current)
               (if (> current-length best-length) current best)
               (max current-length best-length))))))
This doesn't actually take much time (I discovered the time function recently):
$ lein run
Processing...
837799
"Elapsed time: 17328.997568 msecs"
17 seconds isn't too bad! But we can do better. A lot of these chains flow into one another, so we end up repeating a number of calculations: a pattern called overlapping sub-problems. This leads us to a technique called dynamic programming, which in this case simply involves us caching the lengths of chains and loading them from the cache when we need them.

Here's an example:
(defn populate-lengths [root lengths]
  (let [next-val (get-next-val root)
        new-lengths (if (not (contains? lengths next-val))
                      (populate-lengths next-val lengths)
                      lengths)]
    (assoc new-lengths root (+ 1 (get new-lengths next-val)))))

(defn find-longest-chain-dp [max-start-value]
  (loop [current 2
         lengths {1 1}
         best 1
         best-length 1]
    (if (== current max-start-value)
      best
      (let [new-lengths (populate-lengths current lengths)
            current-length (get new-lengths current)]
        (recur (+ 1 current)
               new-lengths
               (if (> current-length best-length) current best)
               (max current-length best-length))))))
This runs much faster:
$ lein run
Processing...
837799
"Elapsed time: 6036.937932 msecs"
This is much faster, and will scale a lot better than the brute force approach.

There is an option to use Clojure's memoize function to wrap get-chain-length. This unfortunately gets you into some annoyances around variable bindings, so I just ended up sticking to the dynamic programming approach.