8
Suppose you have a set of sets of integers. It's possible that some of the sets will overlap (i.e. sharing elements). You could get rid of the overlaps by deleting elements from the sets, but then some of them might end up empty; that would be a shame. Can we make all the sets disjoint without emptying any of them?
Note that in this situation, there's never any reason to leave multiple elements in a set, so this problem can always be solved by reducing each set to just one element. That's the version of the problem we're solving here.
The task
Write a program or function, as follows:
Input: A list of sets of integers.
Output: A list of integers, of the same length as the input, for which:
- All integers in the output are distinct; and
- Each integer in the output is an element of the corresponding set of the input.
Clarifications
- You can represent a set as a list if you wish (or whatever's appropriate for your language), disregarding the order of elements.
- You don't have to handle the case where no solution exists (i.e. there will always be at least one solution).
- There might be more than one solution. Your algorithm must always produce a valid solution, but is allowed to be nondeterministic (i.e. it's OK if it picks a different valid solution each time it runs).
- The number of distinct integers appearing in the input, n, will be equal to the number of sets in the input, and for simplicity, will be the integers from 1 to n inclusive (as their actual values don't matter). It's up to you whether you wish to exploit this fact or not.
Testcases
[{1,2},{1,3},{1,4},{3,4}] -> [2,3,1,4] or [2,1,4,3]
[{1,3},{1,2,4},{2,3},{3},{2,3,4,5}] -> [1,4,2,3,5]
[{1,3,4},{2,3,5},{1,2},{4,5},{4,5}] -> [1,3,2,4,5] or [3,2,1,4,5] or [1,3,2,5,4] or [3,2,1,5,4]
Victory condition
A program requires an optimal time complexity to win, i.e. if an algorithm with a better time complexity is found, it disqualifies all slower entries. (You can assume that your language's builtins run as fast as possible, e.g. you can assume that a sorting builtin runs in time O(n log n). Likewise, assume that all integers of comparable size to n can be added, multiplied, etc. in constant time.)
Because an optimal time complexity is likely fairly easy to obtain in most languages, the winner will therefore be the shortest program among those with the winning time complexity, measured in bytes.
If it makes no sense then why is it that they understood it over here http://www.dreamincode.net/forums/topic/408035-has-this-algorithm-been-written-yet/
– fred russell – 2017-12-05T13:43:42.157@fredrussell maybe, just maybe, it's about how you explain it, and not about, e. g. the notation on the image? – NieDzejkob – 2017-12-05T13:44:53.107
@fredrussell This is the wrong stackexchange for this question. On our site we host programming competitions. You may be able to get help on Computer Science Stackexchange, but be sure to read their help center first. – user202729 – 2017-12-05T13:46:27.097
7@fredrussell your explanation of the "challenge" is unclear and not formatted. On this site you'll usually find properly formatted and ordered questions, for example following a layout like "Input; Output; Rules; Testcases", but you aren't providing anything of that. Additionally, you don't have a winning criteria which could determine a winner. And after your insult I don't think anyone is willing to solve the question now. Even on SO, you should always keep in mind that the people answering are doing this in their free time, so you should not be rude like that. – Luca H – 2017-12-05T13:49:34.597
Open question: is an optimal time complexity (i.e. without actually giving an upper bound) really covered by the restricted-complexity tag? Right now, it looks more like a fastest-code challenge with code-golf as the 2nd winning criterion. – Arnauld – 2017-12-05T17:30:00.247
1@Arnauld [tag:fastest-code] would imply that if we both write O(n) algorithms, but yours is 100 times faster, then you win. If we only require an optimal time complexity, then it's okay if my code is 100 times slower, as long as it's one byte smaller. But this challenge might well count as [tag:fastest-algorithm]. – Misha Lavrov – 2017-12-05T17:32:26.617
Wikipedia leads me to think that the optimal known time complexity for this problem is O(n^ω), the speed of fast matrix multiplication. I'm looking forward to seeing solutions based on that. – Misha Lavrov – 2017-12-05T17:49:18.603
2
This is exactly the problem of finding a full matching in a bipartite graph. The time complexities of the best known algorithms depend on how large the sets are compared to the number of sets. Related challenge.
– Zgarb – 2017-12-05T20:38:19.717I don't like that because perfect matching is a famous problem, likely the thing to do is to copy published algorithms. – xnor – 2017-12-05T23:49:55.620
@fredrussell Do you want the question to be formed this way?
– user202729 – 2017-12-06T01:23:14.9031
If you do, then just keep it this way, and keep in mind how to write a challenge next time. (Sandbox)2
If you don't like "obscure answers written in obscure languages", and what you do is purely asking for homework/algorithm/problem, then this is not the correct SE. Go to Computer-science SE for that.3
Be nice when comment.