{'fraction_non_alphanumeric': 0.047507314388343325, 'fraction_numerical': 0.01355696532545407, 'mean_word_length': 3.6727125067677315, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 2, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Graver basis for an undirected graph and its application to testing the beta model of random graphs

In this paper we give an explicit and algorithmic description of Graver basis for the toric ideal associated with a simple undirected graph and apply the basis for testing the beta model of random graphs by Markov chain Monte Carlo method.


Introduction
Random graphs and their applications to the statistical modeling of complex networks have been attracting much interest in many fields, including statistical mechanics, ecology, biology and sociology (e.g. Newman [10], Goldenberg et al [6]). Statistical models for random graphs have been studied since Solomonoff and Rapoport [19] and Erdős and Rényi [5] introduced the Bernoulli random graph model. The beta model generalizes the Bernoulli model to a discrete exponential family with vertex degrees as sufficient statistics. The beta model was discussed by Holland and Leinhardt [8] in the directed case and by Park and Newman [15], Blitzstein and Diaconis [1] and Chatterjee et al [2] in the undirected case. The Rasch model [17], which is a standard model in the item response theory, is also interpreted as a beta model for undirected complete bipartite graphs. In this article we discuss the random sampling of graphs from the conditional distribution in the beta model when the vertex degrees are fixed.
In the context of social network the vertices of the graph represent individuals and their edges represent relationships between individuals. In the undirected case the graphs are sometimes restricted to be simple, i.e., no loops or multiple edges exist. The sample size for such cases is at most the number of edges of the graph and is often small. The goodness of fit of the model is usually assessed by large sample approximation of the distribution of a test statistic. When the sample size is not large enough, however, it is desirable to use a conditional test based on the exact distribution of a test statistic. For the general background on conditional tests and Markov bases, see Drton et al [4].
Random sampling of graphs with a given vertex degree sequence enables us to numerically evaluate the exact distribution of a test statistics for the beta model. Blitzstein and Diaconis [1] developed a sequential importance sampling algorithm for simple graphs which generates graphs through operations on vertex degree sequence. In this article we construct a Markov chain Monte Carlo algorithm for sampling graphs by using the Graver basis for the toric ideal arising from the underlying graph of the beta model. A Markov basis [3] is often used for sampling from discrete exponential families. Algebraically a Markov basis for the underlying graph of the beta model is defined as a set of generators of the toric ideal arising from the underlying graph of the beta model. A set of graphs with a given vertex degree sequence is called a fiber for the underlying graph of the beta model. A Markov basis for the underlying graph of the beta model is also considered as a set of Markov transition operators connecting all elements of every fiber. Petrović et al [16] discussed some properties of the toric ideal arising from the model of [8] and provided Markov bases of the model for small directed graphs. Properties of toric ideals arising from a graph have been studied in a series of papers by Ohsugi and Hibi ([11], [12], [13]).
The Graver basis is the set of primitive binomials of the toric ideal. Applications of the Graver basis to integer programming are discussed in Onn [14]. Since the Graver basis is a superset of any minimal Markov basis, the Graver basis is also a Markov basis and therefore connects every fiber. When the graph is restricted to be simple, however, a Markov basis does not necessarily connect all elements of every fiber. A recent result by Hara and Takemura [7] implies that the set of square-free elements of the Graver basis connects all elements of every fiber of simple graphs with a given vertex degree sequence. Thus if we have the Graver basis, we can sample graphs from any fiber, with or without the restriction that graphs are simple, in such a way that every graph in the fiber is generated with positive probability.
In the sequential importance sampling algorithm of [1] the underlying graph for the model was assumed to be complete, i.e., all the edges have positive probability. In our approach we can allow that some edges are absent from the beginning (structural zero edges in the terminology of contingency table analysis), such as the bipartite graph for the case of the Rasch model. In fact the Graver basis for an arbitrary graph is obtained by restriction of the Graver basis for the complete graph to the existing edges of G (cf. Proposition 4.13 of Sturmfels [20]). Moreover our algorithm can be applied not only for sampling simple graphs but also for sampling general undirected graphs without substantial adjustment. These are the advantages of the Graver basis.
The Graver basis for small graphs can be computed by a computer algebra system such as 4ti2 (4ti2 team [21]). For even moderate-sized graphs, however, it is difficult to compute the Graver basis via 4ti2 in a practical amount of time. In this article we first provide a complete description of the Graver basis for an undirected graph. In general the number of elements of the Graver basis is too large. So we construct an adaptive algorithm for sampling elements from the Graver basis, which is enough for constructing a connected Markov chain over any fiber. The recent paper of Reyes et al [18] discusses the Graver basis for an undirected graph and gives a characterization of the Graver basis. We give a new description of the Graver basis, which is more suitable for sampling elements from the Graver basis.
The organization of this paper is as follows. In Section 2 we give a brief review on some statistical models for random graphs and clarify the connection between the models and toric ideals arising from graphs. In Section 3 we provide an explicit description of the Graver basis for the toric ideal associated with an undirected graph. Section 4 gives an algorithm for random sampling of square-free elements of the Graver basis. In Section 5 we apply the proposed algorithm to some data sets and confirm that it works well in practice. We conclude the paper with some remarks in Section 6.

The beta model of random graphs
In this section we give a brief review of the beta model for undirected graphs according to Chatterjee et al [2].
Let G be an undirected graph with n vertices V (G) = {1, 2, . . . , n}. Here we assume that G has no loop. Let E = E(G) be the set of edges. For each edge {i, j} ∈ E, let a non-negative integer x ij be the weight for {i, j} and denote x = {x ij | {i, j} ∈ E}. x is considered as an |E| dimensional integer vector. We assume that an observed graph H is generated by independent binomial distribution B(n ij , p ij ) for each edge {i, j} ∈ E, i.e., x ij ∼ B(n ij , p ij ) with Then the probability of H is described as The model (1) is called the beta model [2]. Note that if x ij = 0 then the observed graph H does not have an edge {i, j} even if {i, j} ∈ E(G) for the underlying graph G. This model was considered by many authors (e.g. Park and Newman [15], Blitzstein and Diaconis [1] and Chatterjee et al [2]). The p 1 model for random directed graphs by Holland and Leinhardt [8] can be interpreted as a generalization of the beta model. When G is a complete bipartite graph, the beta model coincides with the Rasch model [17]. The many-facet Rasch model by Linacre [9], which is a multivariate version of the Rasch model, can be interpreted as a generalization of the beta model such that G is a complete k-partite graph.
Let d 1 , . . . , d n be a degree sequence, i.e., d i := j:{i,j}∈E x ij for each vertex i. Denote d := (d 1 , . . . , d n ). The sufficient statistic for (1) is d. Let A : |V | × |E| denote the incidence matrix between vertices and edges of G. Then it is easily seen that x and d are related as Ax = d.
A set of graphs (without restriction to be simple) F d = {x ≥ 0 | Ax = d} with a given degree sequence d is called a fiber for A (or for the underlying graph G). An integer array z of the same dimension as x is called a move if Az = 0. A move z is written as the difference of its positive part and negative part as z = z + − z − . Since Az = Az + − Az − , every move is written as the difference of two graphs in the same fiber. A finite set of moves is called a Markov basis for the incidence matrix A if for every fiber any two graphs are mutually accessible by the moves in the set [3]. By adding or subtracting moves in a Markov basis, we can sample graphs from any fiber in such a way that every graph in the fiber is generated with positive probability. Note that x ij in the beta model (1) is restricted as 0 ≤ x ij ≤ n ij . We denote the subset of the fiber F d with this restriction as To assess the goodness of fit of the beta model we usually utilize a large sample approximation of the distribution of a test statistics. However, when n ij 's are not large enough, it is not appropriate to use the large sample approximation. Especially, as mentioned in Section 1, graphs are restricted to be simple (n ij ≡ 1) in some practical problems. For a simple graph, x ij , {i, j} ∈ E, is either zero or one. A Markov basis for the incidence matrix A guarantees the connectivity of every fiber F d if the restriction that graphs are simple is not imposed. Under the restriction, however, a Markov basis does not necessarily connect the subset F d,1 of the fiber F d . For example, consider the beta model with the underlying graph G in Figure 1 and n ij = 1 for each edge {i, j} ∈ E. It can be shown that a set of all 4-cycles in G is a Markov basis for the incidence matrix of G. However x and y in Figure 1 are not mutually accessible by 4-cycles under the restriction that graphs are simple. For a given x, supp(x) = {e | x e > 0} denotes the set of observed edges of x. For two moves z 1 , z 2 , the sum z 1 + z 2 is called conformal if there is no cancellation of signs in . The set of moves which can not be written as a conformal sum of two nonzero moves is called the Graver basis. The Graver basis is known to be a Markov basis [e.g. 4]. A move is square-free if the absolute values of its elements are 0 or 1. By the same augment of Proposition 2.1 of Hara and Takemura [7], we can obtain the following proposition. Proposition 1. The Graver basis for the underlying graph of the beta model connects all elements of every fiber. Furthermore, the set of square-free moves of the Graver basis connects all elements of every fiber with the restriction of simple graphs.
Proof. Let x, y be two elements of the same fiber. The difference y − x is written as a conformal sum of primitive moves: where z i , 1 ≤ i ≤ r, are elements of the Graver basis. Since there is no cancellation of signs on the right hand side, x + z 1 + · · · + z k belongs to the same fiber for k = 1, . . . , r. Therefore the Graver basis connects all elements of every fiber. Suppose n ij = 1 for every {i, j} ∈ E in the setting of the beta model. It is easy to see that each z i is square-free in (2). It means that the set of square-free moves of the Graver basis connects all elements of every fiber with the restriction of simple graphs.
Therefore it suffices to have the Graver basis to sample graphs from any fiber with or without the restriction that graphs are simple. In the next section we derive the Graver basis for the underlying graph of the beta model

Graver basis for an undirected graph
In this section we will give two characterizations of the Graver basis for an undirected graph. Theorem 1 in Section 3.2 is the main result of this paper which gives a necessary and sufficient condition for a element of the Graver basis as a sequence of vertices. Proposition 3, which is used for the proof of Theorem 1, gives a characterization of the Graver basis through recursive operations on the graph, which is of some independent interests.

Preliminaries
The length of the walk w is the number of edges q of the walk. An even (respectively odd) walk is a walk of even (respectively odd) length. A walk w is For a walk w, let V (w) = {i 1 , . . . , i q+1 } denote the set of vertices appearing in w and let E(w) = {{i 1 , i 2 }, {i 2 , i 3 }, . . . , {i q , i q+1 }} denote the set of edges appearing in w. Furthermore let G w = (V (w), E(w)) be the subgraph of G, whose vertices and edges appear in the walk w.
In order to describe known results on the toric ideal I G arising from an undirected graph G, we give an algebraic definition of I G . Let K[t] = K[t 1 , . . . , t n ] be a polynomial ring in n variables over K. We will associate each edge . . , s m ] be a polynomial ring in m = |E(G)| variables over K and let π be a homomorphism from K[s] to K[t] defined by π : s r → t r . Then the toric ideal I G of the graph G is defined as The Graver basis of I G is the set of all primitive binomials belonging to I G and we denote it by G(I G ). If we write the monomials u, v as primitive if and only if supp(x)∩supp(y) = ∅ and x−y can not be written as a conformal sum of two nonzero moves.
For a given even closed walk w = (e j 1 , e j 2 , . . . , e j 2p ) we define a binomial f w ∈ I G as . Note that even if there is a proper subwalk w ′ of an even closed walk w, w ′ dose not necessarily go along with w, i.e., the edges of w ′ may not appear as consecutive edges of w. An even closed walk w is called primitive, if its binomial f w is primitive. Then the primitiveness of w is equal to non-existence of a proper subwalk of w.
A characterization of the primitive walks of graph G, which gives a necessary condition for a binomial to be primitive, was given by Ohsugi and Hibi [12].
). Let G be a finite connected graph. If f ∈ I G is primitive, then we have f = f w where w is one of the following even closed walks: (i) w is an even cycle of G. Every binomial in the first two cases is primitive but a binomial in the third case is not necessarily primitive.

Characterization of primitive walks
In this subsection we give a simple characterization of the primitive walks of a graph G as sequences of vertices. Express an even closed walk w as a sequence of vertices: denote the number of times i is visited in the walk w before it returns to the vertex i 1 . Consider the following condition for the even closed walk w. Figure  2).
Using Condition 1, we can characterize the Graver basis for a graph G as follows.
Theorem 1. A binomial f ∈ I G is primitive if and only if there exists an even closed walk w with f w = f satisfying Condition 1.

Remark 2.
It follows from the definition of primitive walks and Theorem 1 that if an even closed walk w is primitive, every even closed walk w ′ with f w ′ = f w is primitive and satisfies Condition 1. Figure 2: Even closed walk w.
Remark 3. As mentioned in Section 1, there is another characterization of Graver basis in Theorem 3.1 of Reyes et al [18]. It also gives a necessary and sufficient condition for the primitiveness of even closed walks, by using some new graphical concepts such as "block" and "sink". Our characterization in Theorem 1 gives a simpler description of Graver basis, because it does not need any new graphical concepts. Furthermore it is more convenient in the algorithmic viewpoint: When an even closed w is given as a sequence of vertices or edges, we can easily determine if w is primitive by checking directly Condition 1 without distinguishing any graphical objects.
Before proving Theorem 1, we state another characterization of primitive walks given in Proposition 3 below. In order to that, we need some more definitions on graphs. For a walk w = (e j 1 , e j 2 , . . . , e jq ), let W = W (w) denote the weighted subgraph (V (w), E(w), ρ) of G where ρ : E(w) → Z is the weight function defined by ρ(e) := #{l | e j 2l+1 = e}−#{l | e j 2l = e} for each edge e ∈ E(w). For simplicity, we denote a weight +1 (respectively −1) by + (respectively −) in our figures. For a vertex i ∈ V (w), we define two kinds of degrees of vertex i: deg Gw (i) is the usual degree of i in G w . Note that the same weighted graph W might correspond to two different even closed walks w, w ′ , i.e. W (w) = W (w ′ ). Given a weighted graph W , we say that w spans W if W = W (w) and {e j l | l:odd} ∩ {e j l | l:even} = ∅. Now we define two operations, contraction and separation, on a weighted graph W .
• Let e = {i, j} ∈ E(w) be an edge with |ρ(e)| = 2, whose removal from G w increases the number of connected components of the remaining subgraph. Contraction of e is an operation as shown in Figure 3. That is, it first replaces W by  Note that the separation is not defined for any vertex i with deg Gw (i) = deg W (i) = 4, if i fits to none of three cases (a)-(c) in Figure 4. The vertex i in Figure 5 is such an example, because its positive side fits to none of three cases (a)-(c) in Figure 4. Let insertion and binding be the reverse operations of contraction and separation, respectively. With these operations, consider the following condition for an even closed walk w = (e j 1 , e j 2 , . . . , e j 2p ).  We establish some lemmas to prove Proposition 3. Our proof also shows thatW in Condition 2 does not depend on the order of application of contractions and separations excepting the sign inversion of weights of edges of each connected component inW .

