13
3
Task
You will be given a positive integer and you must output a "self-complementary graph" with that many nodes. If you don't know what a self-complementary graph is the wikipedia article wont help you much so below are two explanations, a technical and a non-technical one.
Non-Technical
A graph is a set of nodes that are connected by lines. Each pair of points can be connected by one line or none. The "complement" of a graph is the result of taking a graph and connecting all the nodes that are not connected and disconnecting all the nodes that are.
A self-complementary graph is a graph whose complement can be rearranged into the shape of the original. Below is an example of a self-complementary graph and a demonstration of how.
Here is a graph with 5 nodes:
We will highlight the all the places where connections could go with red dotted lines:
Now we will find the complement of the graph by swapping the red and black edges:
This does not look like the original graph but if we move the nodes around like so (each step swaps two nodes):
We get the original graph! The graph and its complement are the same graph
Technical
A self-complementary graph is a graph that is isomorphic to its complement.
Specifications
You will receive an positive integer via whatever method suits you best. And you will output a graph in whatever method you deem appropriate, this includes but is not limited to Adjacency Matrix Form, Adjacency List Form, and of course pictures! The outputted graph must be its own complement and have as many nodes as the integer input. If no such graph exists you must output a falsy value.
This is code-golf and you should aim to minimize your byte count.
Test Cases
Below are pictures of possible outputs for several n
4

5

9







A self-complementary graph can only exist where the complete graph has an even number of edges. Are we guaranteed this? – xnor – 2017-01-24T21:11:04.770
@xnor I forgot to include that. Fixed now. – Post Rock Garf Hunter – 2017-01-24T21:12:07.543
Do we have to handle negative inputs? – xnor – 2017-01-24T21:12:54.990
@xnor No. I will fix the question to be congruent – Post Rock Garf Hunter – 2017-01-24T21:13:51.733
What should be the output for 1? I believe the empty graph on 1 vertex is technically self-complementary. – xnor – 2017-01-24T21:14:59.223
@xnor All order-1 and order-0 graphs are self complementary. – Post Rock Garf Hunter – 2017-01-24T21:16:22.013
Is the following a legitimate output format: a list of pairs of integers between 1 and the input, where each pair represents an edge connecting the corresponding vertices? For example,
{{1,2},{2,3},{3,4}}if the input is4? (And if so, also checking that the empty list{}, which is not falsy for me, suffices if the input is0or1.) – Greg Martin – 2017-01-24T21:47:09.570@GregMartin If you deem that method appropriate it is. I really care very little about how things are outputted and want the challenge to be about manipulating graphs.
{}should suffice for0and1. – Post Rock Garf Hunter – 2017-01-24T21:49:22.2103Before anyone gets the idea of basing an answer on
GraphData@{"SelfComplementary",{#,1}}&, I believe that simply loads some examples for lownfrom Wolfram's database, so this won't work for arbitrarily large inputs. – Martin Ender – 2017-01-24T22:03:09.983There is an error in the graph for 8. Each vertex should have an average of 7/2=3.5 edges, but instead has only an average of 2.5 vertices. A solution is to draw in 4 edges forming a square. See my answer – Level River St – 2017-01-25T22:53:47.403
@LevelRiverSt Ah I seem to have commented those out in my code. Good catch, I will fix when I have the time. – Post Rock Garf Hunter – 2017-01-25T22:56:17.353