Is there a mistake in my solution for this recurrence?

  • Thread starter 22990atinesh
  • Start date
  • Tags
    Recurrence
In summary: Thanx for the help.In summary, the conversation discussed the recurrence relation T(n) = 2T(n/2) + 2 and its solution using different methods. The conversation also included an example code for finding the minimum and maximum of an array using recursion. The correct solution for the recurrence relation was determined to be ⌈ 3n/2 ⌉ - 2.
  • #1
22990atinesh
143
1

Homework Statement


##T(n) = 2 T(\frac{n}{2}) + 2##
##T(1) = 2##

Homework Equations

The Attempt at a Solution


I'm coming up with ##T(n) = n-1## on solving it by recursion tree method. But that's not the ans
 
Physics news on Phys.org
  • #2
I'm not sure of what methods are expected to solve these type of equations. In this case though, you can calculate T for n_(i+1) = 2n_(i): T(1) = 2, T(2) = 6, T(4) = 14, T(8) = 30, ... , then you can look for a linear equation that produces the same results.
 
  • #3
Here is the original question and they way I'm solving it. Is there any other way for solving this question

image.jpg


image.jpg


image.jpg
 
  • #4
Find min and max of array of size n, by recursively splitting the array until subarray size == 2 and doing a single compare and returning the min and max. If subarray size == 1, return min = max = array[0], no compare. For each pair of returned (min, max) from two sub-arrays, do one compare for the two min's and one compare for the two max's.

The recurrence formula T(1) = 2, T(n) = 2T(n/2) + 2 = 4n - 2 as previous posted, but this doesn't represent the number of compares if you recursively split the array, since T(2) = 1 (only one compare needed to determine min and max with just two numbers). So by recursively splitting the array:

T(2) = 1, T(n) = 2T(n/2) + 2 = ⌈ 3n/2 ⌉ - 2.

Example code:
Code:
typedef struct{
unsigned int min;
unsigned int max;
}MINMAX;

static size_t cmp;              // number of compares (optional global)

MINMAX findminmax(int a[], size_t sizea)
{
MINMAX minmax;
MINMAX minmaxl;
MINMAX minmaxr;
size_t sizea2;

    if(sizea == 0){             // if empty array (can only happen on initial call)
        minmax.min = 0;         //   set min = max = 0
        minmax.max = 0;
        return minmax;
    }
    if(sizea == 1){             // if size == 1
        minmax.min = a[0];      //   set min = max = a[0]
        minmax.max = a[0];
        return minmax;
    }
    if(sizea == 2){             // if size == 2
        if(a[0] <= a[1]){       //   set min and max
            minmax.min = a[0];
            minmax.max = a[1];
        } else {
            minmax.min = a[1];
            minmax.max = a[0];
        }
        cmp += 1;
        return minmax;
    }

    sizea2 = sizea/2;           // recursively split array, set min and max
    minmaxl = findminmax(&a[0], sizea2);
    minmaxr = findminmax(&a[sizea2], sizea - sizea2);
    minmax.min = minmaxl.min <= minmaxr.min ? minmaxl.min : minmaxr.min;
    minmax.max = minmaxl.max >= minmaxr.max ? minmaxl.max : minmaxr.max;
    cmp += 2;
    return minmax;
}
 
Last edited:
  • #5
@rcgldr
How you are giving two solutions of the same recurrence relation
##T(n) = 2T(\frac{n}{2}) + 2##
##T(1) = 2##
as ##4n-2## and ##\lfloor \frac{3n}{2} \rfloor - 2##
and what method are you using. If I use Master theorem, I'm coming up with the ans n and with recursion tree method n-1
 
  • #6
22990atinesh said:
How you are giving two solutions of the same recurrence relation
##T(1) = 2##
The recurrence relation is the same, but instead of T(1) = 2, it should be T(2) = 1, and this changes the result from 4n-2 to ⌈ 3n/2 ⌉ - 2. (Note its ceiling (round up), not floor). T(2) = 1, because if a subarray only has 2 elements, then it only takes 1 compare to determine which is the minimum and which is the maximum. This result matches the compare count in the example code posted above.
 
Last edited:
  • #7
@rcgldr
hmm Sorry ##T(2) = 1## is correct. But still as you can see from the Pic that I've provided, that I'm getting ##n - 1## with recursion tree method. Could you please point out the msitake that I'm using.
 
  • #8
I''m not sure how to do a recursion tree except for specific cases. Powers of 2 are easiest, for example 8. The bottom row has no numbers (you need to compare pairs). The next to bottom row will have 1's, and all the rows above it will have 2's.

n = 8
Code:
            2
      2           2
   1     1     1     1   
  x x   x x   x x   x x

n = 6
Code:
            2
      2           0
   1     1     1   
  x x   x x   x x

n = 5
Code:
            2
      2           0
   1     1     0   
  x x   x x   x

Also answer b should have been written as "at most ⌈ 1.5 n ⌉ - 2 compares" are needed. It left out the part about rounding up if n is odd.
 
Last edited:
  • #9
@rcgldr
Thanx got it. I was doing one algebraic mistake, that's why I was always ending up with ##n - 1##
##\frac{3}{2} n - 2## is correct
 

Related to Is there a mistake in my solution for this recurrence?

1. How do I determine the base case for a recurrence?

The base case for a recurrence is the starting point of the problem, where the solution is known and doesn't require any further recursion. It is typically the input value that results in a simple and straightforward solution. To determine the base case, you can examine the pattern of the recurrence and identify the simplest case where the solution is known.

2. What is the difference between a linear and a non-linear recurrence?

A linear recurrence is one where the recurrence relation is expressed in terms of a constant number of previous terms. This means that the number of previous terms used to calculate the current term remains constant, regardless of the input value. On the other hand, a non-linear recurrence involves a variable number of previous terms, making it more complex to solve.

3. How can I use a recurrence to solve a problem?

Recurrences are useful for solving problems that involve repeated subproblems. By breaking down a problem into smaller subproblems and using the recurrence relation to relate them, you can solve the original problem more efficiently. This is known as the divide and conquer approach, where the problem is divided into smaller parts, and the solutions to these subproblems are combined to solve the original problem.

4. Is there a general formula for solving any recurrence?

No, there is no general formula for solving any recurrence. Each recurrence problem is unique and requires a specific approach for solving it. Some common techniques for solving recurrences include substitution, iteration, and master theorem. It is important to understand the characteristics of the recurrence and choose the appropriate method for solving it.

5. Can I use a recurrence to solve a problem with multiple variables?

Yes, you can use a recurrence to solve a problem with multiple variables. However, the recurrence relation will involve multiple terms and may be more complex to solve. It is important to define the base case and determine the pattern of the recurrence before attempting to solve it. Additionally, you may need to use techniques such as induction or recursion trees to solve a recurrence with multiple variables.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top