Download as pdf or txt
Download as pdf or txt
You are on page 1of 102

Divide and conquer (contd.

)
Abhiram Ranade

20 January 2015

Recursion trees: a graphical method for solving recurrences

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.
Example: Mergesort.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.
Example: Mergesort.
I

Number of levels = log n, where n = size of entire sequence.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.
Example: Mergesort.
I

Number of levels = log n, where n = size of entire sequence.

Work at internal nodes cm where m = size of the


subsequence generated.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.
Example: Mergesort.
I

Number of levels = log n, where n = size of entire sequence.

Work at internal nodes cm where m = size of the


subsequence generated.

Total work at any level cn

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.
Example: Mergesort.
I

Number of levels = log n, where n = size of entire sequence.

Work at internal nodes cm where m = size of the


subsequence generated.

Total work at any level cn

There are n leaves, and hence total work at the leaves is also
c 0 n.

Recursion trees: a graphical method for solving recurrences


1. Draw the tree representing the recursive calls made by the
algorithm. Root = original call.
2. Figure out number of levels.
3. Estimate the work at each level of the tree.
4. Add up the work.
Example: Mergesort.
I

Number of levels = log n, where n = size of entire sequence.

Work at internal nodes cm where m = size of the


subsequence generated.

Total work at any level cn

There are n leaves, and hence total work at the leaves is also
c 0 n.

Thus total time cn log n + cn = O(n log n).

Unequal size subproblems

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.
The number of levels? Some branches terminate early, others go
deep.

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.
The number of levels? Some branches terminate early, others go
deep.
The rightmost leaf would be deepest, with number of levels
= log3/2 n.

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.
The number of levels? Some branches terminate early, others go
deep.
The rightmost leaf would be deepest, with number of levels
= log3/2 n.
Max depth = log3/2 n.

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.
The number of levels? Some branches terminate early, others go
deep.
The rightmost leaf would be deepest, with number of levels
= log3/2 n.
Max depth = log3/2 n.
Time cn log3/2 n

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.
The number of levels? Some branches terminate early, others go
deep.
The rightmost leaf would be deepest, with number of levels
= log3/2 n.
Max depth = log3/2 n.
Time cn log3/2 n
Graphical analysis is easier than algebraic?

Unequal size subproblems


Suppose just for fun we recurse on sequences of size n/3 and 2n/3.
What changes in the analysis?
The total work at each level is still cn, because total number of
keys at each level is cn.
The number of levels? Some branches terminate early, others go
deep.
The rightmost leaf would be deepest, with number of levels
= log3/2 n.
Max depth = log3/2 n.
Time cn log3/2 n
Graphical analysis is easier than algebraic?
Equal sized subproblems better.

Floors and ceilings

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e 1/2
Equality for n = 3.

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e 1/2
Equality for n = 3.
Thus depth of tree log3/2 n.

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e 1/2
Equality for n = 3.
Thus depth of tree log3/2 n.
Work at each level cn.

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e 1/2
Equality for n = 3.
Thus depth of tree log3/2 n.
Work at each level cn.
Thus total time = O(n log n)

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e 1/2
Equality for n = 3.
Thus depth of tree log3/2 n.
Work at each level cn.
Thus total time = O(n log n)

Note that loga n = log2 n/ log2 a = (log2 n)

Floors and ceilings


If number of keys being sorted is not a power of 2, we will
encounter floors and ceilings in the analysis.
The subproblems will not be exactly equal.
Ratio of problem sizes = bn/2c / dn/2e 1/2
Equality for n = 3.
Thus depth of tree log3/2 n.
Work at each level cn.
Thus total time = O(n log n)

Note that loga n = log2 n/ log2 a = (log2 n)


Base of the logarithm does not matter if we are ignoring constant
factors and the base is a constant.

Master Theorem
If T (n) = aT (dn/be) + O(nd ) for some constants a > 0, b > 1,
and d 0, then

if d > logb a
O(nd )
O(nd log n) if d = logb a
T (n) =

O(nlogb a )
if d < logb a

Master Theorem
If T (n) = aT (dn/be) + O(nd ) for some constants a > 0, b > 1,
and d 0, then

if d > logb a
O(nd )
O(nd log n) if d = logb a
T (n) =

O(nlogb a )
if d < logb a
Proof: Omitted.

Master Theorem
If T (n) = aT (dn/be) + O(nd ) for some constants a > 0, b > 1,
and d 0, then

if d > logb a
O(nd )
O(nd log n) if d = logb a
T (n) =

O(nlogb a )
if d < logb a
Proof: Omitted.
Covers many cases that may arise in practice. Gives insight.

Master Theorem
If T (n) = aT (dn/be) + O(nd ) for some constants a > 0, b > 1,
and d 0, then

if d > logb a
O(nd )
O(nd log n) if d = logb a
T (n) =

O(nlogb a )
if d < logb a
Proof: Omitted.
Covers many cases that may arise in practice. Gives insight.
You may memorize this and use it, unless you are explicitly asked
not to.

Master Theorem
If T (n) = aT (dn/be) + O(nd ) for some constants a > 0, b > 1,
and d 0, then

if d > logb a
O(nd )
O(nd log n) if d = logb a
T (n) =

O(nlogb a )
if d < logb a
Proof: Omitted.
Covers many cases that may arise in practice. Gives insight.
You may memorize this and use it, unless you are explicitly asked
not to.
Exercise: Solve T (n) 7T (n/7) + cn.

General remarks

General remarks

Need to understand carefully which constants can be ignored


and which cannot.

General remarks

Need to understand carefully which constants can be ignored


and which cannot.
I

If the time taken for something is cn, usually the precise value
of c does not matter.

General remarks

Need to understand carefully which constants can be ignored


and which cannot.
I

If the time taken for something is cn, usually the precise value
of c does not matter.
If something creates c subproblems, the the value of c matters
very much!

General remarks

Need to understand carefully which constants can be ignored


and which cannot.
I

If the time taken for something is cn, usually the precise value
of c does not matter.
If something creates c subproblems, the the value of c matters
very much!

Dividing subproblems equally is often more efficient.

Closest pair in the plane

Closest pair in the plane


Input: n points in the plane.

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.
Useful in graphics, geographical information systems, air traffic
control..

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.
Useful in graphics, geographical information systems, air traffic
control..
Brute force: Check all pairs. Report closest. Time = (n2 ).

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.
Useful in graphics, geographical information systems, air traffic
control..
Brute force: Check all pairs. Report closest. Time = (n2 ).
One dimensional version: Points on a line.

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.
Useful in graphics, geographical information systems, air traffic
control..
Brute force: Check all pairs. Report closest. Time = (n2 ).
One dimensional version: Points on a line.
Easy O(n log n) time algorithm =

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.
Useful in graphics, geographical information systems, air traffic
control..
Brute force: Check all pairs. Report closest. Time = (n2 ).
One dimensional version: Points on a line.
Easy O(n log n) time algorithm = Sort, take minimum distance
between consecutive points.

Closest pair in the plane


Input: n points in the plane.
Output: Pair of points that are closest among those given.
Closest = smallest Euclidean distance.
Useful in graphics, geographical information systems, air traffic
control..
Brute force: Check all pairs. Report closest. Time = (n2 ).
One dimensional version: Points on a line.
Easy O(n log n) time algorithm = Sort, take minimum distance
between consecutive points.
Two dimensions: Nice divide and conquer algorithm.

How should we apply divide and conquer?

How should we apply divide and conquer?


Idea 1:

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:
Subproblem 1 = Leftmost n/2 points
Subproblem 2 = Rightmost n/2 points

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:
Subproblem 1 = Leftmost n/2 points
Subproblem 2 = Rightmost n/2 points
Assume all x coordinates are distinct.

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:
Subproblem 1 = Leftmost n/2 points
Subproblem 2 = Rightmost n/2 points
Assume all x coordinates are distinct.
How to decide: Equal sized problems?

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:
Subproblem 1 = Leftmost n/2 points
Subproblem 2 = Rightmost n/2 points
Assume all x coordinates are distinct.
How to decide: Equal sized problems? Overhead of splitting?

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:
Subproblem 1 = Leftmost n/2 points
Subproblem 2 = Rightmost n/2 points
Assume all x coordinates are distinct.
How to decide: Equal sized problems? Overhead of splitting?
Which will be easier to combine?

How should we apply divide and conquer?


Idea 1:
Subproblem 1 = first n/2 points in given order.
Subproblem 2 = last n/2.
Idea 2:
Subproblem 1 = Points falling in left half of region.
Subproblem 2 = Points falling in right half of region.
Width of left half = width of right half
Idea 3:
Subproblem 1 = Leftmost n/2 points
Subproblem 2 = Rightmost n/2 points
Assume all x coordinates are distinct.
How to decide: Equal sized problems? Overhead of splitting?
Which will be easier to combine? Try all?

