Given a list, L
, of sets of numbers like this:
[[1], [0,2,3], [3,1], [1,2,3]]
Output a single list of numbers such that 2 numbers A and B appear next to each other at least once if and only if B in L[A]
.
That is all such pairs must exist in the output, and only such pairs may exist in the output, where the order of any pair in the output may be read in either direction.
If A
is in L[B]
then B
is guaranteed to be in L[A]
, in other words, the relationship is symmetric.
In this case, one possible output would be this:
[0, 1, 2, 3, 3, 1, 3]
In this case, you can see 0
is only ever next to 1
, which matches our adjacency list. 1
appears next to 0
and 2
in the first instance and 3
in the second instance. It appears next to 3
again but that doesn't matter. Numbers are adjacent if they appear together at least once, but more doesn't matter. 2
appears next to 1
and 3
, and finally 3
appears next to 2
, 1
, and itself (necessary since L[3]
contains a 3
).
For any given adjacency list there are many possible solutions. You may choose to output any valid solution. Your solution does not need to be the shortest one, but it does need to have a finite length.
Test Cases
Input | Possible Solution |
---|---|
[[0]] |
[0, 0] |
[[1],[0]] |
[0, 1] |
[[0,1,2], [0], [0]] |
[1, 0, 0, 2] |
[[0,1,2], [0,1,2], [0,1,2]] |
[0, 1, 1, 2, 2,0, 0] |
[[1,2,3,4], [0], [0], [0], [0]] |
[1, 0, 2, 0, 3, 0, 4] |
[[1, 2], [0, 2], [0, 1]] |
[0, 1, 2, 0] |
[[], [2], [1]] |
[1,2] |
IO
Any of list of sets, dict of sets, list of lists, list of dicts, adjacency matrix, are valid input formats. You don't have to use numbers if you use a mapping, any primitive data type with a potentially infinite number of elements is valid.
For the input [[0,1,2], [0], [0]]
the following would all be possible input formats from you to choose from. You only need to handle one of these of your choice.
[[0,1,2], [0], [0]]
{0: {0,1,2}, 1: {0}, 2: {0}}
{'a': "abc", 'b': "a", 'c': "a"}
(Strings and arrays of characters are considered equivalent.[[1,1,1],[1,0,0],[1,0,0]]
(Adjacency matrix)
[[11],[3],[6],[1,4,7],[3,5,8],[4,6,8],[2,5,9],[3],[4,5,11],[6],[11],[8,10,0]]
. This seems to trip up some of the BFS answers. It's a triangular loop with three spokes each of which branch - like this. \$\endgroup\$