Tuesday, April 2, 2013

Project Euler Problem 18 : Maximum Path Sum 1

In the name of Allah.

For those who loves programming challenges, can look at this link for a list.

Phew. Finally finish writing a program for Problem 18 Project Euler.

#########################################


Maximum Path Sum 1 (Project Euler Problem 18)

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) 

#########################################

At first, I thought it just look going to max adjacent below row route. But after trying the triangle posted by them, it is about the maximum sum of a path of numbers. Therefore, I thought that it might be good to look it up from the bottom.

But I can't think of a way.

So first, how would I stored the data? I thought of 2D array, binary tree and graph. Graph won't work, binary tree may show a fast speed, but it doesn't seems to have any way of storing 0 (or I just didn't see it). Therefore, the overused array are used.

Second,question to see how they are adjacent to each others. Third, how to know it is a maximum path from top to bottom. I read an interesting thing that max f(x) = -min[f(x)]. But, my imagination see no specific function for this, and then I guess it is not needed when I finally found the one-line function.

<_< >_>

Alright, after 30 minutes of thinking, I just google it. -.-" But, I only read the C# program explanation, then I use that 'inspiration' to build it on my own. Cheating? Yeah, I still noob. T_T.

First, I used this algorithm/pseduocode.

1. Read input from input.txt.
2. Store into 2D array integer.
3. Do the triangle function thingy.
4. Display last triangle function OR display the [0][0] element.
5. Eat supper.

Okay, ignore that number 5. I am currently in hyper mode from listening to Vocaloid songs.

And the triangle Function thingy I got:

for (i=SIZE-1; i>=0; i--)
for (j=0; i<SIZE;j++)
[i-1] [j] = [i-1][j] + max([i][j],[i][j+1]

where SIZE is the number of lines in text files. Or, size of row/column of the array.

So, how in the heaven and on earth I got that?

First, the data representation, as mentioned, is done by putting into 2D array. The empty part is filled with zeros. Because I don't really remember how to file input, I use manual array initialisation now. Therefore, it is in form of

If we find the maximum between 2 lowest row, then add to the higher row adjacent element, it create some sort of triangle maximum summation. So, Uh, continue doing so until the top making the top having the maximum sum path thing. (Getting hard to explain).

I tried some trial and error to see the pattern as I usually do.
2 + max(8,5) <=> [2][0]+max([3][0],[3][1]);
4 + max(5,9) <=> [2][1]+max([3][1],[3][2]);
6 + max(9,3) <=> [2][2]+max([3][2],[3][3]); 
-----------------------------------------------------
[i-1] [j] = [i-1][j] + max([i][j],[i][j+1] 
-----------------------------------------------------

So, collapsing triangle like recursively:

3
7 4
2 4 6
8 5 9 3

3
7 4
10 13 15

3
20 19

23
You can see that the operation reduce the triangle to the answer. But, my function seems to meet with the boundary. So, re-adjust them:

 for (i=SIZE-2; i>=0; i--)
for (j=0; i<=1;j++)
[i-1] [j] += max([i+1][j],[i+1][j+1]




No comments:

Post a Comment