The algorithm

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.
3. Combine: Find closest pair with one point on each side.

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.
3. Combine: Find closest pair with one point on each side.
4. Report best of the three solutions.

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.
3. Combine: Find closest pair with one point on each side.
4. Report best of the three solutions.
T (n) = Tdivide + 2T (n/2) + Tcombine

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.
3. Combine: Find closest pair with one point on each side.
4. Report best of the three solutions.
T (n) = Tdivide + 2T (n/2) + Tcombine
Divide: sorting. Tdivide = O(n log n).

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.
3. Combine: Find closest pair with one point on each side.
4. Report best of the three solutions.
T (n) = Tdivide + 2T (n/2) + Tcombine
Divide: sorting. Tdivide = O(n log n).
Notation: L , R = closest distance on left, right as calculated by
the conquer step.

The algorithm

1. Divide: Draw vertical line Sep with n/2 points on each side.
2. Conquer: Find closest pair recursively on each side.
3. Combine: Find closest pair with one point on each side.
4. Report best of the three solutions.
T (n) = Tdivide + 2T (n/2) + Tcombine
Divide: sorting. Tdivide = O(n log n).
Notation: L , R = closest distance on left, right as calculated by
the conquer step.
= min(L , R ) : closest distance found before combine.

The combine step

The combine step

We need to find the closest distance cross between pi , pj s.t. pi is


on left, and pj is on right.

The combine step

We need to find the closest distance cross between pi , pj s.t. pi is


on left, and pj is on right.
Or do we?

The combine step

We need to find the closest distance cross between pi , pj s.t. pi is


on left, and pj is on right.
Or do we?
If cross , we dont need to know its value.

The combine step

We need to find the closest distance cross between pi , pj s.t. pi is


on left, and pj is on right.
Or do we?
If cross , we dont need to know its value.
Observation: We only need to consider points which are within
distance of Sep.

The combine step

We need to find the closest distance cross between pi , pj s.t. pi is


on left, and pj is on right.
Or do we?
If cross , we dont need to know its value.
Observation: We only need to consider points which are within
distance of Sep.
Observation: This vertical strip problem is almost one dimensional..

The combine step (contd.)

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.
If distance (pi , pj ) < , then they can be separated by at most one
rows of squares.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.
If distance (pi , pj ) < , then they can be separated by at most one
rows of squares.
pi , pj together belong to 3 consecutive rows.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.
If distance (pi , pj ) < , then they can be separated by at most one
rows of squares.
pi , pj together belong to 3 consecutive rows.
There must be < 10 points between them.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.
If distance (pi , pj ) < , then they can be separated by at most one
rows of squares.
pi , pj together belong to 3 consecutive rows.
There must be < 10 points between them.

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.
If distance (pi , pj ) < , then they can be separated by at most one
rows of squares.
pi , pj together belong to 3 consecutive rows.
There must be < 10 points between them.

The number 11 can be reduced with better analysis..

The combine step (contd.)


1. Sort the points in the vertical strip by their y coordinate.
2. For i = 1 to number of points
3.

i = minimum distance between pi and pi+1 , . . . , pi+11 .

4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
Every such square can contain at most 1 point.
If distance (pi , pj ) < , then they can be separated by at most one
rows of squares.
pi , pj together belong to 3 consecutive rows.
There must be < 10 points between them.

The number 11 can be reduced with better analysis..


Tcombine = O(n log n)

Summary

Summary
T (n) = O(n log n) + 2T (n/2)

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)
Improvements Possible: Can sort points once for all into array X
by x coordinate and array Y by y coordinate.

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)
Improvements Possible: Can sort points once for all into array X
by x coordinate and array Y by y coordinate.
Pass these arrays to recursive calls and eliminate sorting.

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)
Improvements Possible: Can sort points once for all into array X
by x coordinate and array Y by y coordinate.
Pass these arrays to recursive calls and eliminate sorting.
T (n) = Tpresort + Trec (n)

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)
Improvements Possible: Can sort points once for all into array X
by x coordinate and array Y by y coordinate.
Pass these arrays to recursive calls and eliminate sorting.
T (n) = Tpresort + Trec (n)
Trec (n) = O(n) + 2Trec (n/2)

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)
Improvements Possible: Can sort points once for all into array X
by x coordinate and array Y by y coordinate.
Pass these arrays to recursive calls and eliminate sorting.
T (n) = Tpresort + Trec (n)
Trec (n) = O(n) + 2Trec (n/2)
Trec (n) = O(n log n)

Summary
T (n) = O(n log n) + 2T (n/2)
Solves to: T (n) = O(n log2 n)
Improvements Possible: Can sort points once for all into array X
by x coordinate and array Y by y coordinate.
Pass these arrays to recursive calls and eliminate sorting.
T (n) = Tpresort + Trec (n)
Trec (n) = O(n) + 2Trec (n/2)
Trec (n) = O(n log n)
T (n) = O(n log n)

You might also like