Professional Documents
Culture Documents
Divide and Conquer (Contd.) : Abhiram Ranade
Divide and Conquer (Contd.) : Abhiram Ranade
)
Abhiram Ranade
20 January 2015
There are n leaves, and hence total work at the leaves is also
c 0 n.
There are n leaves, and hence total work at the leaves is also
c 0 n.
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
General remarks
If the time taken for something is cn, usually the precise value
of c does not matter.
General remarks
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
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!
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.
4. Report c = mini i
4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
4. Report c = mini i
Thm: If distance(pi , pj ) < then |i j| 11.
Proof: Divide the strip into squares of width /2.
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.
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.
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.
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.
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.
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.
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.
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)