0% found this document useful (0 votes)
2 views

Week 13

The document discusses algorithms for finding the closest pair of points in both 1D and 2D, detailing a divide and conquer approach for the 2D case that improves upon a brute-force method. It also outlines a practice exam question involving edge swapping in trees and provides a proof of correctness for the algorithm used. Additionally, it describes a DynamicMatrix class for managing a matrix with dynamic size, including its time complexity for various operations.

Uploaded by

Abhinav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Week 13

The document discusses algorithms for finding the closest pair of points in both 1D and 2D, detailing a divide and conquer approach for the 2D case that improves upon a brute-force method. It also outlines a practice exam question involving edge swapping in trees and provides a proof of correctness for the algorithm used. Additionally, it describes a DynamicMatrix class for managing a matrix with dynamic size, including its time complexity for various operations.

Uploaded by

Abhinav Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Week 13 – Revision

Closest pair of points 1D version

- Given a list of points,


- eg. X = [2,8,4,21,1]
Sort the list of points in O(nlogn) using mergesort or
heapsort
Then iterate through the list, check distance of
adjacent points, find the two with the closest distance.

Closest pair of points 2D version


- O(n^2) brute-force solution
- Iterate through each combination of pairs, and
check the distance – find pair with minimum
distance.
- There are n(n-1)/2 pairs in total
Use divide and conquer to make a better algorithm
Base case: for a small number of values (eg, <10) use
the brute-force approach.

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)

The algorithm can be further improved to eliminate


sorting on each recursive call by keeping two lists: one
sorted by y-coordinate and another sorted by x-
coordinate (maintaining reference to the same points).
Merge-sort of y-coordinate lists occurs simultaneously

Practice exam Q4.

1. All the edges that are the same (both in S and T)


are fixed and will not change in the swapping
sequence
2. Duplicate edges in S into a new list M. Remove all
the duplicate edges that existed in both T and S
from M. (it now contains the edges in S which are
not in T),
3. Iterate through M and add one edge to T – this
forms a cycle. Need to find the other edge that can
be removed to break this cycle. (it can’t be any of
the edges fixed in place in step 1). Save this as the
tree Ri
4. Repeat this process until no more edges exist in M.

Proof of correctness – step 3 will always be able to


find an edge to break the cycle that isn’t fixed in
place, because if not then S wasn’t a tree because
it had a cycle (it had all edges that are fixed + the
one we added to form the cycle).
- There must be an edge that is in T but was not in S,
and this can be removed. There could be multiple
such edges, any one can be removed, because the
others will be removed on subsequent iterations. –
not true because this would mean on subsequent
iterations another edge filled the gap created by
the removed edge (edge was in T but not in S) but
then on a later iteration it is added back (edge was
in S but not in T) – contradiction.
- Therefore there will be only one such edge.
- Gap must exist because it’s a tree (no cycles)
- Multiple gaps won’t exist because it spans all the
nodes (spanning trees T and S)

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

Upper and lower lists are arrays containing references


to arrays.
Diagonal list is also an array.

class DynamicMatrix:
def __init__(self):
self.diagonal = [0]
self.upper = []
self.lower = []

def get(self, i, j):


if i == j:
return self.diagonal[i]
if i > j:
return self.upper[i][j]
else:
return self.lower[j][i]
def increase_size():
n = len(self.diagonal)
self.diagonal.append(0)
self.upper.append([0]*n)
self.lower.append([0]*n)
Time complexity: append does resize
operation in O(n), so increase_size
takes 3*O(n) + 2*O(n) for
initialisation of two new arrays
= O(n)

You might also like