0

Considering the classical staircase problem as "Davis has a number of staircases in his house and he likes to climb each staircase 1, 2, or 3 steps at a time. Being a very precocious child, he wonders how many ways there are to reach the top of the staircase."

My approach is to use memoization with recursion as

# TimeO(N), SpaceO(N), DP Bottom Up + Memoization
def stepPerms(n, memo = {}):

    if n < 3:
        return n
    elif n == 3:
        return 4

    if n in memo:
        return memo[n]
    else:
        memo[n] = stepPerms(n - 1, memo) + stepPerms(n - 2 ,memo) + stepPerms(n - 3 ,memo)
        return memo[n]

The question that comes to my mind is that, is this solution bottom-up or top-down. My way of approaching it is that since we go all the way down to calculate the upper N values (imagine the recursion tree). I consider this bottom-up. Is this correct?

cs guy
  • 926
  • 2
  • 13
  • 33

2 Answers2

2

Recoursion strategies are as a general rule topdown approaches, whether they have memory or not. The underlaying algorithm design is dynamic programming, which traditionally built in a bottom-up fashion.

I noticed that you wrote your code in python, and python is generally not happy about deep recoursion (small amounts are okay, but performance quickly takes a hit and there is a maximum recousion depth of 1000 - unless it was changed since I read that).

If we make a bottom-up dynamic programmin version, we can get rid of this recousion, and we can also recognise that we only need constant amount of space, since we are only really interested in the last 3 values:

def stepPerms(n):
    if n < 1: return n
    memo = [1,2,4]
    if n <= 3: return memo[n-1]

    for i in range(3,n):
        memo[i % 3] = sum(memo)
    return memo[n-1]

Notice how much simpler the logic is, appart from the i is one less than the value, since the positions are starts a 0 instead of the count of 1.

Ninetails
  • 254
  • 1
  • 5
  • In a Binary Tree an algorithm similar to my algorithm would be considered as bottom-up, since the result is accumulating from bottom to up, I assume that terminologies are different between BT and DP? – cs guy Jun 23 '19 at 15:34
  • The destrinction does not have as much to do with how the results move as you think. Binary trees are in their base form a top down data-structure, since they are a datastructure build around the top-down method divide and conqour, even though most divide and conqour algorithms I recall are designed from bottom-up properties (since merging is the main part of most divide and conqour algorithms, and they are bouild around bottom-up properties). Confused? That is understandable, because bottom-up vs top down really is about how to mentally break down a problem, more that a strict catagories. – Ninetails Jun 23 '19 at 15:58
0

In the top-down approach, the complex module is divided into submodules. So it is top down approach. On the other hand, bottom-up approach begins with elementary modules and then combine them further.

And bottom up approach of this solution will be:

memo{}

for i in range(0,3):
   memo[i]=i
memo[3]=4

for i in range(4,n+1):
  memo[i]=memo[i-1]+memo[i-2]+memo[i-3]
mahbubcseju
  • 2,200
  • 2
  • 16
  • 21
  • In a Binary Tree an algorithm similar to my algorithm would be considered as bottom-up, since the result is accumulating from bottom to up, I assume that terminologies are different between BT and DP? – cs guy Jun 23 '19 at 15:34
  • In a Binary Tree an algorithm similar to your example would be considered as bottom-up ? I don't think so ! Please read this : https://www.quora.com/What-is-the-difference-between-top-down-merge-sort-and-bottom-up-merge-sort – mahbubcseju Jun 23 '19 at 15:41
  • This is still not a Binary Tree, merge sort is pictured a binary tree there, please read this article if you have leetcode and you would understand what i mean, https://leetcode.com/explore/learn/card/data-structure-tree/17/solve-problems-recursively/534/, – cs guy Jun 23 '19 at 15:45
  • Merge sort obviously follow the binary tree approach. Even the complete binary tree approach. – mahbubcseju Jun 23 '19 at 15:48