SlideShare a Scribd company logo
1 of 17
By POSTECH Computer Algorithm Team
DP Easy
송문경
SO EZ
Contents
POSTECH Computer Algorithm Team
구현 1
개요 0
연습 문제 2
POSTECH Computer Algorithm Team
- DP는 Dynamic Programming의 약자이며, 동적 계획법이라고도 부른다.
- DP는 재귀(recursion)를 이용하여 최적화 솔루션을 얻어내는 방식을 사용한다.
- 알고리즘의 꽃이라고도 부르며, 쉬운 문제는 엄청 쉽지만 한없이 어렵게 낼 수도 있는 분야이다.
- DP는 부분 문제들이 DAG(Directed Acyclic Graph)의 형태면 적용할 수 있다.(?????)
DP란?
개요
POSTECH Computer Algorithm Team
개요
- 피보나치 수를 예시로 들자.
- F(n) = n번째 피보나치 수 라고 정의하자.
- F(n) = F(n – 1) + F(n – 2)
- 보시다시피 n번째 피보나치 수를 구할 때는 n – 1번째와 n – 2번째 피보나치 수를 이용한다.
Q) F(n-1), F(n-2)를 구할 때 F(n)이 필요한가?
POSTECH Computer Algorithm Team
개요
A) 필요가 없다.
- 만약 F(n-1) 또는 F(n-2)를 구하는데 F(n)이 필요하다면, 사이클이 생겨버린다.
- 하지만 피보나치 수는 그런 사이클이 생기지 않는다.
- 즉, 부분 문제들 사이에 사이클이 생기지 않는다.
POSTECH Computer Algorithm Team
개요
- 이제 피보나치 수는 사이클이 없는, DAG 모양의 문제라는 것을 알 수 있다.
- 이제 피보나치 수를 여러 개 구한다고 해보자. 5번째, 9번째, 3번째 등등…
- 원래대로라면 F(5) = F(4) + F(3) = F(3) + F(2) + F(2) + F(1) = 3 * F(2) + 2 * F(1) 로 구하고,
- F(9)나 F(3)도 F(1), F(2)가 나올 때까지 이런 계산 작업을 반복해야 한다.
Q) F(5)를 계산한 상태에서 F(9)나 F(3)를 효율적으로 계산할 수 없을까?
POSTECH Computer Algorithm Team
개요
A)
- F(5)를 계산하는 과정에서 F(1~4)도 계산한다.
- 즉, F(9)를 계산할 때 F(4)나 F(5)와 관련된 식이 나오면 바로 계산할 수 있다.
- F(3)도 마찬가지다.
- 이렇게 이미 계산한 부분 문제들을 이용하는 것을 “메모이제이션(memoization)”이라고 한다. Memorization과는
다르다!
Q) 만약 부분 문제들 사이에 사이클이 있으면 메모이제이션을 사용할 수 있을까?
POSTECH Computer Algorithm Team
1. 부분 문제를 정의한다. (F(n) = n번째 피보나치 수)
2. 부분 문제들과 관련된 점화식을 세운다. (F(n) = F(n – 1) + F(n – 2))
3. 초항(base case)에 대해 문제를 해결한다. (F(1) = F(2) = 1)
DP Solve
개요
POSTECH Computer Algorithm Team
DP는 다음과 같이 두 가지 방법으로 구현할 수 있다.
1. 상향식(반복문)
2. 하향식(재귀함수)
구현
구현
Code Explanation
POSTECH Computer Algorithm Team
Code
int num[1000001] = {0,};
int FS(int n){
if(num[n] != 0) return num[n];
if(n == 1) return num[1] = 1;
if(n == 2) return num[2] = 1;
return num[n] = f(n-1) + f(n-2);
}
피보나치 수(하향식)
Code Explanation
POSTECH Computer Algorithm Team
Code
int main(void){
int n, num[10000001];
cin >> n;
num[1] = 1;
num[2] = 1;
for(int i = 3; i <= n; i++){
num[i] = num[i-1] + num[i-2];
}
cout << num[n] << ‘n’;
}
피보나치 수(상향식)
POSTECH Computer Algorithm Team
구현
- 하향식은 점화식과 똑같이 구현하면 되기 때문에 구현 난이도가 낮다.
- 상햑식은 제일 작은 문제들부터 풀어나가는 식이기 때문에 점화식과는 방향이 반대여서, 구현이 어렵다.
- 다만 함수를 계속 부르는 하향식에 비해, 상향식이 성능이 더 좋다.
(물론 컴파일러 성능이 좋아진 현재, 의미가 없지만)
POSTECH Computer Algorithm Team
- https://www.acmicpc.net/problem/1463
- https://www.acmicpc.net/problem/9095
- https://www.acmicpc.net/problem/2579
연습 문제
연습 문제
POSTECH Computer Algorithm Team
- f(n) : n을 1로 만드는 최소 연산 수
- 점화식 : f(n) = min(f(n/3), f(n/2), f(n-1)) + 1
- Base cases : f(1) = 0, f(2) = 1, f(3) = 1
1로 만들기
연습 문제
POSTECH Computer Algorithm Team
- f(n) : 1, 2, 3으로 n을 만들 수 있는 방법의 수
- 점화식 : f(n) = f(n-1) + f(n-2) + f(n-3)
- Base cases : f(1) = 1, f(2) = 2, f(3) = 4
1, 2, 3 더하기
연습 문제
POSTECH Computer Algorithm Team
- f(n) : n번째 계단까지 쌓을 수 있는 최대 점수
- 점화식 : f(n) = s(n) + max(s(n-1) + f(n-3),f(n-2))
- Base cases : f(1) = s(1), f(2) = s(1) + s(2), f(3) = max(s(1), s(2)) + s(3)
계단 오르기
연습 문제
POSCAT
Thank you :-)

More Related Content

What's hot

한양대 2017 hcpc advanced division 해설
한양대 2017 hcpc advanced division 해설한양대 2017 hcpc advanced division 해설
한양대 2017 hcpc advanced division 해설NAVER D2
 
Ch.5 Deep Learning
Ch.5 Deep LearningCh.5 Deep Learning
Ch.5 Deep LearningPartPrime
 
The Art of Computer Programming 1.3.2 MIXAL
The Art of Computer Programming 1.3.2 MIXALThe Art of Computer Programming 1.3.2 MIXAL
The Art of Computer Programming 1.3.2 MIXALhyun soomyung
 
Pure Function and Rx
Pure Function and RxPure Function and Rx
Pure Function and RxHyungho Ko
 
Pure Function and Honest Design
Pure Function and Honest DesignPure Function and Honest Design
Pure Function and Honest DesignHyungho Ko
 
인하대 프로그래밍 경진대회 - 문제풀이
인하대 프로그래밍 경진대회 - 문제풀이인하대 프로그래밍 경진대회 - 문제풀이
인하대 프로그래밍 경진대회 - 문제풀이NAVER D2
 
Graph2
Graph2Graph2
Graph2GNGLB
 
Project#1파스칼 삼각형
Project#1파스칼 삼각형Project#1파스칼 삼각형
Project#1파스칼 삼각형Kimjeongmoo
 
Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기
Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기
Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기Hyemin Song
 
2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차Moonki Choi
 
2012 Dm C3 06
2012 Dm C3 062012 Dm C3 06
2012 Dm C3 06chl132435
 