Lemma 1.
If w is a primitive walk, w satisfies (i) in Condition 2.
Proof. Consider a vertex i ∈ V (w). Since w is closed, deg W (i) is even. Furthermore, since w is primitive, {e j l | l:odd} ∩ {e j l | l:even} = ∅ holds which implies that there is no cancellation in the calculation of weight on any edge. Then, a half of the weight deg W (i)/2 is assigned as positive weights and other half deg W (i)/2 is assigned as negative weights to the edges connected to i on W . Therefore deg W (i) ∈ {2, 4, 6, . . .}. Now suppose deg W (i) ≥ 6. Consider that we start from a vertex i along an edge with positive weight and go along the walk w or its reverse until returning back to i again for the first time. Since w is primitive, we have to come back to i along an edge with positive weight for the first time. Let us continue along w or its reverse until returning back to i. By the same reasoning, the last edge of this closed walk has a negative weight. This implies that this even closed walk becomes a proper subwalk of w, a contradiction to the primitiveness of w. Therefore deg W (i) is 2 or 4.
To prove the remaining part, let i ∈ V (w) be a vertex with deg W (i) = 4 and consider all closed walks on W , where the edge starting from i and the edge coming back to i have positive weights. Let V + be the set of vertices other than i which appear in one of these walks and V − is defined in the same way.
Suppose that there exists a vertex j ∈ V + ∩ V − . Then, as shown in Figure 6, there are two closed walks ({i, i + 1 }, . This implies that we can construct a proper subwalk of w by the combination of {i, i + k }, Γ + k (k = 1, 2), and Γ − l , {i − l , i}(l = 1, 2), a contradiction to the primitiveness of w. Therefore V + ∩ V − = ∅. Second, suppose that the removal of the holds as shown above. Hence, as shown in Figure 7, an even closed walk is a proper subwalk of w for appropriate k, l ∈ {1, 2}, k = l, which contradicts to the primitiveness of w. Therefore the removal of i from G w increases the number of connected components of the remaining subgraph.
In the following four lemmas, we show that contraction, separation, and these inverse operations preserve the primitiveness of an even closed walk. The proofs of lemmas are postponed to Appendix. Figure 7: Case that there exists an edge {v + , v − }. Lemma 2. Let an even closed walk w be primitive andW be the weighted graph which is obtained by a contraction for an edge with its weight ±2 on W . Then any even closed walkw spanningW is primitive. Lemma 3. Let an even closed walk w be primitive and W 1 , W 2 be the weighted graphs obtained by the separation of a vertex i. Then any even closed walks w l (l = 1, 2) spanning W l (l = 1, 2) are primitive or of length two with f w l = 0.
Lemma 4. Let w be a primitive walk and letW be the weighted graph obtained by the insertion to i with deg W (i) = 4 on W . Then any even closed walkw spanningW is primitive.
Lemma 5. Let each w l (l = 1, 2) be a primitive walk or a closed walk with length two, and W be the weighted subgraph obtained by binding of W 1 and W 2 . Then any even closed walk w spanning W is primitive.
We now give proofs of Proposition 3 and Theorem 1.
Proof of Proposition 3. Let w be a primitive walk. From Lemma 1 w satisfies (i) in Condition 2 and every edge e with |ρ(e)| = 2 can be contracted. Furthermore, it is easy to see that every vertex i with deg W (i) = 4 can be separated after recursively applying contractions of all possible edges. Therefore deg W (i) = 2 holds for every vertex i onW . From Lemmas 2 and 3, each even closed walk corresponding to the connected component ofW is primitive or of length two. Then, every connected component ofW is an even cycle or an edge with weight 0, because from Proposition 2 every primitive walk includes a vertex i with deg W (i) = 4 if it is not an even cycle. Therefore, a primitive walk w satisfies Condition 2. Conversely, suppose an even closed walk w satisfies Condition 2. From Proposition 2 and Lemmas 4 and 5, w is primitive.
Conversely, let w be an even closed walk with Condition 1. From Proposition 3, it suffices to show that w satisfies Condition 2. The condition (i) in Condition 2 follows from Condition 1. Then, it is enough to confirm that w satisfies the condition (ii) in Condition 2.
First, we claim that every edge e ∈ E(w) with |ρ(e)| = 2 can be contracted and every vertex j with # w (j) = 2 and deg Gw (j) = 4, i.e. deg Gw (j) = deg W (j) = 4, can be separated. The case of contraction is obvious from Condition 1. We confirm the case of separation. Consider the vertex j in Figure 8. with weight +1, it belongs to the case (a) or (b) in Figure 4, respectively. Let us consider the case that there exists an edge {k 1 , k 2 } with weight −1 and suppose that the vertex k 1 connects to more than three edges as shown in Figure 9. Then, j, k 1 and k 2 appear in w like (j, k 1 , . . . , k 1 , k 2 , j) or (j, k 1 , . . . , k 1 , k 2 , . . . , k 2 , j), because V (w j 1 ) ∩ V (w j 2 ) = {j} holds. This implies that (k 1 , . . . , k 1 ) is even as shown in Figure 10, which contradicts Condition 1. Hence the case with {k 1 , k 2 } with weight −1 belongs to (c) in Figure 4. Therefore the claim is confirmed. Figure 10: Case that there exists a vertex j in Figure 9.
Second, we verify that contraction and separation on W preserve Condition 1. Consider the case of contraction of an edge {i, j} ∈ E(W ). From Condition 1, such i, j appear in w as w = (i 1 , . . . , i l 1 , i, j, i l 2 , . . . , i l 3 , j, i, i l 4 , . . . , i 1 ). The contraction of {i, j} is equivalent to replacing w by (i 1 , . . . , i l 1 , i, i l 2 , . . . , i l 3 , i, i l 4 , . . . , i 1 ). This change causes the decrease of two edges from w, and preserves Condition 1. The case of separation is checked in the same way.
Finally, consider the weighted graph W ′ obtained by all possible contractions and separations on W . From the claims above, every connected component of W ′ satisfies Condition 1 and has no vertex j with # w (j) = 2, i.e. an even cycle or an edge with weight 0. Therefore w satisfies Condition 2.

Algorithm for generating elements of Graver basis
In this section we present an algorithm for generating elements randomly from the Graver basis for a simple undirected graph. As shown in Proposition 1, for testing the beta model of random graphs with n ij = 1, we only need square-free elements of the Graver basis. Therefore the restriction to square-free elements of our algorithm will be discussed in Remark 4. Theorem 1 guarantees the correctness of our algorithm.
We need some tools in order to construct an algorithm. Let T be a weighted tree . . } is a weight function. For this weighted tree T , let us consider the following condition.
With these tools, let us consider generating an element of the Graver basis for a simple undirected graph G = (V (G), E(G)). For simplicity, first consider the case that that G is complete. We call an edge e with |ρ(e)| = 2 a cycle in G w for an even closed walk w in this section. We will discuss later the case that G is not complete. Let T = (V (T ), E(T ), µ) be a weighted tree satisfying Condition 3 and the following equation Then, we can construct a primitive walk in G using T as follows. First, we assign the set of vertices and every vertex v ∈ V (G) is assigned at most twice. Equation (3) guarantees that this assignment is possible. Second, we make cycles in G by arbitrarily ordering the vertices V v T . Then we make a subgraph of G by taking the union of these cycles. Finally, we obtain a closed walk by choosing a root vertex from this subgraph and going around it. It is easy to see that this closed walk is primitive by Theorem 1.
Conversely we can construct a weighted tree with Condition 3 and (3) from each primitive walk. Let w be a primitive walk. First, the vertex set V (T ) is constructed by creating a vertex v c of T for each cycle c in G w . Second, the edge set E(T ) is obtained by adding edge {v c , v c ′ } to E(T ) for each pair of cycles c, c ′ in G w with V (c) ∩ V (c ′ ) = ∅. Then, we assign weight µ(v c ) := |V (c)| to each vertex v c ∈ V (T ).
Therefore, once we have a weighted tree T with Condition 3 and (3), we can construct an element of the Graver basis for G. Such a tree T is constructed by the following algorithm.
(a) For each vertex v T ∈ V (T ) which is deepest from r, add edges {v T , v i T } to E(T ) and the endpoints v i T (i = 0, 1, . . . , I v T ) to V (T ), where the number I v T is randomly decided under the following two conditions: 5. If |V (T )| = 1 and µ(r) is odd, change µ(r) to µ(r) − 1 or µ(r) + 1.
6. If |V (T )| > 1 and T has a leaf with even weight, subtract or add 1 to the weight. 7. Output T .
Algorithm 1 provides a simple algorithm for generating an element of Graver basis as follows.
Algorithm 2 (Algorithm for generating an element of Graver basis). Input : A complete graph G = (V (G), E(G)). Output : A primitive walk w.  Algorithm 2 allows us to uniformly sample graphs with the common degree sequence via Metropolis-Hastings algorithm with the Graver basis, with or without the restriction that graphs are simple. It is done by constructing a connected Markov chain of graphs with the common degree sequence. In each iteration, a primitive walk is randomly generated by Algorithm 2. If the primitive walk is applicable, a new sample graph with the same degree sequence is obtained by adding the primitive walk, otherwise the primitive walk is rejected. Note that Metropolis-Hastings algorithm does not require the uniformity of the distribution of generated primitive walks. As long as there is a positive probability of generating every element of the Graver basis, the Metropolis-Hastings algorithm realizes uniform sampling of graphs with the common degree sequence.

