Place Intervals to Maximize Number of Adjacency - algorithm

My problem:
I have n "items" to place on an integer axis. Each item contains several choices of placement, represented as closed intervals of integers. These intervals can have some overlapping elements. The goal is to find a non-overlapping placement (if any) of the n items on the int axis with maximal number of interval adjacency.
More details with the terms used above:
1) Overlapping: interval [1, 4] and [3, 6] have two overlapping elements {3} and {4}; interval [2, 5] and [6, 10] do not overlap.
2) Interval adjacency: interval [a, b] and [b+1, c] are called adjacent. The number of interval adjacency for this example is 1. The maximal possible number of interval adjacency for n items is n-1, which occurs when the placement makes n intervals pair-wisely adjacent.
Example:
There are 3 items; their placement choices are listed here
item1 has 2 choices: [1, 4], [2, 5]
item2 has 3 choices: [5, 8], [9, 11], [16, 18]
item3 has 2 choices: [3, 5], [13, 15]
One feasible placement is
[1, 4](item1), [5, 8](item2), [13, 15](item3)
Another two feasible placement are
[1, 4](item1), [16, 18](item2), [13, 15](item3); and
[2, 5](item1), [16, 18](item2), [13, 15](item3).
All these three placement in this example are optimal. The number of interval adjacency is 1.
My question:
Is there a better way than enumeration of all possibilities?
I can only think of that if an interval choice of an item overlaps with all the choices of another item, then this choice can be excluded. Any ideas are welcome:)

Related

The best solution (considering time complexity) for the function implementation

A function does the following task:
For example L = [[1, 2, 3], [1, 2], [1, 2, 3, 5, 6, 8], [1, 8, 6, 10, 21], [1, 4, 6, 9], [22]]; (array of arrays)
find out the index number of L such that all digit numbers in the value(sub-array) don't appear in other sub-arrays. In this example, the function would return 5 (the index of [22]) because 22 is only in this sub-array.
What could be the optimal solution in time complexity
The algorithm is to keep track of all the numbers you've seen so far (for example in a hashset), and process the sub-arrays one by one until you find one which matches your condition. In the worst case it's O(n) basic set operations, where n is the sum of the lengths of the subarrays of L. This is O(n) comparisons on average if you use a hashset.

Minimize the difference of the distance between points on the line

My problem is as follows:
Given n points on a line segment and a threshold k, pick the points on the line so that would minimize the average difference of the distance between each consecutive point and the threshold.
For example:
If we were given an array of points n = [0, 2, 5, 6, 8, 9], k = 3
Output: [0, 2, 6, 9]
Explanation: when we choose this path, the difference from the threshold in each interval is [1, 1, 0] which gets an average of .66 difference.
If I chose [0, 2, 5, 8, 9], the differences would be [1, 0, 0, 2], which averages to .75.
I understand enough dynamic programming to consider several solutions including memorization and depth-first search, but I was hoping someone could offer a specific algorithm with the best efficiency.

Partition an array into subsets of fixed size with minimum sum difference

I have found versions of this problem for either 2 subsets of half the size of the original array or with any number of subsets of any length. Does anybody have any pointers to any good solution for this problem? (can be greedy)
Given an array of positive numbers of length N (can have repetitions)
Partition the N numbers into subsets of length M with their sum difference minimized.
Simple example:
N=9, M=2
[5, 2, 3, 7, 5, 3, 7, 8, 1]
into
[[8, 1], [7, 2], [7, 3], [5, 5]] + [3] (leftover)
9 9 10 10
My real world use case is to group files of different sizes into batches of a given length but having the total size of each batch be as close as possible to that of the other batches.
Thanks!

Efficiently finding overlapping intervals from a list of intervals

This is related to finding overlapping intervals. I know how to do so given a list of intervals (interval trees). What I have is a list of list of intervals. For example,
[2,6], [7,11]
[1,3], [5,10], [11,13]
[2,5], [6,8]
The result for this should be
[2,3], [7,8]
What I need to do is to find a list of intervals that are common in all the lists.
I see this problem as similar to merging n lists. The problem is I cannot apply pairwise merging of lists. Applying this method can cause a loss of overlapping intervals. So I need to merge all the lists together considering all of them at a time (instead of pairwise).
I can use interval trees. Inserting the first intervals from each list to the interval tree and finding the overlap. Removing the weakest interval from the tree and inserting next interval from one of the lists. I haven't yet completely figured out how I can use this method but it seems It'll get too expensive.
Is there any efficient algorithm for finding overlapping intervals from a list of list of intervals.?
Additional Info:
The intervals within a list are sorted. They don't overlap and form a sequence.
Create a single, sorted array of transitions. Each transition has a position, and a cumulative number based on how many intervals you're joining or leaving. As you pass through the list, keep track of how many intervals you are in. When you're in as many intervals as series, that's when you're in a common interval.
For your example the transitions would be:
[2, 1], [6, -1], [7, 1], [11, -1],
[1, 1], [3, -1], [5, 1], [10, -1], [11, 1], [13, -1]
[2, 1], [5, -1], [6, 1], [8, -1]
which after sorting by position and merging collapses to:
[1, 1], [2, 2], [3, -1], [5, 0], [6, 0], [7, 1], [8, -1], [10, -1], [11, 0], [13, -1]
which gives you transitions for running totals of:
[1, 1], [2, 3], [3, 2], [7, 3], [8, 2], [10, 2], [13, 1]
And then we can read off the intervals where we're at 3 as one starting at 2 and going to 3, and another starting at 7 and going to 8. Which is the answer.
The idea of creating one long list and sorting is admittedly extra work. You can instead create those lists and merge them on the fly. The savings is a factor of the log of the number of series rather than the log of the number of events.
My understanding of what you want to do is to apply the intersection operation over list of intervals. And you can do this pairwise as intersection is associative.
What I would do is something like
Let S be the set of sets, R = s1, s1 in S
for each set s2 in S / {s1}
for each element e1 in R
for each element e2 in s2 s.t. e1.sup < e2.inf
e1 <- intersection (e1, e2)
And the intersection operation between two intervals is
intersection (e1,e2):
return new Interval(max(e1.inf, e2.inf), min (e1.sup, e2.sup));
You said each individual list of intervals is sorted and non-overlapping. So,
Keep track of where you are in each list, starting at the beginning of each.
While none of the lists has run out:
If the current intervals (one from each list) all overlap:
Output the intersection of the current intervals
Find which of the current intervals has the earliest end point
Advance one position within that list.
If there are K lists of intervals and N intervals altogether, this should take O(N K) time if implemented in the most straightforward way, but you should be able to reduce this to O(N log K) time by tracking information about the current intervals in a heap or some other priority queue.

Given some integer ranges, finding a smallest set containing at least one integer from each range

How can I find a set of minimum number of integers such that, for some given ranges of integers, for each range, the set contains at least one integer. For example, if I'm given these ranges :
[0, 4], [1, 2], [5, 7], [6, 7], [6, 9], [8, 10]
Then some solution sets are : { 1, 6, 8 }, { 2, 7, 9 }, { 1, 7, 8 } etc.
Imagine you draw all your ranges, ordered by end value, as you would draw meetings inside a day planner.
You can visually choose your numbers in a greedy manner, such that the first one is the segment that finishes first (in your example, that would be 2).
Then you erase all segments that contain that number, and you start all over.
This algo would yield solution { 2, 7, 10 }
0 1 2 3 4 5 6 7 8 9 10
----
-------------
^ -------
| ----
----------
^ -------
| ^
|
Algorithm:
Sort the start and end points. Pass over them until you meet an endpoint. Add it to the answer and remove all ranges which startpoints already passed (i.e. which contain current endpoint). Repeat until there's any point left.
Example:
[0, 4], [1, 2], [5, 7], [6, 7], [6, 9], [8, 10]
After sorting will become
[0, [1, 2], 4], [5, [6, [6, 7], 7], [8, 9], 10], ans = []
First endpoint is 2], we add it to ans and remove ranges opened before it, i.e. [0 and [1:
[5, [6, [6, 7], 7], [8, 9], 10], ans = [2]
Now first endpoint is 7] and we remove ranges [5, 7], [6, 7], [6, 9]:
[8, 9], ans = [2, 7]
Finally add 9 and remove the last range. The result will be [2, 7, 9].
Complexity:
Sorting will take O(nlogn) time, after that you'll pass on each element twice: once when looking for next endpoing and once when removing all currently opened intervals, which is linear, and total complexity will be O(nlogn) which comes from sorting.
We sort the intervals by the end numbers. For any interval, if its start is not greater than the previous end, (end is not smaller than the previous end since the intervals have been sorted), then we have an overlap at the previous end, and can skip this interval. If the start of the current interval is greater than the previous end, we have no overlap, and add the current end to the result set.
Consider the intervals (0, 3), (2, 6), (3, 4), (6, 10). After sorting, we have (0, 3), (3, 4), (2, 6), (6, 10). We start with result = [3] and previous = 3. Since 3 <= previous, we skip the interval (3, 4); previous remains unchanged. Since 2 <= previous, we skip the interval (2, 6); previous remains unchanged. Lastly, since 6 > previous, we add 10 to the result, and update previous = 10. The algorithm terminates; the answer is [3, 10].
Time complexity: n log(n), where n is the number of intervals.

Resources