[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이
[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이
[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이NAVER D2
 
자료구조 01 최종 보고서
자료구조 01 최종 보고서자료구조 01 최종 보고서
자료구조 01 최종 보고서pkok15
 
2019 ppc answers
2019 ppc answers2019 ppc answers
2019 ppc answers승혁 조
 

What's hot (20)

자료구조01
자료구조01자료구조01
자료구조01
 
컴퓨터개론09
컴퓨터개론09컴퓨터개론09
컴퓨터개론09
 
한양대 2017 hcpc advanced division 해설
한양대 2017 hcpc advanced division 해설한양대 2017 hcpc advanced division 해설
한양대 2017 hcpc advanced division 해설
 
Ch.5 Deep Learning
Ch.5 Deep LearningCh.5 Deep Learning
Ch.5 Deep Learning
 
DP 중급 2
DP 중급 2DP 중급 2
DP 중급 2
 
6주차 스터디
6주차 스터디6주차 스터디
6주차 스터디
 
The Art of Computer Programming 1.3.2 MIXAL
The Art of Computer Programming 1.3.2 MIXALThe Art of Computer Programming 1.3.2 MIXAL
The Art of Computer Programming 1.3.2 MIXAL
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
Pure Function and Rx
Pure Function and RxPure Function and Rx
Pure Function and Rx
 
Pure Function and Honest Design
Pure Function and Honest DesignPure Function and Honest Design
Pure Function and Honest Design
 
인하대 프로그래밍 경진대회 - 문제풀이
인하대 프로그래밍 경진대회 - 문제풀이인하대 프로그래밍 경진대회 - 문제풀이
인하대 프로그래밍 경진대회 - 문제풀이
 
Graph2
Graph2Graph2
Graph2
 
Project#1파스칼 삼각형
Project#1파스칼 삼각형Project#1파스칼 삼각형
Project#1파스칼 삼각형
 
Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기
Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기
Aperture : 6. 피에조 부저를 이용해 멜로디 연주하기
 
01. dp hard
01. dp hard01. dp hard
01. dp hard
 
2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차2021 2학기 정기 세미나 5주차
2021 2학기 정기 세미나 5주차
 
2012 Dm C3 06
2012 Dm C3 062012 Dm C3 06
2012 Dm C3 06
 
[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이
[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이
[D2 CAMPUS] 2016 한양대학교 프로그래밍 경시대회 문제풀이
 
자료구조 01 최종 보고서
자료구조 01 최종 보고서자료구조 01 최종 보고서
자료구조 01 최종 보고서
 
2019 ppc answers
2019 ppc answers2019 ppc answers
2019 ppc answers
 

Similar to 03. dp easy

피보나치 수열과 파이썬.pptx
피보나치 수열과 파이썬.pptx피보나치 수열과 파이썬.pptx
피보나치 수열과 파이썬.pptxssuser791410
 
1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)fmbvbfhs
 
05 divide and conquer
05 divide and conquer05 divide and conquer
05 divide and conquer승혁 조
 
Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기
Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기
Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기Ki-Hwan Kim
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexityskku_npc
 
01. c and time complexity
01. c and time complexity01. c and time complexity
01. c and time complexity승혁 조
 
2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdfkd19h
 
2012 Ds B2 02
2012 Ds B2 022012 Ds B2 02
2012 Ds B2 02chl132435
 
[신경망기초] 퍼셉트론구현
[신경망기초] 퍼셉트론구현[신경망기초] 퍼셉트론구현
[신경망기초] 퍼셉트론구현jaypi Ko
 
객체지향 정리. Part1
객체지향 정리. Part1객체지향 정리. Part1
객체지향 정리. Part1kim HYUNG JIN
 
자료구조 Project2
자료구조 Project2자료구조 Project2
자료구조 Project2KoChungWook
 
사운드처리_텀과제_이정근.pptx
사운드처리_텀과제_이정근.pptx사운드처리_텀과제_이정근.pptx
사운드처리_텀과제_이정근.pptxtangtang1026
 
자료구조 프로젝트
자료구조 프로젝트자료구조 프로젝트
자료구조 프로젝트hyungoh kim
 

Similar to 03. dp easy (17)

피보나치 수열과 파이썬.pptx
피보나치 수열과 파이썬.pptx피보나치 수열과 파이썬.pptx
피보나치 수열과 파이썬.pptx
 
1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)1.자료구조와 알고리즘(강의자료)
1.자료구조와 알고리즘(강의자료)
 
05 divide and conquer
05 divide and conquer05 divide and conquer
05 divide and conquer
 
Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기
Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기
Python의 계산성능 향상을 위해 Fortran, C, CUDA-C, OpenCL-C 코드들과 연동하기
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 
01. c and time complexity
01. c and time complexity01. c and time complexity
01. c and time complexity
 
2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf2012 Ds B2 02 Pdf
2012 Ds B2 02 Pdf
 
2012 Ds B2 02
2012 Ds B2 022012 Ds B2 02
2012 Ds B2 02
 
[신경망기초] 퍼셉트론구현
[신경망기초] 퍼셉트론구현[신경망기초] 퍼셉트론구현
[신경망기초] 퍼셉트론구현
 
Dp 1
Dp 1Dp 1
Dp 1
 
자료구조01
자료구조01자료구조01
자료구조01
 
객체지향 정리. Part1
객체지향 정리. Part1객체지향 정리. Part1
객체지향 정리. Part1
 
Mylab
MylabMylab
Mylab
 
자료구조 Project2
자료구조 Project2자료구조 Project2
자료구조 Project2
 
자구2번
자구2번자구2번
자구2번
 
사운드처리_텀과제_이정근.pptx
사운드처리_텀과제_이정근.pptx사운드처리_텀과제_이정근.pptx
사운드처리_텀과제_이정근.pptx
 
자료구조 프로젝트
자료구조 프로젝트자료구조 프로젝트
자료구조 프로젝트
 

More from 승혁 조

RSS Live Slider
RSS Live SliderRSS Live Slider
RSS Live Slider승혁 조
 
Parallel binary search
Parallel binary searchParallel binary search
Parallel binary search승혁 조
 
Geometry Algorithms
Geometry AlgorithmsGeometry Algorithms
Geometry Algorithms승혁 조
 
Heavy light decomposition
Heavy light decompositionHeavy light decomposition
Heavy light decomposition승혁 조
 
Advanced segment tree
Advanced segment treeAdvanced segment tree
Advanced segment tree승혁 조
 
L-R network flow
L-R network flowL-R network flow
L-R network flow승혁 조
 
Sqrt(n) algorithm
Sqrt(n) algorithmSqrt(n) algorithm
Sqrt(n) algorithm승혁 조
 
String algorithm
String algorithmString algorithm
String algorithm승혁 조
 
Tree algorithm
Tree algorithmTree algorithm
Tree algorithm승혁 조
 
05. network flow 2
05. network flow 205. network flow 2
05. network flow 2승혁 조
 
04. network flow 1
04. network flow   104. network flow   1
04. network flow 1승혁 조
 
04. binary search
04. binary search04. binary search
04. binary search승혁 조
 
03. segment tree
03. segment tree03. segment tree
03. segment tree승혁 조
 
02. binary search tree
02. binary search tree02. binary search tree
02. binary search tree승혁 조
 

More from 승혁 조 (20)

RSS Live Slider
RSS Live SliderRSS Live Slider
RSS Live Slider
 
Parallel binary search
Parallel binary searchParallel binary search
Parallel binary search
 
Geometry Algorithms
Geometry AlgorithmsGeometry Algorithms
Geometry Algorithms
 
FFT
FFTFFT
FFT
 
Heavy light decomposition
Heavy light decompositionHeavy light decomposition
Heavy light decomposition
 
Advanced segment tree
Advanced segment treeAdvanced segment tree
Advanced segment tree
 
L-R network flow
L-R network flowL-R network flow
L-R network flow
 
MCMF
MCMFMCMF
MCMF
 
Sqrt(n) algorithm
Sqrt(n) algorithmSqrt(n) algorithm
Sqrt(n) algorithm
 
NIM game
NIM gameNIM game
NIM game
 
분할정복
분할정복분할정복
분할정복
 
String algorithm
String algorithmString algorithm
String algorithm
 
Tree algorithm
Tree algorithmTree algorithm
Tree algorithm
 
Number theory
Number theoryNumber theory
Number theory
 
06. sorting
06. sorting06. sorting
06. sorting
 
05. network flow 2
05. network flow 205. network flow 2
05. network flow 2
 
04. network flow 1
04. network flow   104. network flow   1
04. network flow 1
 
04. binary search
04. binary search04. binary search
04. binary search
 
03. segment tree
03. segment tree03. segment tree
03. segment tree
 
02. binary search tree
02. binary search tree02. binary search tree
02. binary search tree
 

03. dp easy

  • 1. By POSTECH Computer Algorithm Team DP Easy 송문경 SO EZ
  • 2. Contents POSTECH Computer Algorithm Team 구현 1 개요 0 연습 문제 2
  • 3. POSTECH Computer Algorithm Team - DP는 Dynamic Programming의 약자이며, 동적 계획법이라고도 부른다. - DP는 재귀(recursion)를 이용하여 최적화 솔루션을 얻어내는 방식을 사용한다. - 알고리즘의 꽃이라고도 부르며, 쉬운 문제는 엄청 쉽지만 한없이 어렵게 낼 수도 있는 분야이다. - DP는 부분 문제들이 DAG(Directed Acyclic Graph)의 형태면 적용할 수 있다.(?????) DP란? 개요
  • 4. POSTECH Computer Algorithm Team 개요 - 피보나치 수를 예시로 들자. - F(n) = n번째 피보나치 수 라고 정의하자. - F(n) = F(n – 1) + F(n – 2) - 보시다시피 n번째 피보나치 수를 구할 때는 n – 1번째와 n – 2번째 피보나치 수를 이용한다. Q) F(n-1), F(n-2)를 구할 때 F(n)이 필요한가?
  • 5. POSTECH Computer Algorithm Team 개요 A) 필요가 없다. - 만약 F(n-1) 또는 F(n-2)를 구하는데 F(n)이 필요하다면, 사이클이 생겨버린다. - 하지만 피보나치 수는 그런 사이클이 생기지 않는다. - 즉, 부분 문제들 사이에 사이클이 생기지 않는다.
  • 6. POSTECH Computer Algorithm Team 개요 - 이제 피보나치 수는 사이클이 없는, DAG 모양의 문제라는 것을 알 수 있다. - 이제 피보나치 수를 여러 개 구한다고 해보자. 5번째, 9번째, 3번째 등등… - 원래대로라면 F(5) = F(4) + F(3) = F(3) + F(2) + F(2) + F(1) = 3 * F(2) + 2 * F(1) 로 구하고, - F(9)나 F(3)도 F(1), F(2)가 나올 때까지 이런 계산 작업을 반복해야 한다. Q) F(5)를 계산한 상태에서 F(9)나 F(3)를 효율적으로 계산할 수 없을까?
  • 7. POSTECH Computer Algorithm Team 개요 A) - F(5)를 계산하는 과정에서 F(1~4)도 계산한다. - 즉, F(9)를 계산할 때 F(4)나 F(5)와 관련된 식이 나오면 바로 계산할 수 있다. - F(3)도 마찬가지다. - 이렇게 이미 계산한 부분 문제들을 이용하는 것을 “메모이제이션(memoization)”이라고 한다. Memorization과는 다르다! Q) 만약 부분 문제들 사이에 사이클이 있으면 메모이제이션을 사용할 수 있을까?
  • 8. POSTECH Computer Algorithm Team 1. 부분 문제를 정의한다. (F(n) = n번째 피보나치 수) 2. 부분 문제들과 관련된 점화식을 세운다. (F(n) = F(n – 1) + F(n – 2)) 3. 초항(base case)에 대해 문제를 해결한다. (F(1) = F(2) = 1) DP Solve 개요
  • 9. POSTECH Computer Algorithm Team DP는 다음과 같이 두 가지 방법으로 구현할 수 있다. 1. 상향식(반복문) 2. 하향식(재귀함수) 구현 구현
  • 10. Code Explanation POSTECH Computer Algorithm Team Code int num[1000001] = {0,}; int FS(int n){ if(num[n] != 0) return num[n]; if(n == 1) return num[1] = 1; if(n == 2) return num[2] = 1; return num[n] = f(n-1) + f(n-2); } 피보나치 수(하향식)
  • 11. Code Explanation POSTECH Computer Algorithm Team Code int main(void){ int n, num[10000001]; cin >> n; num[1] = 1; num[2] = 1; for(int i = 3; i <= n; i++){ num[i] = num[i-1] + num[i-2]; } cout << num[n] << ‘n’; } 피보나치 수(상향식)
  • 12. POSTECH Computer Algorithm Team 구현 - 하향식은 점화식과 똑같이 구현하면 되기 때문에 구현 난이도가 낮다. - 상햑식은 제일 작은 문제들부터 풀어나가는 식이기 때문에 점화식과는 방향이 반대여서, 구현이 어렵다. - 다만 함수를 계속 부르는 하향식에 비해, 상향식이 성능이 더 좋다. (물론 컴파일러 성능이 좋아진 현재, 의미가 없지만)
  • 13. POSTECH Computer Algorithm Team - https://www.acmicpc.net/problem/1463 - https://www.acmicpc.net/problem/9095 - https://www.acmicpc.net/problem/2579 연습 문제 연습 문제
  • 14. POSTECH Computer Algorithm Team - f(n) : n을 1로 만드는 최소 연산 수 - 점화식 : f(n) = min(f(n/3), f(n/2), f(n-1)) + 1 - Base cases : f(1) = 0, f(2) = 1, f(3) = 1 1로 만들기 연습 문제
  • 15. POSTECH Computer Algorithm Team - f(n) : 1, 2, 3으로 n을 만들 수 있는 방법의 수 - 점화식 : f(n) = f(n-1) + f(n-2) + f(n-3) - Base cases : f(1) = 1, f(2) = 2, f(3) = 4 1, 2, 3 더하기 연습 문제
  • 16. POSTECH Computer Algorithm Team - f(n) : n번째 계단까지 쌓을 수 있는 최대 점수 - 점화식 : f(n) = s(n) + max(s(n-1) + f(n-3),f(n-2)) - Base cases : f(1) = s(1), f(2) = s(1) + s(2), f(3) = max(s(1), s(2)) + s(3) 계단 오르기 연습 문제