14
2
Background
A spanning tree (Wikipedia) of an undirected graph is a subgraph that is a tree which includes all of the vertices of the original graph. The following is an example of a spanning tree of a 4-by-4 grid graph.
Task
Given two positive integers w
and h
, output any valid spanning tree of the grid graph which has w
vertices horizontally and h
vertices vertically. There are lots of valid answers; just output one of them. Your code is even allowed to give different outputs across runs, as long as all of them are valid.
Input and output
The input is two positive integers w
and h
.
The output is a representation of a spanning tree. The output format is restricted to "a list of pairs of numbered vertices", in order to prevent various boring answers (as discussed in the sandbox). Also, the vertex numbering should be in row-major order, e.g.
0 - 1 - 2 - 3
| | | |
4 - 5 - 6 - 7
| | | |
8 - 9 - 10- 11
or
(0,0) - (0,1) - (0,2) - (0,3)
| | | |
(1,0) - (1,1) - (1,2) - (1,3)
| | | |
(2,0) - (2,1) - (2,2) - (2,3)
You can choose to use 0-based or 1-based indexing for vertices. Also, you may output nested or flattened arrays.
Scoring & winning criterion
The standard code-golf rules apply. The shortest valid submission in bytes wins.
Example I/O
w=3, h=2
If you want to output the following graph
+-+-+
| |
+ +-+
The list-of-vertex-pairs representation will be (using 0-based indexing for example):
[(0, 1), (0, 3), (1, 2), (1, 4), (4, 5)]
or
[((0, 0), (0, 1)),
((0, 0), (1, 0)),
((0, 1), (0, 2)),
((0, 1), (1, 1)),
((1, 1), (1, 2))]
w=1, h=1
The graph is a single vertex with no edges, and its only spanning tree is the graph itself. Your code should output an empty list (which means no edges).
1Does rule anything prevent us from always zig-zagging through the array? E.g. 0 → 1 → 2 → 3 ↓ 7 ← 6 ← 5 ← 4 ↓ 8 → 9 → 10 → 11 – Adám – 2019-11-07T02:33:29.530
@Adam It doesn't, as far as I see. You can also simply use, for example, a vertical line combined with many horizontal ones. – my pronoun is monicareinstate – 2019-11-07T02:54:38.727
Can we input h and w in unary? Can we input them as a grid made of a specific character? – my pronoun is monicareinstate – 2019-11-07T04:30:42.347
@someone Unary: yes, but only if it's the most natural representation of a positive integer in your language of choice. Grid input: no. – Bubbler – 2019-11-07T04:34:42.363