In this challenge we considered a frog hopping around a lily pond. To recap the lily pond was represented as a finite list of positive integers. The frog can only jump forward or backwards by a distance equal to the number at its current location. So for example:
[2, 3, 1, 4, 1]
🐸
Here the frog is on a 1 so it can move forward 1 to the 4 or backwards 1 to the 3. In the last challenge the frog had a specific starting location, but in this one we don't care about that. We just care what jumps a frog can make from a particular lily pad.
We can represent these as a directed graph, where each pad is a vertex and the edges represent hops the frog can make. Thus each vertex can have at most 2 children (forward and backwards), vertices can have fewer children if jumping in a certain direction would put them out of bounds. And indeed at least two vertices in every lily pond have fewer than two children.
As an example the pond shown above [2,3,1,4,1] can be represented as the graph:
{ 0 : [ 2 ]
, 1 : [ 4 ]
, 2 : [ 1, 3]
, 3 : []
, 4 : [ 3 ]
}
Of course not all graphs where each node has fewer than 2 children represents a lily pond. For example, the complete graph of order 3:
{ 0 : [ 1, 2 ]
, 1 : [ 0, 2 ]
, 2 : [ 0, 1 ]
}
Each node has two children, but the first element of the list can have at most 1 child (can't go backwards from the start). So none of the elements can go first, thus this can't be a lily pond.
Task
Your answer should take as input a directed graph which satisfies the following properties:
- it doesn't have a vertex with more than 2 outgoing edges (children).
- it doesn't have a self loop (an edge from a vertex to itself).
- it doesn't have two edges with the same origin and destination vertices (the graph is simple).
Graph nodes are unlabeled and unordered.
Your answer should output one of two consistent values. The first if the input represents some lily pond, the second if it does not. What the two values are are up to you to decide.
This is code-golf the goal is to minimize your source code as measured in bytes.
Test cases
Test cases are given where each element is a list of indices that are children. Note that since graph nodes are unlabeled and unordered the order given in the input in the case below does not represent positioning of the potential solution. The second test case should demonstrate this.
Represents a lily pond
Here each input is provided with a potential solution. The solution does not need to be output it is just there to help you.
[[2],[4],[1,3],[],[3]] ~ [2, 3, 1, 4, 1]
[[],[2],[0],[4],[0,1]] ~ [2, 3, 1, 4, 1]
Represents no lily ponds
[[1,2],[0,2],[0,1]]
[[1,2],[],[],[0,1]]
[[1,2],[],[],[],[1,2]]