Numerical experiments
In this section we present numerical experiments with elements of the Graver basis computed by Algorithm 2 in Section 4. The implementation of Metropolis-Hastings algorithm with Algorithm 2 is done by Java 1.6.0 on Windows OS with Intel(R) Core(TM) i7-2829QM CPU@2.30GHz.

A simulation with a small graph
We run a Markov chain over the fiber containing a small graph H 0 in Figure 13. The underlying graph G = K 8 is assumed to be complete with eight vertices. By the Markov chain we sampled 510,000 graphs in the fiber, including 10,000 burn-in steps. The number of types of obtained graphs in our chain is 591. By enumeration we checked that 591 is actually the number of the elements of the fiber of H 0 . The histogram of this experiment is shown in Figure 14. The horizontal axis expresses the frequency of each type of graph and the vertical axis expresses the number of types. The mean of the number of appearances of each type is 829 and the standard deviation is 179. This experiment shows that the algorithm samples each element of the fiber almost uniformly.
Although there is a self loop at the vertex 19 in the observation, we ignored it for simplicity.
We set the beta model (1) in Section 2 with n ij = 1 for each edge {i, j} as the null hypothesis. Then the probability of H is described as Parameter α i (i ∈ V ) is interpreted as the value of organism represented by the vertex i as a food to other organisms. Then the beta model (4) implies that a vertex i with large α i is likely to be connected to many edges. Let P ∈ (4) mean that P can be expressed by (4) for a set of parameters {α i } i∈V . Consider now the statistical hypothesis testing problem H 0 : P ∈ (4) versus H 1 : P / ∈ (4).
Starting from the graph in Figure 15, we construct a Markov chain of 10,100,000 graphs including 100,000 burn-in steps and compute the chi-square statistic of each graph as a test statistic. The running time of the calculation is 5 minutes and 4.8 seconds. Using the maximum likelihood estimator, the chi-square value of observed graph H is 477 and the histogram of the estimated distribution of the chi-square values is shown in Figure  Chi 16. The approximate p-value is 0.286. This value is not so small and there is no evidence against the beta model (4). Next we consider some other characteristics of the observed graph H and graphs obtained by the above Markov chain. We compute their clustering coefficient defined by Wattz and Strogatz [23] and also count the number of triangles (3-cycles). For the observed graph H, the values of clustering coefficient and the number of triangles are 0.447 and 101, respectively. For the sampled graphs, the histograms are obtained as in Figure 17 and 18 and their mean values are 0.436 and 92.4, respectively. The differences between the actual values and the means of sampled graphs are not large. It suggests that these statistics agree with the beta model (4).  As mentioned in Section 1 there are computer algebra systems such as 4ti2 (4ti2 team [21]) to compute the Graver basis. However the whole Graver basis is huge and difficult to compute even for a moderate-sized graph like the real data above. Algorithm 2, our adaptive algorithm, enables us to perform the Markov chain Monte Carlo method for such a moderate-sized graph.

