<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Overlapping_subproblems</id>
	<title>Overlapping subproblems - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Overlapping_subproblems"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Overlapping_subproblems&amp;action=history"/>
	<updated>2026-10-26T02:20:44Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Overlapping_subproblems&amp;diff=5512104&amp;oldid=prev</id>
		<title>imported&gt;MusikBot II: Removing protection templates from unprotected page (more info)</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Overlapping_subproblems&amp;diff=5512104&amp;oldid=prev"/>
		<updated>2025-12-26T23:34:17Z</updated>

		<summary type="html">&lt;p&gt;Removing protection templates from unprotected page (&lt;a href=&quot;/wiki143/index.php?title=User:MusikBot_II/FixPP/FAQ&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;User:MusikBot II/FixPP/FAQ (page does not exist)&quot;&gt;more info&lt;/a&gt;)&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Previous revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 23:34, 26 December 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>imported&gt;MusikBot II</name></author>
	</entry>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Overlapping_subproblems&amp;diff=945434&amp;oldid=prev</id>
		<title>imported&gt;Frap: Add category</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Overlapping_subproblems&amp;diff=945434&amp;oldid=prev"/>
		<updated>2025-02-13T19:32:24Z</updated>

		<summary type="html">&lt;p&gt;Add category&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In [[computer science]], a [[Computational problem|problem]] is said to have &amp;#039;&amp;#039;&amp;#039;overlapping subproblems&amp;#039;&amp;#039;&amp;#039; if the problem can be broken down into subproblems which are reused several times or a recursive algorithm for the problem solves the same subproblem over and over rather than always generating new subproblems.&amp;lt;ref&amp;gt;[https://books.google.com/books?id=NLngYyWFl_YC&amp;amp;dq=introduction+to+algorithms&amp;amp;pg=PP1 Introduction to Algorithms], 2nd ed., (Cormen, Leiserson, Rivest, and Stein) 2001, p. 327. {{ISBN|0-262-03293-7}}.&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;[https://books.google.com/books?id=jUF9BAAAQBAJ&amp;amp;q=introduction+to+algorithms+3rd+edition Introduction to Algorithms], 3rd ed., (Cormen, Leiserson, Rivest, and Stein) 2014, p. 384. {{ISBN|9780262033848}}.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;[https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/video-lectures/lecture-13/ Dynamic Programming: Overlapping Subproblems, Optimal Substructure], MIT Video.&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example, the problem of computing the [[Fibonacci sequence]] exhibits overlapping subproblems.  The problem of computing the &amp;#039;&amp;#039;n&amp;#039;&amp;#039;th [[Fibonacci number]] &amp;#039;&amp;#039;F&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;), can be broken down into the subproblems of computing &amp;#039;&amp;#039;F&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;&amp;amp;minus;&amp;amp;nbsp;1) and &amp;#039;&amp;#039;F&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;&amp;amp;minus;&amp;amp;nbsp;2), and then adding the two.  The subproblem of computing &amp;#039;&amp;#039;F&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;&amp;amp;minus;&amp;amp;nbsp;1) can itself be broken down into a subproblem that involves computing&amp;amp;nbsp;&amp;#039;&amp;#039;F&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;&amp;amp;minus;&amp;amp;nbsp;2).  Therefore, the computation of &amp;#039;&amp;#039;F&amp;#039;&amp;#039;(&amp;#039;&amp;#039;n&amp;#039;&amp;#039;&amp;amp;nbsp;&amp;amp;minus;&amp;amp;nbsp;2) is reused, and the Fibonacci sequence thus exhibits overlapping subproblems.&lt;br /&gt;
&lt;br /&gt;
A naive [[recursive]] approach to such a problem generally fails due to an [[Exponential time|exponential complexity]]. If the problem also shares an [[optimal substructure]] property, [[dynamic programming]] is a good way to work it out.&lt;br /&gt;
&lt;br /&gt;
==Fibonacci sequence example==&lt;br /&gt;
In the following two implementations for calculating [[fibonacci sequence]], &amp;lt;code&amp;gt;fibonacci&amp;lt;/code&amp;gt; uses regular recursion and &amp;lt;code&amp;gt;fibonacci_mem&amp;lt;/code&amp;gt; uses [[memoization]]. &amp;lt;code&amp;gt;fibonacci_mem&amp;lt;/code&amp;gt; is much more efficient as the value for any particular &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; is computed only once. &lt;br /&gt;
{| style=&amp;quot;width: auto;&amp;quot;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def fibonacci(n):&lt;br /&gt;
    if n &amp;lt;= 1:&lt;br /&gt;
        return n&lt;br /&gt;
    return fibonacci(n - 1) + fibonacci(n - 2)&lt;br /&gt;
&lt;br /&gt;
def fibonacci_mem(n, cache):&lt;br /&gt;
    if n &amp;lt;= 1:&lt;br /&gt;
        return n&lt;br /&gt;
    if n in cache:&lt;br /&gt;
        return cache[n]&lt;br /&gt;
    cache[n] = fibonacci_mem(n - 1, cache) + fibonacci_mem(n - 2, cache)&lt;br /&gt;
    return cache[n]&lt;br /&gt;
&lt;br /&gt;
print(fibonacci_mem(5, {}))  # 5&lt;br /&gt;
print(fibonacci(5))  # 5&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
When executed, the &amp;lt;code&amp;gt;fibonacci&amp;lt;/code&amp;gt; function computes the value of some of the numbers in the sequence many times over, whereas &amp;lt;code&amp;gt;fibonacci_mem&amp;lt;/code&amp;gt; reuses the value of &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; which was computed previously:&lt;br /&gt;
{|&lt;br /&gt;
|- &lt;br /&gt;
! style=&amp;quot;vertical-align:top;&amp;quot; | Recursive Version&lt;br /&gt;
! style=&amp;quot;vertical-align:top;&amp;quot; | Memoization&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;vertical-align:top;&amp;quot; |&lt;br /&gt;
 f(5) = f(4) + f(3) = 5&lt;br /&gt;
        {{pipe}}      {{pipe}}&lt;br /&gt;
        {{pipe}}      f(3) = f(2) + f(1) = 2&lt;br /&gt;
        {{pipe}}             {{pipe}}      {{pipe}}&lt;br /&gt;
        {{pipe}}             {{pipe}}      f(1) = 1&lt;br /&gt;
        {{pipe}}             {{pipe}}&lt;br /&gt;
        {{pipe}}             f(2) = 1&lt;br /&gt;
        {{pipe}}&lt;br /&gt;
        f(4) = f(3) + f(2) = 3&lt;br /&gt;
               {{pipe}}      {{pipe}}&lt;br /&gt;
               {{pipe}}      f(2) = 1&lt;br /&gt;
               {{pipe}}&lt;br /&gt;
               f(3) = f(2) + f(1) = 2&lt;br /&gt;
                      {{pipe}}      {{pipe}}&lt;br /&gt;
                      {{pipe}}      f(1) = 1&lt;br /&gt;
                      {{pipe}}&lt;br /&gt;
                      f(2) = 1&lt;br /&gt;
| style=&amp;quot;vertical-align:top;&amp;quot; |&lt;br /&gt;
 f(5) = f(4) + f(3) = 5&lt;br /&gt;
        {{pipe}}      {{pipe}}&lt;br /&gt;
        f(4) = f(3) + f(2) = 3&lt;br /&gt;
               {{pipe}}      {{pipe}}&lt;br /&gt;
               f(3) = f(2) + f(1) = 2&lt;br /&gt;
                      {{pipe}}      {{pipe}}&lt;br /&gt;
                      {{pipe}}      f(1) = 1&lt;br /&gt;
                      {{pipe}}&lt;br /&gt;
                      f(2) = 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The difference in performance may appear minimal with an &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; value of 5; however, as &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; increases, the [[computational complexity]] of the original &amp;lt;code&amp;gt;fibonacci&amp;lt;/code&amp;gt; function grows exponentially. In contrast, the &amp;lt;code&amp;gt;fibonacci_mem&amp;lt;/code&amp;gt; version exhibits a more linear increase in complexity.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Dynamic programming]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Overlapping Subproblem}}&lt;br /&gt;
[[Category:Articles with example Python (programming language) code]]&lt;br /&gt;
[[Category:Dynamic programming]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Frap</name></author>
	</entry>
</feed>