- #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
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;
}
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.22990atinesh said:How you are giving two solutions of the same recurrence relation
##T(1) = 2##
2
2 2
1 1 1 1
x x x x x x x x
2
2 0
1 1 1
x x x x x x
2
2 0
1 1 0
x x x x x
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.
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.
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.
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.
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.