Skip to main content

Problem 18 Project Euler Solution with python

Maximum path sum I

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)

As stated in the question, yes this problem has a clever way to solve. But it is very simple. The approach is dynamic programming.

They both gave a very good explanation in a simple way. I will try my level best explaining the solution in my way. We are not going to consider the main problem, but the example problem for explanation.
Have a look at the following photograph:
Bottom Up Approach
Bottom Up Approach
Sorry for bad handwriting 😐.
I am not sure if we can call this approach bottom up approach, but I am using this term because it seemed relevant for me. Now let us understand the concept.
First I took the last but one row in the pyramid.
Now in the case of 2: we had option of 8 and 5. 8 has maximum value when compared to 5 and so the sum is 10.
Now for 4: 9 is better when compared to 5.
For 6: 9 is better when compared to 4.
Now the values are changed to 10(2+8), 13(4+9), 15(6+9)
Going one step further
considering 7: between 10 and 13, 13 is better.
For 4: between 13 and 15, 15 is better.
So the values change to 20(7+13), 19(4+15)
This is the final step. We have to choose between 20 and 19, in which 20 is better so the final solution will be 23.

Now you may have a  doubt that why we have chosen bottom up approach when compared to top down approach. To explain this I will consider Jeffery Sax Answer on StackOverflow.
See the following photograph and the pyramid.
1
2 3
9 1 1
Top Down approach
Top Down approach
Now as you can see that if we follow path 1 then we get our maximum sum as 5, but if we follow path2 then we get the sum of 12. Which one do you think is maximum?

If we were to solve this problem using bottom up approach, then we will get correct solution. See photograph if you want.
Bottom up approach solution for example 2
Bottom up approach solution for example 2
As you can see using bottom up approach we have arrived at the correct solution.
you can also apply this concept for reverse pyramid.
I have used Bottom Up approach to solve this problem. I have also counted the number of iterations in which we have arrived at the solution and compared it with the number of iterations one would take to arrive at the solution if we were to follow all the paths.
Compared to 16384 paths given in the question, my program only did 105 iterations and I have arrived at the solution. That means that using this approach I have eliminated 16279(99%) extra iterations. Also I have obtained the solution in a very small amount of run time.

Okay, by now after reading till here, I am assuming that you have understood the algorithm. Lets get into the programming part.

Program

The program which I wrote is as follows:
The code is well commented again.
You can see the execution of the algorithm which I've discussed above from Line41 in the program.
If you want to download above program then you can download it from Github Gist pep18.py

Output

Summary

I think this problem had a little twist. When the problem was introduced in the question it was introduced in the form of top down approach, but the wise solution was to solve the problem using bottom up approach. Anyways we have solved the problem in less than 1% iterations given in the question. So we can call this program as optimized or framing it other way performance of the program is satisfactory 😉.

I hope you enjoyed reading solution for this problem. If you have any doubt or didn't understand anything then comment in the comment box below and I will be glad to help you.

Please don't hesitate to comment if you have found any typo or you have better/different solution.

You can also contact me if you want .

Thank you. Have a nice day.😀

Popular posts from this blog

Problem 60 Project Euler Solution with python

Prime pair sets The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property. Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime. This problem is j u st a brute force problem. If you have come here because you don't know the limit upto which you will h ave to gener ate the prime numbers t hen go ahe ad and t r y with 10,000 . When I first start ed solving the problem I chose 1 million(beca use most of the problem s on project E uler have this limit ), but it took very long for the computer to fin d the solution. After searching on the internet then I found many people choosing 10, 000 so I have changed my in put f rom 1 million to 10000 and the output was f ast. He

Making a quiz web app with python and flask

Edit : When you are creating a web app with h tml templates, then y ou will have to sa ve the html file in templates folder in the Current Wor ki ng Directory( CWD). If you save the file in the C W D directl y you will get a TemplateNotFound error. Thank you Udhay for pointing it out.   In this post we will create a quiz website using python . I will be using the flask framework . After reading this tutorial you will learn form submission , flask templates , python code in flask templates , shuffling the questions and options with the random module and few others.  Please note that this tutorial is not big as it seems to be. Some of the code has been rewritten to maintain consistency and also font size is somewhat big so that your eyes won't get stressed reading this tutorial. Also the content has not occupied the full width of the page. In this tutorial I am assuming that you are having a very basic understanding of the flask framework . Please refer the documentation

Add/Embed SVG to Blogger website

In this post I will tell you my method(trick) of adding SVG images in a blogger website or blog. Before starting , the first thin g I am assu m ing is that you are aware of SVG if you are here. If not please see S calable V ec tor G raphics Recently when I tried to embed a SVG image for a post on pygal, I tried uploading the SVG file and blogger Image uploader came up with an error, because of which I had to find some other way.  SVG File upload Error in Blogger  I started sea rc hing Google " Embed SVG in Blogger " . I found blogorrhea , w h ich gave some i nformatio n on add ing SVG directly as a markup , which worked , but I faced another problem using this . Also th is guy has used lot of Javascript which was confusin g for me, being new to using SVG.   So I first t houg ht of learning on h ow to embed SVG in HTML and t his on e worked out. Actually we can embed SVG in HTML i n following ways: Using Object tag Using Iframe tag Using embed tag Us