Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your first recurrence relation is normally used to describe running time of <strong>divide-and-conquer</strong> algorithms. <code>a</code> here shows how many parts you are dividing your data to, <code>1/b</code> shows what piece of original data is used in each part, and <code>f(n)</code> shows how much time you need on each "level". For example, in QuickSort algorithm you divide your data (array or list) into 2 parts, each of which is exactly half (1/2) of original data, and on each "level" of dividing you need to go through all <code>n</code> elements 1 time. So recurrence relation is </p> <pre><code>T(n) = 2T(n/2) + n </code></pre> <p>(which evaluates to O(n * lg(n))) And in binary search you divide data into 2 parts, but take only 1 of them, and time on each "level" is constant, so relation is:</p> <pre><code>T(n) = T(n/2) + 1 </code></pre> <p>However, your function in C doesn't follow divide-and-conquer strategy. Instead it demonstrates <strong>mathematical induction</strong>. You define start conditions: if <code>l</code> equals <code>NULL</code>, then length is 0, and if it <code>l-&gt;next</code> equals <code>NULL</code> (you also define condition for 1 element in list). Then you define a kind of inductive step - you define how to compute function for nth element, if you know function value for (n - 1)th element. So, knowing value of a function for 1st element you can apply this rule and get value for 2nd element, then for 3rd and so on. </p> <p>So you can see that induction method is recurrence algorithm by the nature. Here we have 2 times to consider. First is a time to compute value at the start point (or end point - it depends on the order you are looking at the list). In your function you just return this value, so it is constant (<code>C1</code>). Second is a time to make 1 step. In your function it is also constant (<code>C2</code>). Intuitively you should see that execution time of this algorithm will be: </p> <pre><code>T(n) = C1, when n = 1 T(n) = T(n - 1) + C2, when n &gt; 1 </code></pre> <p>Thus, unless <code>n = 1</code>, you define time to execute algorithm on <code>n</code> element through time to execute it on <code>n - 1</code> elements. In BigO notation any constant is defined as <code>1</code> and case of 1 element is not considered, so your final recurrence relation is: </p> <pre><code>T(n) = T(n - 1) + 1 </code></pre> <p>(which evaluates to <code>T(n) = T(n - 1) + 1 = T(n - 2) + 2 = ... = 1 + n</code>, or <code>O(n)</code>)</p> <p><strong>Further reading</strong>: </p> <ul> <li><a href="http://rads.stackoverflow.com/amzn/click/0262033844" rel="noreferrer">Introduction to Algorithms</a> by Thomas H. Cormen. </li> <li><a href="http://rads.stackoverflow.com/amzn/click/0201485419" rel="noreferrer">Art of Computer Programming</a> by Donald Knuth.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload