ðåçèšç»æ³ã¯ç·åœããã§ãã¯ã¿ãããªãã®ã§ããïŒ
åçèšç»æ³ïŒDynamic Programming, DPïŒã¯ç·åœãããã§ãã¯ïŒBrute ForceïŒãšã¯ç°ãªãã¢ã«ãŽãªãºã ã®èšèšææ³ã§ããäž¡è ã¯åé¡ã解決ããããã®ã¢ãããŒãã«ãããŠæ ¹æ¬çãªéãããããŸãã
ç·åœãããã§ãã¯ã¯ãåé¡ã®å¯èœãªãã¹ãŠã®è§£ãçæãããã®äžããæé©ãªè§£ãéžæããæ¹æ³ã§ãããã®ã¢ãããŒãã¯éåžžã«ã·ã³ãã«ã§çŽæçã§ãããåé¡ã®ãµã€ãºã倧ãããªããšèšç®æéãææ°é¢æ°çã«å¢å ãããããå®çšçã§ã¯ãªãå Žåãå€ãã§ãã
åçèšç»æ³ã¯ãè€éãªåé¡ãããå°ããªãµããããã¬ã ã«åå²ãããããã®ãµããããã¬ã ã®è§£ãèšæ¶ããŠåå©çšããããšã§ãå¹ççã«å šäœã®åé¡ã解決ããæ¹æ³ã§ããDPã¯ãã¡ã¢åãããããã ã¢ããããªã©ã®ãã¯ããã¯ã䜿çšããŠãèšç®ã®éè€ãé¿ããå¹ççã«åé¡ã解決ããŸããåçèšç»æ³ã¯ç¹ã«ãæé©ååé¡ãã«ãŠã³ãåé¡ã確çåé¡ãªã©ã«æå¹ã§ãããèšç®æéãå€§å¹ ã«åæžã§ããå ŽåããããŸãã
ã€ãŸããåçèšç»æ³ã¯åé¡ã解ãããã«èšç®ã®éè€ãé¿ããããšã§å¹çãé«ããã®ã«å¯Ÿããç·åœãããã§ãã¯ã¯å¯èœãªè§£ããã¹ãŠè©Šãããšã«ãã£ãŠåé¡ã解決ããŸãããã®ãããåçèšç»æ³ã¯ç·åœãããã§ãã¯ãããèšç®å¹çãè¯ãããšãå€ãã§ãã
åå²ããŠç·åœããé¿ããŠããããšã¿ãããšã¯ã§ããŸããïŒ
ã¯ãããã®ããã«èŠãããšãã§ããŸããåçèšç»æ³ã¯ãåé¡ãããå°ããªãµããããã¬ã ã«åå²ããããããã®ãµããããã¬ã ãäžåºŠã ã解ãããã®çµæãä¿åããŠåå©çšããããšã«ãã£ãŠãåãèšç®ãç¹°ãè¿ãè¡ãããšïŒç·åœããïŒãé¿ããŸãããã®ããã»ã¹ã«ãããèšç®ã®å¹çã倧å¹
ã«åäžããŸãã
ç·åœãããé¿ããããã®ããŒã¯ãã¡ã¢åããšåŒã°ãããã¯ããã¯ã§ããã¡ã¢åã¯ããµããããã¬ã ã®è§£ãèšç®åŸã«èšæ¶ããŠãããåããµããããã¬ã ã«åã³ééããéã«ã¯èšæ¶ããŠããã解ãåå©çšããããšã§ããããã«ãããåããµããããã¬ã ãäœåºŠã解ãå¿
èŠããªããªããèšç®éã倧å¹
ã«åæžã§ããŸãã
åå²ããŠç·åœãããé¿ãããšããèŠç¹ã¯ãåçèšç»æ³ã®å¹çã®è¯ããç解ããäžã§éèŠã§ããåçèšç»æ³ã¯ãèšç®ãå¿
èŠæå°éã«æããããšã§ããã倧ããªåé¡ã«å¯ŸããŠãå®çšçãªæéå
ã«è§£ãèŠã€ãåºãããšãå¯èœã«ãªããŸããããã¯ãç¹ã«èšç®ãªãœãŒã¹ãéãããŠããå Žåã«éåžžã«æå¹ãªã¢ãããŒãã§ãã
匷ååŠç¿ãåãã§ãç·åœãã§ã¯ãªãã§ããïŒ
ã¯ãããã®éãã§ãã匷ååŠç¿ã¯ãç·åœããæ¢çŽ¢ãšã¯ç°ãªãã¢ãããŒããæ¡çšããŠããŸãã匷ååŠç¿ã¯ããšãŒãžã§ã³ããç°å¢ãšçžäºäœçšããªããåŠç¿ããå ±é ¬ãæ倧åãããããªè¡åæ¹éïŒããªã·ãŒïŒãåŠç¿ããäžçš®ã®æ©æ¢°åŠç¿ã®æ¹æ³ã§ãããã®ããã»ã¹ã¯ãè©Šè¡é¯èª€ãéããŠè¡ãããç·åœããæ¢çŽ¢ãããã¯ããã«å¹ççã§ãã
匷ååŠç¿ã§ã¯ããšãŒãžã§ã³ãã¯åæç¹ã§ã®ç¶æ ã«åºã¥ããŠè¡åãéžæãããã®è¡åã®çµæãšããŠåŸãããå ±é ¬ïŒãŸãã¯ããã«ãã£ïŒã芳å¯ããŸãããšãŒãžã§ã³ãã¯ããã®ãã£ãŒãããã¯ãå©çšããŠãå ±é ¬ãæ倧åããè¡åæ¹éãåŠç¿ããŸãããã®ããã»ã¹ã«ããããšãŒãžã§ã³ãã¯æçãªè¡åã匷åããäžå©çãªè¡åãé¿ããããã«ãªããŸãã
匷ååŠç¿ã§ã¯ãå¯èœãªè¡åã®çµã¿åãããèšå€§ã§ããå Žåã§ããå¹ççãªåŠç¿ãå¯èœã§ãããšãŒãžã§ã³ãã¯ãå šãŠã®å¯èœæ§ãè©Šãã®ã§ã¯ãªããçµéšãšãã£ãŒãããã¯ããæé©ãªè¡åãèŠã€ãåºããŸãããããã£ãŠã匷ååŠç¿ã¯ç·åœããæ¢çŽ¢ãšã¯æ ¹æ¬çã«ç°ãªããåŠç¿ããã»ã¹ãéããŠå¹ççãªåé¡è§£æ±ºæŠç¥ãå°ãåºããŸãã
åçèšç»æ³ããã®ä»ã®æé©åæè¡ãšåæ§ã«ã匷ååŠç¿ãåé¡ã解決ããããã®å¹ççãªæ段ãæäŸããŸããããã®çŠç¹ã¯ãšãŒãžã§ã³ããç°å¢ããåŠç¿ããæéãšãšãã«è¡åãæ¹åããããšã«ãããŸãã
åçèšç»æ³ïŒDynamic Programming, DPïŒãç·åœããïŒBrute ForceïŒã¢ãããŒãããå¹ççã§ãããšãããæ ¹æ ã«ã¯ãç¹ã«ãéè€ããéšååé¡ïŒOverlapping SubproblemsïŒãã®è§£æ±ºãšãæé©éšåæ§é ïŒOptimal SubstructureïŒãã®å©çšãæããããŸãã
éè€ããéšååé¡
åçèšç»æ³ã¯ãéè€ããéšååé¡ã解ãã®ã«éåžžã«æå¹ã§ããç·åœããã¢ãããŒãã§ã¯ãåãåé¡ãäœåºŠãç¹°ãè¿ã解ãå¿ èŠããããŸãããåçèšç»æ³ã§ã¯äžåºŠèšç®ããå€ãã¡ã¢åïŒã¡ã¢ãªã«ä¿åïŒããããšã§ãåãèšç®ãç¹°ãè¿ãè¡ãå¿ èŠããªãããŸããããã«ããèšç®æéãå€§å¹ ã«åæžãããŸãã
æé©éšåæ§é
æé©éšåæ§é ã¯ãåé¡ã®æé©è§£ããã®éšååé¡ã®æé©è§£ããæ§æã§ãããšããæ§è³ªã§ããã€ãŸãã倧ããªåé¡ã®æé©è§£ãèŠã€ããããã«ããããæ§æããå°ããªåé¡ã®æé©è§£ãçµã¿åãããããšãã§ããŸããåçèšç»æ³ã§ã¯ãã®æ§è³ªãå©çšããŠãå°ããªåé¡ããé ã«è§£ãæ§ç¯ãã倧ããªåé¡ãžãšæ¡å€§ããŠãããŸãã
èšç®å¹ç
åçèšç»æ³ã¯ãåé¡ã«ãã£ãŠã¯ææ°é¢æ°çãªæéããããç·åœããã¢ãããŒããå€é åŒæéã«åæžããããšãã§ããŸããäŸãã°ããã£ããããæ°åã®èšç®ãããããµãã¯åé¡ãªã©ã§ãã®å¹æãæ確ã«èŠãããšãã§ããŸãã
ç·åœããã¢ãããŒããå šãŠã®å¯èœæ§ãè©Šãã®ã«å¯Ÿããåçèšç»æ³ã¯èšç®éçšã§åŸãããæ å ±ãå¹æçã«åå©çšããäžå¿ èŠãªèšç®ãçç¥ããŸãããã®ãããèšç®è³æºãç¯çŽããããé«éã«åé¡ã解決ããããšãå¯èœã«ãªããŸãããã®ããã«ãåçèšç»æ³ã¯ç¹å®ã®çš®é¡ã®åé¡ã«ãããŠèšç®å¹çãè¯ããããç·åœãããããåªããæ¹æ³ãšãããŠããŸãã
åçèšç»æ³ã«ãããåå©çšã®ã¹ãããããå žåçãªåé¡ã§ãããã£ããããæ°åã®èšç®ãäŸã«ããŠæ¬äŒŒã³ãŒãã§èª¬æããŸãããã£ããããæ°åã¯ã`F(n) = F(n-1) + F(n-2)` ãšããååž°çãªé¢ä¿ã§å®çŸ©ãããŸããããã®åé¡ãåçèšç»æ³ã§å¹ççã«è§£ãæ¹æ³ã¯ãã¡ã¢åååž°ããšãããã ã¢ãããã®2ã€ã®ã¢ãããŒãããããŸãã
ã¡ã¢åååž° (Top-Down Approach)
ã¡ã¢åååž°ã§ã¯ãååž°çãªæ§é ãå©çšããªããããã§ã«èšç®ããçµæãã¡ã¢ãªã«ä¿åãïŒã¡ã¢åïŒãåãåé¡ãåèšç®ããéã«ã¯ä¿åãããå€ãåå©çšããŸããããã«ããèšç®ã®éè€ãé¿ããå¹ççã«åé¡ã解決ã§ããŸãã
function fibonacci(n):
if memo[n] is not null:
return memo[n] # æ¢ã«èšç®ãããå€ãè¿ã
if n <= 1:
return n
memo[n] = fibonacci(n-1) + fibonacci(n-2) # çµæãã¡ã¢å
return memo[n]
# ã¡ã¢é
åã®åæå
memo = array of size n+1 with all values set to null
ããã ã¢ãã (Bottom-Up Approach)
ããã ã¢ããã¢ãããŒãã§ã¯ãå°ãããµããããã¬ã ããå§ããŠããããã®è§£ãå©çšããŠæ®µéçã«å€§ããªåé¡ã®è§£ãæ§ç¯ããŸããåã¹ãããã§åŸãããçµæã¯é åã«ä¿åãããåŸç¶ã®èšç®ã§åå©çšãããŸãã
function fibonacci(n):
if n <= 1:
return n
dp = array of size n+1
dp[0] = 0 # åºåºæ¡ä»¶
dp[1] = 1 # åºåºæ¡ä»¶
for i from 2 to n:
dp[i] = dp[i-1] + dp[i-2] # åã®2ã€ã®å€ãå©çšããŠæ°ããå€ãèšç®
return dp[n]
# é
å dp ã䜿ã£ãŠåãã£ããããæ°ãèšç®ããŠæ ŒçŽããå¿
èŠãªãšãã« O(1) ã§ã¢ã¯ã»ã¹å¯èœ
ãã®2ã€ã®ã¢ãããŒãã¯ã©ã¡ããèšç®æžã¿ã®çµæãåå©çšããç¹ã§å ±éããŠããŸãããã¡ã¢åååž°ã¯å¿ èŠã«å¿ããŠã®ã¿èšç®ãè¡ããããã ã¢ããã¯ãã¹ãŠã®å€ãäžåºŠã«èšç®ããŸããã©ã¡ãã®æ¹æ³ãèšç®ã®éè€ãé¿ããããšã«ãããç·åœãããããå¹çãè¯ããªã£ãŠããŸãã