SlideShare a Scribd company logo
Ch 09. 동적 계획법 테크닉
Dynamic Programming
PNU ALCALL 2017 SUMMER
1
반복적 동적 계획법
9.21
PNU ALCALL 2017 SUMMER
2
반복적 동적 계획법
Iterative Dynamic Programming
반복문(보통 for)을 쓰는 DP
PNU ALCALL 2017 SUMMER
3
반복적인
반복적 동적 계획법
PNU ALCALL 2017 SUMMER
4
의존성 파악이 쉬워야 하고
= 부분구조가 단순해야 한다
예제 : 삼각형 위의 최대 경로
PNU ALCALL 2017 SUMMER
5
@algospot.com
예제 : 삼각형 위의 최대 경로
PNU ALCALL 2017 SUMMER
6
@algospot.com
예제 : 삼각형 위의 최대 경로
PNU ALCALL 2017 SUMMER
7
Path2(y,x) = triangle[y,x] + max
𝑝𝑎𝑡ℎ2(𝑦 + 1, 𝑥)
𝑝𝑎𝑡ℎ2(𝑦 + 1, 𝑥 + 1)
메모이제이션 : Path2(y,x) 함수를 호출 -> Memo[y, x]의 유무 확인 -> 결과값 반환 (Top-down)
반복적 DP : C[y,x]에 바로 값을 대입하여 사용
( = Path2(y,x)를 계산하기 위한 값들(Path2(y+1,x+1), Path2(y+1,x))이 이미 계산 되어있음. )
(Bottom-up)
예제 : 삼각형 위의 최대 경로
PNU ALCALL 2017 SUMMER
8
슬라이딩 윈도우
PNU ALCALL 2017 SUMMER
9
6
1 2
3 7 4
9 4 1 7
2 7 5 9 4
16 11 10 16
19 18 20
20 22
28
계산순서가 일정하므로 가능
슬라이딩 윈도우
PNU ALCALL 2017 SUMMER
10
피보나치 마지막 5자리 DP
PNU ALCALL 2017 SUMMER
11
피보나치 마지막 5자리 DP
PNU ALCALL 2017 SUMMER
12
𝐶𝑖 =
𝑓𝑖𝑏(𝑖 − 1)
𝑓𝑖𝑏(𝑖)
𝐶𝑖+1 =
𝑓𝑖𝑏(𝑖)
𝑓𝑖𝑏(𝑖 + 1)
=
𝑓𝑖𝑏(𝑖)
𝑓𝑖𝑏 𝑖 − 1 + 𝑓𝑖𝑏(𝑖)
=
𝐶𝑖,1
𝐶𝑖,0 + 𝐶𝑖,1
=
0 1
1 1
𝐶𝑖
𝐶 𝑛 =
0 1
1 1
𝑛−1
𝐶1
피보나치 마지막 5자리 DP
PNU ALCALL 2017 SUMMER
13
𝑊 =
0 1
1 1
𝑊 𝑛
= 𝑊 𝑛/2
× 𝑊 𝑛/2
O(log n)
일반화
PNU ALCALL 2017 SUMMER
14
• 𝑛 × 𝑚 행렬 A의 선형 결합으로 정의되는 DP 알고리즘
의 시간복잡도는O(𝑛𝑚2)
• 행렬의 빠른 거듭제곱을 사용하면 O(𝑚3 log 𝑛)
• M의 크기에 따라서 취사선택
재귀함수 vs 반복문
PNU ALCALL 2017 SUMMER
15
• 재귀함수의 장단점
장점 : 직관적인 코드. 의존관계나 계산순서에 무관.
단점 : 공간낭비가 심함. Call Stack Overflow 발생.
• 반복문의 장단점
장점 : 짧은 코드. Call Stack 부담 X. 공간 절약(슬라이딩 윈
도)
단점 : 구현이 비직관적이고 계산순서에 영향을 많이 받음.
문제 : 회전초밥
9.22
PNU ALCALL 2017 SUMMER
16
문제 설명
PNU ALCALL 2017 SUMMER
17
문제 설명
PNU ALCALL 2017 SUMMER
18
문제 : 회전초밥 풀이
9.22
PNU ALCALL 2017 SUMMER
19
Knapsack
PNU ALCALL 2017 SUMMER
20
• 배낭 채우기 문제와 유사
정해진 무게를 넘지 않으면서 최대 가치를 찾기
VS
정해진 예산를 넘지 않으면서 최대 선호도를 찾기
차이점
PNU ALCALL 2017 SUMMER
21
• 중복된 초밥을 가져갈 수 있음
• M < 10^9 인데 필요한 DP 테이블 크기 : N x M
-> 슬라이딩 윈도우로 배열크기를 줄이는게 관건
슬라이딩 윈도우
PNU ALCALL 2017 SUMMER
22
• T[i][j] = i번 초밥만큼 경우가 있을 때 j원으로 먹을 수 있는 최대 금액
테이블 크기 20 * 10^9 (20000MB)
• i:0->N
Prev[j] : i-1 번 초밥까지 먹었을 때 최대 선호도
T[j] : i번초밥까지 먹었을 때 최대 선호도
T[j] = max(T[j],T[j – price[i]]+ price[i]])
• 가격의 단위가 100이므로 100으로 나눠준 값을 index로 사용
소스코드
PNU ALCALL 2017 SUMMER
23
PNU ALCALL 2017 SUMMER
24
Q&A
FIN

