Week 13
Week 13
x = [2,3,5,1,4,6]
y = [2,7,8,3,1,4]
sort by x coordinate
x = [1,2,3,4,5,6]
y = [3,2,7,1,8,4]
divide in half so that each half contains roughly n/2
points
Eg, L = 3.5.
Recursively solve each half - result is returned which is
the closest pair in each half.
Merge step: the overall solution could be one of either
the two solutions in each half, or between two points
which cross over the dividing line.
Delta = min distance of two recursive solutions. We
only need to check points that cross the dividing line if
the are a distance of delta from the dividing line.
Sort the points in the 2delta region by y coordinate.
O(nlogn)
Check 10 neighbouring points of each point in the
other half, if distance is smaller, update current min
distance. This takes O(n*10) = O(n)
- This works because each point can only exist in its
own half delta square, and any points further than
delta don’t need to be checked.
Time complexity:
T(n) = 2T(n/2) + O(nlogn)
Solved using the master theorem:
a=2, b=2, f(n) = nlogn
nlog2(2) = n
case 2 (equal by polynomial amount) where k=1
Add 1 to the log factor, that’s the result:
T(n) = O(nlog2n)
Time complexity:
Step 1: O(n) – tree has n-1 edges
Step 2: O(n2)
Step 3 & 4: O(n2)
Total = O(n2)
Q3.
i is the row index, j is the column index
class DynamicMatrix:
def __init__(self):
self.diagonal = [0]
self.upper = []
self.lower = []