1 /* -*- mode: C -*-  */
2 /* vim:set ts=4 sw=4 sts=4 et: */
3 /*
4    IGraph library.
5    Copyright (C) 2003-2021 The igraph development team
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301 USA
21 
22 */
23 
24 #include "igraph_games.h"
25 
26 #include "igraph_interface.h"
27 
28 /**
29  * \ingroup generators
30  * \function igraph_k_regular_game
31  * \brief Generates a random graph where each vertex has the same degree.
32  *
33  * This game generates a directed or undirected random graph where the
34  * degrees of vertices are equal to a predefined constant k. For undirected
35  * graphs, at least one of k and the number of vertices must be even.
36  *
37  * </para><para>
38  * Currently, this game simply uses \ref igraph_degree_sequence_game with
39  * the \c SIMPLE_NO_MULTIPLE method and appropriately constructed degree sequences.
40  * Thefore, it does not sample uniformly: while it can generate all k-regular graphs
41  * with the given number of vertices, it does not generate each one with the same
42  * probability.
43  *
44  * \param graph        Pointer to an uninitialized graph object.
45  * \param no_of_nodes  The number of nodes in the generated graph.
46  * \param k            The degree of each vertex in an undirected graph, or
47  *                     the out-degree and in-degree of each vertex in a
48  *                     directed graph.
49  * \param directed     Whether the generated graph will be directed.
50  * \param multiple     Whether to allow multiple edges in the generated graph.
51  *
52  * \return Error code:
53  *         \c IGRAPH_EINVAL: invalid parameter; e.g., negative number of nodes,
54  *                           or odd number of nodes and odd k for undirected
55  *                           graphs.
56  *         \c IGRAPH_ENOMEM: there is not enough memory for the operation.
57  *
58  * Time complexity: O(|V|+|E|) if \c multiple is true, otherwise not known.
59  */
igraph_k_regular_game(igraph_t * graph,igraph_integer_t no_of_nodes,igraph_integer_t k,igraph_bool_t directed,igraph_bool_t multiple)60 int igraph_k_regular_game(igraph_t *graph,
61                           igraph_integer_t no_of_nodes, igraph_integer_t k,
62                           igraph_bool_t directed, igraph_bool_t multiple) {
63     igraph_vector_t degseq;
64     igraph_degseq_t mode = multiple ? IGRAPH_DEGSEQ_SIMPLE : IGRAPH_DEGSEQ_SIMPLE_NO_MULTIPLE;
65 
66     /* Note to self: we are not using IGRAPH_DEGSEQ_VL when multiple = false
67      * because the VL method is not really good at generating k-regular graphs.
68      * Actually, that's why we have added SIMPLE_NO_MULTIPLE. */
69 
70     if (no_of_nodes < 0) {
71         IGRAPH_ERROR("number of nodes must be non-negative", IGRAPH_EINVAL);
72     }
73     if (k < 0) {
74         IGRAPH_ERROR("degree must be non-negative", IGRAPH_EINVAL);
75     }
76 
77     IGRAPH_VECTOR_INIT_FINALLY(&degseq, no_of_nodes);
78     igraph_vector_fill(&degseq, k);
79     IGRAPH_CHECK(igraph_degree_sequence_game(graph, &degseq, directed ? &degseq : 0, mode));
80 
81     igraph_vector_destroy(&degseq);
82     IGRAPH_FINALLY_CLEAN(1);
83 
84     return IGRAPH_SUCCESS;
85 }
86