More Related Content

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Dynamic Programming

  • 1. Ch 09. 동적 계획법 테크닉 Dynamic Programming PNU ALCALL 2017 SUMMER 1
  • 2. 반복적 동적 계획법 9.21 PNU ALCALL 2017 SUMMER 2
  • 3. 반복적 동적 계획법 Iterative Dynamic Programming 반복문(보통 for)을 쓰는 DP PNU ALCALL 2017 SUMMER 3 반복적인
  • 4. 반복적 동적 계획법 PNU ALCALL 2017 SUMMER 4 의존성 파악이 쉬워야 하고 = 부분구조가 단순해야 한다
  • 5. 예제 : 삼각형 위의 최대 경로 PNU ALCALL 2017 SUMMER 5 @algospot.com
  • 6. 예제 : 삼각형 위의 최대 경로 PNU ALCALL 2017 SUMMER 6 @algospot.com
  • 7. 예제 : 삼각형 위의 최대 경로 PNU ALCALL 2017 SUMMER 7 Path2(y,x) = triangle[y,x] + max 𝑝𝑎𝑡ℎ2(𝑦 + 1, 𝑥) 𝑝𝑎𝑡ℎ2(𝑦 + 1, 𝑥 + 1) 메모이제이션 : Path2(y,x) 함수를 호출 -> Memo[y, x]의 유무 확인 -> 결과값 반환 (Top-down) 반복적 DP : C[y,x]에 바로 값을 대입하여 사용 ( = Path2(y,x)를 계산하기 위한 값들(Path2(y+1,x+1), Path2(y+1,x))이 이미 계산 되어있음. ) (Bottom-up)
  • 8. 예제 : 삼각형 위의 최대 경로 PNU ALCALL 2017 SUMMER 8
  • 9. 슬라이딩 윈도우 PNU ALCALL 2017 SUMMER 9 6 1 2 3 7 4 9 4 1 7 2 7 5 9 4 16 11 10 16 19 18 20 20 22 28 계산순서가 일정하므로 가능
  • 11. 피보나치 마지막 5자리 DP PNU ALCALL 2017 SUMMER 11
  • 12. 피보나치 마지막 5자리 DP PNU ALCALL 2017 SUMMER 12 𝐶𝑖 = 𝑓𝑖𝑏(𝑖 − 1) 𝑓𝑖𝑏(𝑖) 𝐶𝑖+1 = 𝑓𝑖𝑏(𝑖) 𝑓𝑖𝑏(𝑖 + 1) = 𝑓𝑖𝑏(𝑖) 𝑓𝑖𝑏 𝑖 − 1 + 𝑓𝑖𝑏(𝑖) = 𝐶𝑖,1 𝐶𝑖,0 + 𝐶𝑖,1 = 0 1 1 1 𝐶𝑖 𝐶 𝑛 = 0 1 1 1 𝑛−1 𝐶1
  • 13. 피보나치 마지막 5자리 DP PNU ALCALL 2017 SUMMER 13 𝑊 = 0 1 1 1 𝑊 𝑛 = 𝑊 𝑛/2 × 𝑊 𝑛/2 O(log n)
  • 14. 일반화 PNU ALCALL 2017 SUMMER 14 • 𝑛 × 𝑚 행렬 A의 선형 결합으로 정의되는 DP 알고리즘 의 시간복잡도는O(𝑛𝑚2) • 행렬의 빠른 거듭제곱을 사용하면 O(𝑚3 log 𝑛) • M의 크기에 따라서 취사선택
  • 15. 재귀함수 vs 반복문 PNU ALCALL 2017 SUMMER 15 • 재귀함수의 장단점 장점 : 직관적인 코드. 의존관계나 계산순서에 무관. 단점 : 공간낭비가 심함. Call Stack Overflow 발생. • 반복문의 장단점 장점 : 짧은 코드. Call Stack 부담 X. 공간 절약(슬라이딩 윈 도) 단점 : 구현이 비직관적이고 계산순서에 영향을 많이 받음.
  • 16. 문제 : 회전초밥 9.22 PNU ALCALL 2017 SUMMER 16
  • 17. 문제 설명 PNU ALCALL 2017 SUMMER 17
  • 18. 문제 설명 PNU ALCALL 2017 SUMMER 18
  • 19. 문제 : 회전초밥 풀이 9.22 PNU ALCALL 2017 SUMMER 19
  • 20. Knapsack PNU ALCALL 2017 SUMMER 20 • 배낭 채우기 문제와 유사 정해진 무게를 넘지 않으면서 최대 가치를 찾기 VS 정해진 예산를 넘지 않으면서 최대 선호도를 찾기
  • 21. 차이점 PNU ALCALL 2017 SUMMER 21 • 중복된 초밥을 가져갈 수 있음 • M < 10^9 인데 필요한 DP 테이블 크기 : N x M -> 슬라이딩 윈도우로 배열크기를 줄이는게 관건
  • 22. 슬라이딩 윈도우 PNU ALCALL 2017 SUMMER 22 • T[i][j] = i번 초밥만큼 경우가 있을 때 j원으로 먹을 수 있는 최대 금액 테이블 크기 20 * 10^9 (20000MB) • i:0->N Prev[j] : i-1 번 초밥까지 먹었을 때 최대 선호도 T[j] : i번초밥까지 먹었을 때 최대 선호도 T[j] = max(T[j],T[j – price[i]]+ price[i]]) • 가격의 단위가 100이므로 100으로 나눠준 값을 index로 사용
  • 24. PNU ALCALL 2017 SUMMER 24 Q&A FIN