0. Append the vectors to a list
1. Sort the list by the x-axis (presumably with mergesort) ) O(nlogn) only once
1. Divide the list into two from the midpoint according to the x-axis values
2. Recursively divide the list into halves logn times
3. In each division into half, check the relationship between 3 closest pairs in each side
The distance between two points is checked with the use Pythagorean theorem.
The runtime of this algorithm turns out to be O(nlogn) if we use the Master Theorem that was introduced in the previous blog entry.
The obvious bruteforce method takes O(n^2).
Now I will compare the runtime of my algorithm with the bruteforce approach below:
10000 pairs
Bruteforce: 0:00:08.874909
Divide and Conquer: 0:00:00.581441
50000 pairs
Bruteforce: 0:00:09.365906
Divide and Conquer: 0:00:04.014672
You might notice the leap in the runtime of the divide and conquer algorithm. That is caused mostly by the runtime of the mergesort that sorts our list before our actual pair finding algorithm starts working.
The runtime of this algorithm turns out to be O(nlogn) if we use the Master Theorem that was introduced in the previous blog entry.
The obvious bruteforce method takes O(n^2).
Now I will compare the runtime of my algorithm with the bruteforce approach below:
10000 pairs
Bruteforce: 0:00:08.874909
Divide and Conquer: 0:00:00.581441
50000 pairs
Bruteforce: 0:00:09.365906
Divide and Conquer: 0:00:04.014672
You might notice the leap in the runtime of the divide and conquer algorithm. That is caused mostly by the runtime of the mergesort that sorts our list before our actual pair finding algorithm starts working.
No comments:
Post a Comment