<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>time complexity Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/time-complexity/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/time-complexity/</link>
	<description></description>
	<lastBuildDate>Thu, 08 Feb 2018 16:21:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>time complexity Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/time-complexity/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱  Time/Space Complexity of Recursion Functions SP4</title>
		<link>https://zxi.mytechroad.com/blog/sp/time-space-complexity-of-recursion-functions-sp4/</link>
					<comments>https://zxi.mytechroad.com/blog/sp/time-space-complexity-of-recursion-functions-sp4/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 08 Feb 2018 04:48:37 +0000</pubDate>
				<category><![CDATA[SP]]></category>
		<category><![CDATA[recusion]]></category>
		<category><![CDATA[space complexity]]></category>
		<category><![CDATA[time complexity]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1777</guid>

					<description><![CDATA[<p>&#160; How to analyze the time and space complexity of a recursion function? 如何分析一个递归函数的时间/空间复杂度？ We can answer that using master theorem or induction in most&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sp/time-space-complexity-of-recursion-functions-sp4/">花花酱  Time/Space Complexity of Recursion Functions SP4</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/OQi4n8EKRD8?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>&nbsp;</p>
<p>How to analyze the time and space complexity of a recursion function?</p>
<p>如何分析一个递归函数的时间/空间复杂度？</p>
<p>We can answer that using <a href="https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)">master theorem</a> or induction in most of the cases.</p>
<p>在大部分时候我们可以使用主方法和数学归纳法来进行解答。</p>
<p>First of all, we need to write down the recursion relation of a function.</p>
<p>首先我们要写出一个函数的递归表达式。</p><pre class="crayon-plain-tag">def func(n):
  if n &lt; 0: return 1
  return func(n/2) + func(n/2)</pre><p>Let&#8217;s use T(n) to denote the running time of func with input size of n.</p>
<p>让我们用T(n)来表示func函数在输入规模为n的运行时间。</p>
<p>Then we have：</p>
<p>那么我们有：</p>
<p>T(n) = 2*T(n/2) + O(1)</p>
<p>a = 2, b = 2, c_crit = logb(a) = 1, f(n) = n^c, c = 0.</p>
<p>c &lt; c_crit, apply master theorem case 1:</p>
<p>根据主方法第一条我们得到</p>
<p>T(n) =  Θ(n^c_crit) = Θ(n)</p>
<p>&nbsp;</p>
<p>Let&#8217;s look at another example:</p><pre class="crayon-plain-tag">def func(n):
  if n &lt; 0: return 1
  s = 0
  for i in range(n):
    s += i
  return s + func(n/2) + func(n/2)</pre><p>T(n) = 2*T(n/2) + O(n)</p>
<p>a = 2, b = 2, c_crit = logb(a) = 1, f(n) = n^c, c = 1,</p>
<p>c = c_crit, apply master theorem case 2:</p>
<p>根据主方法第二条我们得到</p>
<p>T(n) =Θ(n^c_crit * (logn)^1)) = Θ(nlogn)</p>
<p><strong>Cheatsheet</strong></p>
<table>
<tbody>
<tr>
<th>Equation</th>
<th>Time</th>
<th>Space</th>
<th>Examples</th>
</tr>
<tr>
<td>T(n) = 2*T(n/2) + O(n)</td>
<td>O(nlogn)</td>
<td>O(logn)</td>
<td>quick_sort</td>
</tr>
<tr>
<td>T(n) = 2*T(n/2) + O(n)</td>
<td>O(nlogn)</td>
<td>O(n + logn)</td>
<td>merge_sort</td>
</tr>
<tr>
<td>T(n) = T(n/2) + O(1)</td>
<td>O(logn)</td>
<td>O(logn)</td>
<td>Binary search</td>
</tr>
<tr>
<td>T(n) = 2*T(n/2) + O(1)</td>
<td>O(n)</td>
<td>O(logn)</td>
<td>Binary tree traversal</td>
</tr>
<tr>
<td>T(n) = T(n-1) + O(1)</td>
<td>O(n)</td>
<td>O(n)</td>
<td>Binary tree traversal</td>
</tr>
<tr>
<td>T(n) = T(n-1) + O(n)</td>
<td>O(n^2)</td>
<td>O(n)</td>
<td>quick_sort(worst case)</td>
</tr>
<tr>
<td>T(n) = n * T(n-1)</td>
<td>O(n!)</td>
<td>O(n)</td>
<td>permutation</td>
</tr>
<tr>
<td>T(n) = T(n-1)+T(n-2)+&#8230;+T(1)</td>
<td>O(2^n)</td>
<td>O(n)</td>
<td>combination</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p><strong>For recursion with memorization:</strong></p>
<p>Time complexity: |# of subproblems| * |exclusive running time of a subproblem|</p>
<p>Space complexity:|# of subproblems|  + |max recursion depth| * |space complexity of a subproblem|</p>
<p><strong>Example 1:</strong></p><pre class="crayon-plain-tag">def fib(n):
  if n &lt; 1: return 1
  if m[n]: return m[n]
  m[n] = fib(n - 1) + fib(n - 2)
  return m[n]</pre><p>To solve fib(n), there are n subproblems fib(0), fib(1), &#8230;, fib(n)</p>
<p>each sub problem takes O(1) to solve</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n) + O(n) * O(1) = O(n)</p>
<p><strong>Example 2:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-741-cherry-pickup/">LC 741 Cherry Pickup</a></p><pre class="crayon-plain-tag">dp(x1, y1, x2):
 if min(x1, y1, x2) &lt; 0: return 0
 if m[x1][y1][x2]: return m[x1][y1][x2]
 ans = max(dp(x1 - 1, y1, x2 - 1), 
           dp(x1, y1 - 1, x2),
           dp(x1, y1 - 1, x2 - 1), 
           dp(x1 - 1, y1, x2))
 m[x1][y1][x2] = ans
 return m[x1][y1][x2]</pre><p>To solve dp(n, n, n), there are n^3 subproblems</p>
<p>each subproblem takes O(1) to solve</p>
<p>Max recursion depth O(n)</p>
<p>Time complexity: O(n^3) * O(1) = O(n^3)</p>
<p>Space complexity: O(n^3) + O(n) * O(1) = O(n^3)</p>
<p><strong>Example 3:</strong></p>
<p><a href="http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-312-burst-balloons/">LC 312: Burst Balloon</a></p><pre class="crayon-plain-tag">dp(i, j):
 if m[i][j]: return m[i][j]
 for k in range(i, j + 1):
   ans = max(ans, dp(i, k - 1) + C + dp(k + 1, j))
 m[i][j] = ans
 return m[i][j]</pre><p>To solve dp(0, n), there are n^2 subproblems dp(0, 0), dp(0, 1), &#8230;, dp(n-1, n)</p>
<p>each subproblem takes O(n) to solve</p>
<p>Max recursion depth O(n)</p>
<p>Time complexity: O(n^2) * O(n) = O(n^3)</p>
<p>Space complexity: O(n^2) + O(n) * O(1) = O(n^2)</p>
<p><strong>Slides:</strong></p>
<p><img class="alignnone size-full wp-image-1783" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-1.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-1781" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-3.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /> <img class="alignnone size-full wp-image-1782" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-2.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>&nbsp;</p>
<p><img class="alignnone size-full wp-image-1780" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-4.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-4.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-4-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-4-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-1779" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-5.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-5.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-5-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-5-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p><img class="alignnone size-full wp-image-1778" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-6.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-6.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-6-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2018/02/sp4-6-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/sp/time-space-complexity-of-recursion-functions-sp4/">花花酱  Time/Space Complexity of Recursion Functions SP4</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/sp/time-space-complexity-of-recursion-functions-sp4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
