Analysis of recursive and non-recursive implementations of python dynamic planning, python Recursion
Summary
This article briefly introduces the implementation of dynamic programming recursion and non-recursive algorithms.
Case 1
Question 1: Calculate the non-adjacent largest sum of Arrays
[Description]
In an array arr, find a group of numbers that are not adjacent to each other to maximize the sum of the last and last.
[Example input]
Arr = 1 2 4 1 7 8 3
[Sample output]
15
From functools import wrapsdef memoDeco (func): ''' memoDeco mainly caches nodes that have been traversed, reducing recursive memory overhead ''' cashe ={}@ wraps (func) def wrapper (* args): if args not in cashe: cashe [args] = func (* args) return cashe [args] return wrapper @ memoDecodef recMaxArray (array, index ): if index = 0: return array [0] elif index = 1: return max (array [0], array [1]) else: return max (recMaxArray (array, index-2) + array [index], recMaxArray (array, index-1) if _ name __= = "_ main _": array = (1, 2, 4, 1, 7, 8, 3) print (recMaxArray (array, len (array)-1 ))
Non-Recursive Implementation
Def dpMaxArray (array): ''' for details about the code, refer to reference 1: Lantern presentation ''' lens = len (array) maxArray = [0] * (lens) maxArray [0] = array [0] maxArray [1] = max (array [0], array [1]) for I in range (2, lens ): maxArray [I] = max (maxArray [I-2] + array [I], maxArray [I-1]) return maxArray [-1] if _ name __= = "_ main _": array = (1, 2, 4, 1, 7, 8, 3) print (dpMaxArray (array ))
Case 2
[Description]
Given a positive integer s, judge whether there is a group of numbers in an array arr that add up to s.
[Example input]
Arr = 3 34 4 12 5 3
S = 9
[Instance output]
True
Recursive Implementation
From functools import wraps # similar to the first question, the decorator can act as a cache node def memoDeco (func): ''' memoDeco is mainly used to cache nodes that have already been traversed, reduce recursive memory overhead ''' cashe ={}@ wraps (func) def wrapper (* args): if args not in cashe: cashe [args] = func (* args) return cashe [args] return wrapper @ memoDecodef recSubSet (arr, index, tar_num): if index = 0: return arr [0] = tar_num elif tar_num = 0: return True elif arr [index]> tar_num: return recSubSet (arr, index-1, tar_num) else: return recSubSet (arr, index-1, tar_num) or recSubSet (arr, index-1, tar_num-index) if _ name _ = "_ main _": arr = (3, 34, 4, 12, 5, 3) tar_num = 13 index = len (arr)-1 print (recSubSet (arr, index, tar_num ))
Non-Recursive Implementation
'''Multi-dimensional array construction it is more convenient to use python third-party library numpy for code explanation. For details, see reference 1: Lantern presentation on October 10''' import numpy as npdef dpSubSet (arr, tar_num ): subSet = np. zeros (len (arr), tar_num + 1), dtype = bool) subSet [:, 0] = True subSet [0,:] = False subSet [0, arr [0] = True for I in range (1, len (arr): for j in range (1, tar_num + 1): if arr [I]> j: subSet [I, j] = subSet [I-1, j] else: subSet [I, j] = subSet [I-1, j] or subSet [I-1, j-arr [I] return subSet [-1,-1] if _ name _ = "_ main _": arr = (3, 34, 4, 12, 5, 3) tar_num = 13 print (dpSubSet (arr, tar_num ))