Thursday, March 31, 2011

Project Euler-Problems 18 & 67

Description

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)

Solution

As the note in the problem statement mentions, this problem is pretty easy to brute force by simply adding together every possible path. We will assume the triangle is stored as an array of arrays, like so:

class Problem018 
  TRIANGLE = [ [75],
               [95,64],
               [17,47,82],
               [18,35,87,10],

               # additional rows

               [4,62,98,27,23,9,70,98,73,93,38,53,60,4,23]
             ]
end

Now we can build a method that recursively evaluates the “top” of the triangle, plus the two smaller sub-triangles underneath it:

module TriangleSolver
  def add_triangle(triangle, row = 0, col = 0)
    return 0 if row >= triangle.length
    return triangle[row][col] if row == triangle.length - 1
    
    left = add_triangle(triangle, row + 1, col)  # compute the maximal sum of the left sub-triangle
    right = add_triangle(triangle, row + 1, col + 1) # compute the maximal sum of the right sub-triangle

    triangle[row][col] + (left + right ? left : right)
  end
end

Notice that here I’ve used a module because we will be making use of this solution for problem 67 as well. This works well for this problem since there are relatively few paths to compute. But as the triangle gets larger this will become too slow to be able to solve. However, we can greatly improve the number of paths that have to actually be computed.

The first step towards this goal is to realize that each sub-triangle (except the ones on the very edge) get reused. How? Take a look at the original (smaller) example triangle. The triangle 4-5-9 is both the right sub-triangle of the 7 in the second row and it is the left sub-triangle of the 4 in the second row. In our current implementation, we would compute the maximal sum of this sub-triangle both times, but we really only need to compute it once and then reuse that result. So, with some judicious use of memoization, we can actually reduce the number of triangle computations by a large degree!

module TriangleSolver
  def initialize
    @cache = {}
  end

  def add_triangle(triangle, row = 0, col = 0)
    return 0 if row >= triangle.length
    return triangle[row][col] if row == triangle.length - 1
    return @cache[row][col] if @cache.include? row and @cache[row].include? col
    
    left = add_triangle(triangle, row + 1, col)  # compute the maximal sum of the left sub-triangle
    right = add_triangle(triangle, row + 1, col + 1) # compute the maximal sum of the right sub-triangle

    @cache[row] ||= {}
    @cache[row][col] = triangle[row][col] + (left + right ? left : right)
  end
end

With this change, our solution is much more efficient for larger triangles. How can we test that fact? Well, problem 67 is the same exact problem, just a much larger triangle. Here is the description of 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 in triangle.txt (right click and 'Save Link/Target As...'), a 15K text file containing a triangle with one-hundred rows.

NOTE: This is a much more difficult version of Problem 18. It is not possible to try every route to solve this problem, as there are 299 altogether! If you could check one trillion (1012) routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it. ;o)

So to test our solution against this much larger triangle, all we need is a method to load the triangle from disk.

class Problem067
  include TriangleSolver

  def load_file
    File.open('triangle.txt') do |file|
      file.readlines.map { |line| line.split(' ').map { |num| num.to_i } }
    end
  end
end

And now when we run problem 67, we can see that indeed, our memoization technique is a success. We can find the maximal sum of a triangle with 100 rows in less than one second. I dare say that is a lot better than twenty billion years!

The full source and specifications for problem 18 can be seen on github. You can also see the source and specs for the TriangleSolver as well as problem 67. Next time, how many Sundays fell on the first of the month during the twentieth century?

No comments: