Consider
this large set of 1D points on the number line
. Let
S
be the set of
unordered
pairs
(
p
,
q
)
such that
∣
p
−
q
∣
≤
1
0
0
0
. How many elements does
S
contain?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
This algorithm should be O ( n 2 ) right? Because the worst case is all pairs satisfy the property, k = n 2 . I have one slightly better approach is to use Binary Search. After you sort the list O ( n l g n ) , for each element you binary search when does it reach > 1 0 0 0 . This is O ( n l g n ) .
Log in to reply
Yes that is accurate. Despite the problem statement, I should have clarified I was demonstrating a generating algorithm in my solution instead of a counting algorithm. Traditionally, such algorithms are expressed in terms of both n and k (where k is the number of pairs that are generated). It is so because we can easily assert that such algorithms are at least Ω ( k ) (this is not true for the counting case). Therefore, ideally we would like to achieve a O ( n + k ) complexity . For the algorithm in the solution, after sorting, let k i be the number of pairs generated when visiting the i th element in the list x i . Then we do k i + 1 computations for x i (the extra computation for the point that exceeds the radius ). Running time is thus
T ( n , k ) = n l g n + i = 1 ∑ n k i + 1 = n l g n + n + i = 1 ∑ n k i = n l g n + n + k = O ( k + n l g n )
I believe this doesn't change even if we use binary search to find the index, we would still have to report the k i elements.
For an O ( n + k ) algorithm that uses bucketing, take a look at this .
I wrote a script in python which returned exactly 10000 more than the answer, can anyone tel me why? l=[the list] s=0 for i in range(10000):
...for n in range(i,10000):
......if abs(l[n]-l[i])<=1000:
.........s+=1
...if i%100==0:
......print(str(i//100)+'%')
(the last bit is just a progress monitor as the function is very inefficient) Just to reiterate the program returned 10053 and I have no idea why. Thanks in advance
Log in to reply
Your second for loop should be : for n in range(i+1,10000). Else you will end up comparing 10000 same pairs of numbers which differences is 0.
Log in to reply
Ah yes, It's obvious now that you've pointed it out! Thank you
1 2 3 4 5 6 7 |
|
A MATLAB Code
SetS = dlmread('points.txt',',');
S = 0;
for i = 1:l(ength(SetS)-1)
Nop = length(find(abs(SetS((i+1):end)-SetS(i))<=1000));
S = S+Nop;
end
S
This code searches for all points q to the right of a point p in the list, such that ∣ p − q ∣ ≤ 1 0 0 0 , in one go. Thus, there would be 9 9 9 9 vector comparisons done, as compared to 2 9 9 9 9 × 1 0 0 0 0 = 4 9 9 9 5 0 0 0 scalar comparions.
Problem Loading...
Note Loading...
Set Loading...
While admittedly there are faster approaches( O ( n + k ) ) , I will describe an O ( n lo g ( n ) + k ) algorithm(where n is the number of points and k is the number of pairs that satisfy the property). This algorithm suffices for our case and is easy to understand. We begin by sorting the set of points( O ( n lo g ( n ) ). We then proceed to iterate through each point P i . From there we will go through every point P j where j > i until P j − P i > 1 0 0 0 . This generates all the unordered pairs.
EDIT: Despite the problem statement, this algorithm finds all the points and then counts them.(instead of directly counting them). I believe this is a more general and applicable version of the classic computational geometery problem.