0
0
DSA Typescriptprogramming~10 mins

Divide and Conquer Strategy and Recurrence Relations in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Divide and Conquer Strategy and Recurrence Relations
Divide Problem into Subproblems
Recursively Solve Each Subproblem
Combine Subproblem Solutions
Final Solution
Form Recurrence Relation
Analyze Recurrence to Find Complexity
The problem is split into smaller parts, each solved recursively, then combined. This process forms a recurrence relation used to analyze time complexity.
Execution Sample
DSA Typescript
function mergeSort(arr: number[]): number[] {
  if (arr.length <= 1) return arr;
  const mid = Math.floor(arr.length / 2);
  const left = mergeSort(arr.slice(0, mid));
  const right = mergeSort(arr.slice(mid));
  return merge(left, right);
}
This code divides an array into halves recursively and merges sorted halves, illustrating divide and conquer.
Execution Table
StepOperationSubproblem SizeRecursion DepthActionVisual State
1Divide array [8,3,5,4]40Split into [8,3] and [5,4][8,3,5,4]
2Divide subarray [8,3]21Split into [8] and [3][8,3]
3Base case reached for [8]12Return [8][8]
4Base case reached for [3]12Return [3][3]
5Merge [8] and [3]21Merge to [3,8][3,8]
6Divide subarray [5,4]21Split into [5] and [4][5,4]
7Base case reached for [5]12Return [5][5]
8Base case reached for [4]12Return [4][4]
9Merge [5] and [4]21Merge to [4,5][4,5]
10Merge [3,8] and [4,5]40Merge to [3,4,5,8][3,4,5,8]
11Recurrence formed: T(n) = 2T(n/2) + O(n)n-Analyze to find O(n log n)[Sorted array]
💡 All subproblems reduced to size 1 (base case), merged back to final sorted array.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5After Step 10Final
arr[8,3,5,4][8,3] and [5,4][8] and [3][3,8][3,4,5,8][3,4,5,8]
subproblem size421244
recursion depth012100
Key Moments - 3 Insights
Why do we stop dividing when subproblem size is 1?
Because a single element is already sorted, so the base case returns immediately as shown in steps 3 and 4.
How does the recurrence T(n) = 2T(n/2) + O(n) represent the algorithm?
It shows the problem splits into two halves (2T(n/2)) and merging takes linear time (O(n)), as summarized in step 11.
Why does recursion depth increase when dividing and decrease when merging?
Each division adds a level of recursion (steps 1,2), and merging returns back up reducing depth (steps 5,10), reflecting the call stack.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the subproblem size at step 6?
A4
B1
C2
D0
💡 Hint
Check the 'Subproblem Size' column at step 6 in the execution_table.
At which step does the recursion depth return to 0 after dividing?
AStep 5
BStep 10
CStep 3
DStep 1
💡 Hint
Look at the 'Recursion Depth' column in the execution_table and find when it goes back to 0.
If the array size doubles, how does the recurrence relation change?
AT(2n) = 2T(n) + O(2n)
BT(2n) = 2T(n) + O(n)
CT(2n) = 4T(n/2) + O(n)
DT(2n) = T(n) + O(n)
💡 Hint
Recall the recurrence T(n) = 2T(n/2) + O(n) and consider doubling n.
Concept Snapshot
Divide and Conquer splits a problem into smaller parts,
solves each recursively, then combines results.
Recurrence relations express time as T(n) = aT(n/b) + f(n).
Analyzing recurrence finds algorithm complexity.
Example: Merge Sort uses T(n) = 2T(n/2) + O(n) → O(n log n).
Full Transcript
Divide and Conquer strategy breaks a problem into smaller subproblems, solves each recursively, and combines their solutions. This process forms a recurrence relation that expresses the time complexity. For example, merge sort divides an array into halves until single elements remain, then merges sorted halves. The recurrence relation T(n) = 2T(n/2) + O(n) describes this, where 2T(n/2) is the time for recursive calls and O(n) is the merge time. Analyzing this recurrence shows merge sort runs in O(n log n) time.