The problem is simple, There is some given 1D lines on a plane. We need to find the total size of space having at least one line.
Let me discuss this with an example image-
This may a case. Or
This may be a case or anything like this.
I know it is a basic problem of Sweep Line Algorithm.
But there is no proper document in the internet for it to understand properly.
The one best I have is a blog of Top Coder and that is here.
But it is not clear how to implement it or how may be the simulation.
If I want, we can do it in O(n^2) with 2 loops, but I can't realize how would be the procedure.
Or is there any better algorithm better than that O(n log n)?
Can anyone help me by sharing any Sudo Code or a simulation?
If Sudo code or example code is not available, a simulation for understanding is enough from where I can implement this.
Re- Problem calculating overlapping date ranges is not what I am looking because firstly, it is O(n^2) and so, it is not what I want. And it is not fully described like this question.
There is not so much info available for this topic.
So, I am sharing algorithm and a simulation with you which created by me for you and it is also with O(n log n) !!!!!
Let's start-
0
1
2
3
4
5
6
7
8
9
10
11
In your case, find the sum of all the elements of the array from 1 to end (index no 1 to m) and it is your answer.
But with this algorithm and array, you can easily have many more complex question answers like what is the length of space having 3 shadow = Arr3 and so on.
Now the question is what's about order, right?
So, Sorting = O(n log n) and sweeping = O(m) [m=no of action points, so m
So, total order is = O(n log n) + O(m) = O(n log n)
Think you can understand it easily and will be a great help for you and many others. And think you will be able to easily implement it.
Here's an approach that you may try (In C#. I've not tested it, so please forgive typo's and the like; just take the "idea"/strategy).
Performance is O(N log m), where m is the number of disjoint "shadows" you'll create. So, worst case (if ALL linesegments are disjoint with respect to their shadows) you'll have O(N logN), but when you only have few shadows it's essentially O(N).
Make an array/list of structures containing segment end point coordinate
X
and+1
attribute for starting point and-1
attribute for ending pointA
. O(N)Sort array by
X
key. O(NlogN)Init
CurrCount
to zero, run through array, addingA
attribute toCurrCount
. O(N)You'll get ranges where
CurrCount
is greater than zero (covered), and whereCurrCount
is zero (uncovered)Maybe you can do slightly better than with blind sorting, with the following scheme, derived from MergeSort:
assume you have two lists of non-overlapping intervals, sorted by increasing bounds;
perform a merging step like in MergeSort (always move to the closest bound from each list), to compute the union of the intervals;
based on the parity of the indexes from both lists, you can tell which bounds to emit (f.i. merging AB and CDEF with the ordering ACBDEF, the output will be ADEF).
This is a linear-time operation.
Equipped with this modified merge, you can implement a modified MergeSort, which starts with single intervals and recursively forms unions. In the end, you get a single list of intervals that will give you the requested size.
In the worst case, no bound will disappear and the process remains
O(N Log(N))
. But when sufficiently many interval unions do arise, the number of intervals decreases and the processing time can go down to linearO(N)
.This isn't very complicated.
Form an array where you put all interval endpoint abscissas with a flag telling whether it is a starting or ending point.
Sort the array increasingly.
Then scan the array while updating a counter: a starting point increases it, an ending point decreases it.
The requested size is easily found from the values where the counter switches from zero to nonzero and conversely.
I don't think it is possible to make it faster than O(N Log(N)) because of the sort (which I don't think can be avoided), unless the data allows linear-time sorting (such as histogram sort).