Concluding remarks
In this paper we obtained a simple characterization of the Graver basis for toric ideals arising from undirected graphs. This Graver basis allows us to perform the conditional test of the beta model for arbitrary underlying graph. Our characterization allows us to construct an algorithm for sampling elements of the Graver basis, which is sufficient for performing the conditional test.
By numerical experiments we confirmed that our procedure works well in practice. We should mention that the sequential importance sampling method of Blitzstein and Diaconis [1] may work faster for the case of complete underlying graph.
If we allow multiple edges, then we do not need the Graver basis. A minimal Markov basis, which is often much smaller than the Graver basis, is sufficient for connectivity of Markov chains. Properties of Markov basis for the p 1 -model have been given in Petrović et al [16]. It is of interest to study properties of minimal Markov bases for undirected graphs, including the case of allowing self loops.
A Proofs of Lemmas in Section 3.2

A.1 Proof of Lemma 2
The contraction of the edge with its weight ±2 on W is possible from Lemma 1. We denote this edge by e = {i, j} as shown in Figure 19. Supposew is not primitive. Then there exists an proper subwalkw ′ ofw. If i / ∈ V (w ′ ),w ′ is also a proper subwalk of w, a contradiction to the primitiveness of w. Then i ∈ V (w ′ ). However, a proper subwalk of w is constructed by embedding e intoW ′ . Therefore,w is primitive.

A.2 Proof of Lemma 3
We consider the case that both positive and negative sides of i correspond to (a) in Figure  4 and relevant edges are labeled as shown in Figure 20. Suppose w 1 is neither primitive nor of length two. Then there exists a proper subwalk w ′ 1 of w 1 on W 1 . If e + / ∈ E(w ′ 1 ), w ′ is also a proper subwalk of w, a contradiction to the primitiveness of w. Then e + ∈ E(w ′ 1 ). Now w ′ 1 is expressed as follows: w ′ 1 = (e i 1 , e i 2 , . . . , e i k , e + , e i k+1 , . . . , e is ). Then an even closed walk on W (e i 1 , e i 2 , . . . , e i k , e + 1 , e − 1 , . . . , e − 2 , e + 2 , e i k+1 , . . . , e is ) is a proper subwalk of w. This contradicts the primitiveness of w. Therefore w 1 is primitive or of length two. The cases of (b) and (c) in Figure 4 are shown in the same way. Note that it is easy to confirm the possibility of contraction after the step 1 in the case (b) from Lemma 1 and then the primitiveness is guaranteed by Lemma 2. By the same argument, the case of w 2 is confirmed.

A.3 Proof of Lemma 4
Let e be the new edge appearing through the insertion to i as shown in Figure  w is not primitive. Then there exists a proper subwalkw ′ ofw. If e / ∈ E(w ′ ),w ′ is contained inW 1 orW 2 . Thenw ′ or its reverse becomes a proper subwalk of w. This contradicts the primitiveness of w. Hence e ∈ E(w ′ ). Then we can construct a proper subwalk of w by removing e fromw ′ and reversing the weights of edges belonging to E(w 1 ), a contradiction to the primitiveness of w. Therefore,w is primitive.

A.4 Proof of Lemma 5
Let i be the new vertex appearing through the binding. We consider the case that both positive and negative sides of i correspond to (a) in Figure 4 and relevant edges are labeled as shown in Figure 22. Other cases are shown in the same way. Suppose w is not primitive. Then there exists a proper subwalk w ′ of w. Here we choose a primitive walk as w ′ . If i / ∈ V (w ′ ), w ′ is also a proper subwalk of w 1 or w 2 . Then i ∈ V (w ′ ). This implies that all four edges connected to i appear in w ′ . Let us consider the separation of i to W ′ . Then the resulting two weighted graphs W ′ 1 , W ′ 2 are primitive from Lemma 3. Furthermore at least one of w ′ i (i = 1, 2) is a proper subwalk of w i , a contradiction to the primitiveness of w i . Therefore w is primitive.