Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
24 January 2021

Dynamic Programming

by mervyn

Fibonacci Numbers

long long F(long long n){
  if(n<=1) return 1;
  return F(n-1)+F(n-2);
}

Runtime is similar to the actual definition of Fibonacci numbers: \(T(n)=\begin{cases} \Theta(1) & n\leq1 \\ T(n-1)+T(n-2)+\Theta(1) & n>1\end{cases}\) \(F(n)=\begin{cases} 1 & n\leq1 \\ F(n-1)+F(n-2)+1 & n>1 \end{cases}\)

Problem:

Possible solution:

Memoized version:

long long F(long long n, bool isFirstCall= false){
  static long long *memo;
  if(isFirstCall){
    if(memo != NULL){delete[] memo;}
    if(n !=0){
      memo = new long long [n+1];
      for(int i=0; i<n+1; i++){
        memo[i]=0;
      }
    }
  }
  if(n<=1) return 1;
  if(memo[n]==0)
  memo[n]=F(n-1)+F(n-2);
  return memo[n];
}

use ‘static’ instead of global variables.

Top-down and Bottom-up Algorithms

This also allows for another approach:

Alternative approach is bottom-up approach

Dynamic Programming

In solving optimization problems, the top-down approach may require repeatedly obtaining optimal solutions for the same sub-problem. Dynamic programming is distinct from divide-and-conquer, as the divide-and-conquer approach works well if the sub-problems are essentially unique

If sub-problems re-occur, the problem is said to have overlapping sub-problems

source

“K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 15-1 동적계획법의 방법론 이해 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: