<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Ask Ghassem - Recent activity in Data Structures and Algorithms</title>
<link>https://ask.ghassem.com/activity/data-science/data-structures-and-algorithms</link>
<description>Powered by Question2Answer</description>
<item>
<title>Answer selected: How should I count the number of operations in an algorithm?</title>
<link>https://ask.ghassem.com/282/how-should-i-count-the-number-of-operations-in-an-algorithm?show=283#a283</link>
<description>&lt;p&gt;For $n&amp;lt;10$ the answer is obvious because the program stops at &quot;&lt;strong&gt;return 0;&lt;/strong&gt;&quot;. for $n \ge 10$, please start&amp;nbsp;with $n=10$ and run the algorithm by yourself and count how many times it will occur. The Python implementation is as follows. In the following code, you can see the lines that should be counted have labels&amp;nbsp;of $c1, c2, c3, c4, c5, c6$ .&amp;nbsp;You can see and execute&amp;nbsp;the full program&amp;nbsp;&lt;strong&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://repl.it/@tofighi/number-of-operations&quot;&gt;here&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;pre class=&quot;prettyprint lang-python&quot; data-pbcklang=&quot;python&quot; data-pbcktabsize=&quot;4&quot;&gt;
n = 10
s= 0
count = 0
i = 0
while(True):  #c1
  for i in range(5, count+1): #c2
    s = s+i #c3
  if (count &amp;gt; n): #c4
    break #c5
  count = count + 1 #c6
print(s)&lt;/pre&gt;

&lt;p&gt;If you run the algorithm by yourself on paper, you will have the following table. In this table, we start for $n=10$, and under each label such a $c1$ we have written&amp;nbsp;&lt;strong&gt;x&amp;nbsp;&lt;/strong&gt;if the line is executed, and the number in front of &lt;strong&gt;x&lt;/strong&gt; is the number of execution. For example, for $c3$, when $count=9$ in the row, we have&quot;&lt;strong&gt;x 5&lt;/strong&gt;&quot; which means $s=s+i$ executed 5 times (look at for condition: for i = 5 to count , and count is 9, so the body of for loop,$s=s+i$ ,will be executed 5 times).&lt;/p&gt;

&lt;p&gt;In the total row, we counted how many times in total for $n=10$ the lines are executed and tried to draw a general conclusion, based on $n$. For example for $c1$, and for $n=10$ we counted&amp;nbsp;$12$ execution, so it is probably $n+2$ in a general situation (you can prove it using &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Mathematical_induction&quot;&gt;&lt;strong&gt;induction&lt;/strong&gt;&lt;/a&gt;). For $c3$, it will be summations of numbers from $1$ to $n-3$, as we see&amp;nbsp;in the case of $n=10$ it will be $1+2+3+4+5+6+7=\frac{7\times8}{2}$, because &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_⋯&quot;&gt;we have&lt;/a&gt; $1+2+3+...+(n-1)+n = \frac{n(n+1)}{2}$. Therefore, for any n, it seems for $c3$ we should calculate the sum of natural numbers until $n-3$ (as for 10 we sum&amp;nbsp;up up to 7). If you replace $n-3$ in the equation, you will have $1+2+3+....+(n-4)+(n-3) =&amp;nbsp;\frac{(n-3)(n-2)}{2}$&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot;&gt;
	&lt;tbody&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;n&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;count&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;c1&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;c2&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;&amp;nbsp; c3&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;c4&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;&amp;nbsp; c5&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&lt;strong&gt;c6&lt;/strong&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;10&lt;/td&gt;
			&lt;td&gt;0&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;2&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;3&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;4&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;5&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;6&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 2&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;7&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 3&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;8&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 4&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;9&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 5&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;10&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 6&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;11&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 7&lt;/td&gt;
			&lt;td&gt;x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; x 1&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;strong&gt;Total:&lt;/strong&gt;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;$12$&lt;/td&gt;
			&lt;td&gt;$12$&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;$1+2+…+6+7 = \frac{7\times8}{2}=28$&lt;/td&gt;
			&lt;td&gt;$12$&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; $1$&lt;/td&gt;
			&lt;td&gt;$11$&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;&lt;/td&gt;
			&lt;td&gt;$n+2$&lt;/td&gt;
			&lt;td&gt;$n+2$&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;$1+2+...+(n-4)+(n-3) = \frac{(n-3)(n-2)}{2}$&lt;/td&gt;
			&lt;td&gt;$n+2$&lt;/td&gt;
			&lt;td&gt;&amp;nbsp; $1$&lt;/td&gt;
			&lt;td&gt;&amp;nbsp;$n+1$&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;</description>
<category>Data Structures and Algorithms</category>
<guid isPermaLink="true">https://ask.ghassem.com/282/how-should-i-count-the-number-of-operations-in-an-algorithm?show=283#a283</guid>
<pubDate>Sat, 06 Oct 2018 15:00:53 +0000</pubDate>
</item>
</channel>
</rss>