Algorithm Ideas?

Armitage

Banned
Feb 23, 2001
8,086
0
0
So I have a vector of objects. Each object has a range represented by a high & low value associated with it. For every object in the list, I need to find every other object which has an overlapping range.

I have an algorithm that works, but I suspect it could be faster.

Currently, I form a vector of pointers to the original list and sort it by the low value for the objects. I then walk through this list, and for each object, I do a binary search of the list from the current point forward for the objects high value. This diagram may help. This only captures the overlappers that have a higher low value then the current object, but that's OK. As long as each overlapping pair is captured somewhere, we're good, and these missing overlaps are captured in the other objects.

Any ideas? I was thinking of making a list with both the low & high values of each object, and sorting that. If you could look at each object and can know where it's high & low values resided in the list, you wouldn't have to do all those searches. You could just grab everything in the list between those positions, eliminate the duplicates, and go. But I can't seem to come up with a data structure that will accomplish that? How can I sort a list of objects such that each object knows its position in the final sorted list? I was thinking maybe some kind of tree representation instead of a flat vector, but haven't quite wrapped my mind around that yet.

I know I'm using list & vector interchangebly - I'm not worried about what kind of container I actually use. I currently use STL vectors. I need to do this in C++.
Any ideas?
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
How can I sort a list of objects such that each object knows its position in the final sorted list?

I may be missing something here, but if your objects are being sorted into an array/list/vector/whatever, won't they know their position in the sorted list just by the index they occupy in the final list?
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Zugzwang152
How can I sort a list of objects such that each object knows its position in the final sorted list?

I may be missing something here, but if your objects are being sorted into an array/list/vector/whatever, won't they know their position in the sorted list just by the index they occupy in the final list?

If I know the index in the final list, I know the object. But not vice-versa. Remember the "final list" is an array of pointers to the original list I'm looking for a way to look at the original object, and know its position in the sorted list of pointers.

Each object will exist in the pointer list twice - once for its low value, and once for its high value. So, what I'd like to do is iterate on the original object list, and retrieve the indexes in the sorted object list ("final list") for it's high & low values.
 

Extrarius

Senior member
Jul 8, 2001
259
0
0
You could add 'operator <' to your object (if it doesn't have one) and use a map<object, int> to store the object->index mapping to be able to get the index of an object. If you have two indexes per object, you could either maintain two maps as you sort or make it a map<object, pair<int, int> >
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Originally posted by: Armitage
Originally posted by: Zugzwang152
How can I sort a list of objects such that each object knows its position in the final sorted list?

I may be missing something here, but if your objects are being sorted into an array/list/vector/whatever, won't they know their position in the sorted list just by the index they occupy in the final list?

If I know the index in the final list, I know the object. But not vice-versa. Remember the "final list" is an array of pointers to the original list I'm looking for a way to look at the original object, and know its position in the sorted list of pointers.

Each object will exist in the pointer list twice - once for its low value, and once for its high value. So, what I'd like to do is iterate on the original object list, and retrieve the indexes in the sorted object list ("final list") for it's high & low values.

that sounds complicated as hell, but my guess would be you'd need an attribute in the object to hold either the indexes themselves (passed to the object while the pointer array is being populated) or at least a pointer to the array of pointers (in which case you'd have to iterate through the whole damn array everytime you wanted to find the position of those two values).

how to implement that is beyond my CS-flunked-out MIS ass.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Just took a closer look at it - the idea appears to be DOA. The cost of simply sorting a list that's twice as long eats up and passes any potential gains.

My current method: O(nlog(n) + sum(log(n-i)) where i=1->n-1)
Other idea: > O(2nlog(2n))

For n in the neighborhood that I'm interested in, the 2nd method is already 14% greater.

edit - More simply, my existing method is better then O(2nlog(n)) while the newer idea is worse then O(2nlog(2n))

Ideas encountered on airplanes and in a state of sleep deprivation may not survive closer examination when well rested and on the ground I knocked out my other "inspiration" from that flight yesterday.

Any other ideas about fast algorithms to find these overlaps?
 

znaps

Senior member
Jan 15, 2004
414
0
0
Sounds like you'd need an array of Sets. Each set will contain one or more objects in your original Vector. Creating it wouldn't be too difficult but I have no idea on the performance.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |