<?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/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>analysis of algorithms Archives - Dimitris Dimitriadis</title>
	<atom:link href="https://dndimitri.eu/tag/analysis-of-algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>https://dndimitri.eu/tag/analysis-of-algorithms/</link>
	<description>Researcher &#38; Developer</description>
	<lastBuildDate>Tue, 03 Oct 2023 18:43:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.3</generator>

<image>
	<url>https://i0.wp.com/dndimitri.eu/wp-content/uploads/2023/10/cropped-1696348391588_2-scaled-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>analysis of algorithms Archives - Dimitris Dimitriadis</title>
	<link>https://dndimitri.eu/tag/analysis-of-algorithms/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">224285271</site>	<item>
		<title>Estimating Running Time of an Algorithm using Simple Rules.</title>
		<link>https://dndimitri.eu/2020/08/23/estimating-running-time-of-an-algorithm-using-simple-rules/</link>
					<comments>https://dndimitri.eu/2020/08/23/estimating-running-time-of-an-algorithm-using-simple-rules/#respond</comments>
		
		<dc:creator><![CDATA[Dimitris Dimitriadis]]></dc:creator>
		<pubDate>Sun, 23 Aug 2020 17:43:02 +0000</pubDate>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[analysis of algorithms]]></category>
		<guid isPermaLink="false">https://dndimitri.eu/?p=586</guid>

					<description><![CDATA[<p>To estimate the running time of an algorithm and the difference of the algorithm against others, we make a strong...</p>
<p>The post <a rel="nofollow" href="https://dndimitri.eu/2020/08/23/estimating-running-time-of-an-algorithm-using-simple-rules/">Estimating Running Time of an Algorithm using Simple Rules.</a> appeared first on <a rel="nofollow" href="https://dndimitri.eu">Dimitris Dimitriadis</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>To estimate the running time of an algorithm and the difference of the algorithm against others, we make a strong assumption where the running time of an algorithm is independent of specific hardware. Using this assumption, we can study the efficiency of the algorithms and compare the algorithms to each other, deciding which one is better and finding an optimal solution to a problem.</p>



<p>Following the assumption of hardware independence, the analysis of the running time of an algorithm depends only on the statements defined in the algorithm (i.e., the steps that the algorithms need to follow for solving a specific problem). Since the execution time of the algorithms&#8217; statements is not the same for all of them (i.e., some steps need more time to be executed than others), it is necessary to be defined a set of rules. </p>



<h3 class="wp-block-heading">Rule 1: </h3>


<p>The time required to fetch an integer from memory is defined as <strong>t<sub>fetch</sub></strong>. The time needed to store an integer to the memory is defined as <strong>t<sub>store</sub></strong>.&nbsp;</p>
<p>For example, the running time of the statement “x = 1” is t<sub>fetch</sub>&nbsp;+ t<sub>store</sub>, because the integer is in the memory (typically any constant is stored in memory) and have to be fetched and also this integer has to be stored to the variable “x” which is in memory. The same running time has the statement “x = y” because we want to get the value of the variable “y” which is stored in memory. &nbsp;&nbsp;</p>


<h3 class="wp-block-heading">Rule 2:</h3>



<p>The time needed for each basic operation to be executed is defined as <strong>t<sub>+</sub>, t<sub>&#8211;</sub>, t<sub>x</sub>, t<sub>&lt;</sub>, t<sub>/</sub>.</strong> So, the running time of the statement “y = x + 1” is 2t<sub>fetch</sub>&nbsp;+ t<sub>+</sub>&nbsp;+ t<sub>store</sub>. The 2 fetches are for “x” and “1”. The result is stored in variable “y” (t<sub>store</sub>). The addition operation time is t<sub>+</sub>.&nbsp;&nbsp;</p>



<h3 class="wp-block-heading">Rule 3:</h3>



<p>The time needed&nbsp;to&nbsp;a function to be called is defined as&nbsp;<strong>t<sub>call</sub>&nbsp;</strong>while the time required to a function to return a value is defined as <strong>t<sub>return</sub></strong><sub>.</sub></p>



<h3 class="wp-block-heading">Rule 4:</h3>



<p>The time needed to pass a value as parameter to a function is the same as the time needed to store a value in memory. For example, the running time of “y = f(x)” is 2t<sub>store</sub>&nbsp;+ t<sub>fetch</sub>&nbsp;+ t<sub>call</sub>&nbsp;+ T<sub>f(x)</sub>. Note here that the t<sub>return</sub>&nbsp;is part of the function and not of the above statement. Furthermore, the T<sub>f(x)</sub>&nbsp;is the total running time of the function “f”.&nbsp;&nbsp;&nbsp;</p>



<h3 class="wp-block-heading">Rule 5: </h3>



<p>The time required for the address calculation implied by an array subscripting operation is defined as <strong>t<sub>[]&nbsp;</sub></strong>. For example, the running time of the statement “y = a[i]” is 3t<sub>fetch&nbsp;</sub>+ t<sub>store&nbsp;</sub>+t<sub>[]</sub>. The fetches are (1) the value of i, (2) the value of a and (3) the value of a[i].&nbsp;&nbsp;</p>



<p>Using these rules, we can easily estimate the running time of the algorithms. </p>



<h2 class="wp-block-heading">Examples</h2>



<p>Here, we present some programs in C++ and we estimate their running time. </p>




<div class="codecolorer-container cpp dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #0000ff;">int</span> max<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> a, <span style="color: #0000ff;">int</span> b<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><br />
<span style="color: #0000dd;">2</span><span style="color: #008080;">:</span> &nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">&gt;</span> b <span style="color: #008000;">&#41;</span> <br />
<span style="color: #0000dd;">3</span><span style="color: #008080;">:</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">return</span> a<span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">4</span><span style="color: #008080;">:</span> &nbsp; &nbsp; <span style="color: #0000ff;">return</span> b<span style="color: #008080;">;</span> <br />
<span style="color: #0000dd;">5</span><span style="color: #008080;">:</span> <span style="color: #008000;">&#125;</span></div></div>




<p>The running time of the function &#8220;max&#8221; depends on the lines 2,3 and 4. So, we have to estimate for each line (or step) the corresponding time and then to calculate the total time of the function.</p>



<figure class="wp-block-table"><table class="has-white-color has-black-background-color has-text-color has-background"><tbody><tr><td><strong>Lines</strong></td><td><strong>a &gt; b</strong></td><td><strong>a &lt;= b</strong></td><td><strong>Code</strong></td></tr><tr><td><strong>3</strong></td><td>2t<sub>fetch&nbsp;</sub>+ t<sub>&lt;</sub></td><td>2t<sub>fetch&nbsp;</sub>+ t<sub>&lt;</sub></td><td>If (a &gt; b)</td></tr><tr><td><strong>4</strong></td><td>t<sub>return</sub>&nbsp;+ t<sub>fetch</sub></td><td>&#8211;</td><td>return a;</td></tr><tr><td><strong>5</strong></td><td>&#8211;</td><td>t<sub>return</sub>&nbsp;+ t<sub>fetch</sub></td><td>return b;</td></tr><tr><td><strong>Total Running Time</strong></td><td>3t<sub>fetch</sub>&nbsp;+ t<sub>&lt;</sub>&nbsp;+ t<sub>return</sub></td><td>3t<sub>fetch</sub>&nbsp;+ t<sub>&lt;</sub>&nbsp;+ t<sub>return</sub></td><td>&nbsp;</td></tr></tbody></table></figure>



<p>Making our life better, we define the total time of max function when &#8220;a &gt; b&#8221; as t<sub>1</sub> and the total time of max function when &#8220;a &lt;= b&#8221; as t<sub>2</sub>. We can say that T<sub>max(a,b)</sub>&nbsp;= t<sub>1&nbsp;</sub>&nbsp;ή&nbsp;T<sub>max(a,b)</sub>&nbsp;= t<sub>2</sub>. However, as we can see &nbsp;t<sub>1</sub> = t<sub>2</sub>, so the T<sub>max(a,b)</sub> = 3t<sub>fetch</sub>&nbsp;+ t<sub>&lt;</sub>&nbsp;+ t<sub>return</sub>. if  t<sub>1</sub> &lt;&gt;  t<sub>2</sub>, then the total worst running time of the function would be that t with the worst time.   </p>




<div class="codecolorer-container cpp dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #0000dd;">1</span><span style="color: #008080;">:</span> <span style="color: #0000ff;">int</span> findMaxNumber<span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
<span style="color: #0000dd;">2</span><span style="color: #008080;">:</span> &nbsp; &nbsp;<span style="color: #0000ff;">int</span> maxNumber <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">3</span><span style="color: #008080;">:</span> &nbsp; &nbsp;<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><br />
<span style="color: #0000dd;">4</span><span style="color: #008080;">:</span> &nbsp; &nbsp; &nbsp; maxNumber <span style="color: #000080;">=</span> max<span style="color: #008000;">&#40;</span>maxNumber,A<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">5</span><span style="color: #008080;">:</span> &nbsp; &nbsp;<span style="color: #008000;">&#125;</span><br />
<span style="color: #0000dd;">6</span><span style="color: #008080;">:</span> &nbsp; &nbsp;<span style="color: #0000ff;">return</span> maxNumber<span style="color: #008080;">;</span><br />
<span style="color: #0000dd;">7</span><span style="color: #008080;">:</span> <span style="color: #008000;">&#125;</span></div></div>




<p>The function &#8220;findMaxNumber&#8221; gets as input an array A with n values and returns the maximum number of the array. We also use the previous function &#8220;max&#8221; to find the maximum number of two variables.  The running time of the function depends on the lines 2 to 6. <br></p>



<figure class="wp-block-table"><table><tbody><tr><td><strong>Lines</strong></td><td><strong>Running Time</strong></td><td><strong>Code</strong></td></tr><tr><td><strong>2</strong></td><td>t<sub>fetch&nbsp;</sub>+ t<sub>store</sub></td><td>int maxNumber = -1;</td></tr><tr><td><strong>3a</strong></td><td>t<sub>fetch&nbsp;</sub>+ t<sub>store</sub></td><td>int i =0;</td></tr><tr><td><strong>3b</strong></td><td>(2t<sub>fetch</sub>&nbsp;+ t<sub>&lt;</sub>)*(n+1)</td><td>i &lt; n;</td></tr><tr><td><strong>3c</strong></td><td>(2t<sub>fetch&nbsp;</sub>+ t<sub>store</sub>&nbsp;+ t<sub>+</sub>)*n</td><td>i++;</td></tr><tr><td><strong>4</strong></td><td>(3t<sub>store</sub>&nbsp;+ t<sub>call</sub>&nbsp;+ T<sub>max(a,b)</sub>&nbsp;+ 4t<sub>fetch</sub> + t<sub>[]</sub>)*n<sub>&nbsp;&nbsp;&nbsp;</sub></td><td>maxNumber = max(maxNumber,A[i]);</td></tr><tr><td><strong>6</strong></td><td>t<sub>return</sub>&nbsp;+ t<sub>fetch</sub></td><td>return maxNumber;</td></tr></tbody></table></figure>



<p>As we can see in the table, we used the 5 rules defined above to find the running time of the algorithm (<strong>Exercise: </strong>Find the total running time of the algorithm using also the running time of the function &#8220;max&#8221;). There are some interesting parts in this source code: </p>



<ul>
<li>The line 3 is split into three parts. The initialization of the i variable is executed only one time while the statements 3b and 3c are executed at least n times. The 3b statement is executed n + 1 times because we also  take into account when the condition is getting false for the first time.</li>



<li>To find the running time of the 4th statement we needed all 5 rules. Firstly, we have to fetch the A[i] (as we explained above) and the maxNumber. These two parameters need 2t<sub>store</sub> when they are passed to the function. The return value of the function is stored in the maxNumber so we need also a t<sub>store</sub>.   </li>
</ul>
<p>The post <a rel="nofollow" href="https://dndimitri.eu/2020/08/23/estimating-running-time-of-an-algorithm-using-simple-rules/">Estimating Running Time of an Algorithm using Simple Rules.</a> appeared first on <a rel="nofollow" href="https://dndimitri.eu">Dimitris Dimitriadis</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://dndimitri.eu/2020/08/23/estimating-running-time-of-an-algorithm-using-simple-rules/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">586</post-id>	</item>
	</channel>
</rss>
