Question: Write in Python Please You are required to find the greatest path sum starting at the top of the triangle and moving only to adjacent

Write in Python Please

You are required to find the greatest path sum starting at the top of the triangle and moving only to adjacent numbers on the row below.

 3 7 4 2 4 6 8 5 9 3 

In the above triangle, the maximum path sum is 3 + 7 + 4 + 9 = 23 You will read your input from the file triangle.txt. The first line indicates n the number of rows in the triangle triangle.txt

15 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 

You will apply three different approaches to problem solving to this single problem - greedy, divide and conquer (recursive), and dynamic programming. Here is the outline of the code:

import time # returns the greatest path sum using greedy approach def greedy (grid): return # returns the greatest path sum using divide and conquer (recursive) approach def rec_search (grid): return # returns the greatest path sum using dynamic programming def dynamic_prog (grid): return # reads the file and returns a 2-D list that represents the triangle def read_file (): return def main (): # read triangular grid from file ti = time.time() # output greates path from greedy approach tf = time.time() del_t = tf - ti # print time taken using greedy approach ti = time.time() # output greates path from divide-and-conquer approach tf = time.time() del_t = tf - ti # print time taken using divide-and-conquer approach ti = time.time() # output greates path from dynamic programming tf = time.time() del_t = tf - ti # print time taken using dynamic programming main() 

Output:

The greatest path sum through greedy search is 23. The time taken for greedy approach is x seconds. The greatest path sum through recursive search is 23. The time taken for recursive search is x seconds. The greatest path sum through dynamic programming is 23. The time taken for dynamic programming is x seconds. 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!