Is there a mistake in my solution for this recurrence?

  • Thread starter Thread starter 22990atinesh
  • Start date Start date
  • Tags Tags
    Recurrence
Click For Summary

Discussion Overview

The discussion revolves around solving a recurrence relation of the form T(n) = 2T(n/2) + 2, specifically in the context of determining the number of comparisons needed to find the minimum and maximum values in an array using a recursive approach. Participants explore various methods for solving the recurrence, including recursion trees and the Master theorem, while also discussing the implications of different base cases.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant proposes that the solution to the recurrence is T(n) = n - 1 using the recursion tree method, but expresses uncertainty about the correctness of this result.
  • Another participant suggests calculating specific values of T(n) for powers of 2 to identify a linear equation that fits the results, providing a sequence of computed values.
  • A different approach is introduced, where the recurrence is analyzed in the context of finding the minimum and maximum of an array, leading to a proposed formula of T(n) = ⌈3n/2⌉ - 2.
  • There is a discussion about the implications of different base cases, particularly T(1) = 2 versus T(2) = 1, and how this affects the derived solutions.
  • One participant acknowledges an algebraic mistake that led to consistently arriving at T(n) = n - 1, indicating a correction to T(n) = (3/2)n - 2 as the accurate result.

Areas of Agreement / Disagreement

Participants express differing views on the correct solution to the recurrence relation, with some arriving at T(n) = n - 1 and others suggesting T(n) = ⌈3n/2⌉ - 2. The discussion remains unresolved as participants explore these competing solutions without reaching consensus.

Contextual Notes

There are limitations regarding the assumptions made about the base cases and the methods used for solving the recurrence. The dependence on specific definitions and the implications of rounding in the proposed solutions are also noted.

22990atinesh
Messages
143
Reaction score
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
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.
 
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
 
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:
@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
 
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:
@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.
 
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:
@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
 

Similar threads

Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K