3. Enumeration of Graphs with a Tree Skeleton and a Given Number of Vertices and Self-Loops
For two integers and , the aim of this section is to present a method to generate all rooted graphs in . The idea of our enumeration method is to generate a rooted graph by generating a canonical ordered graph of H. To achieve this, we define a canonical graph of a rooted graph and represent the canonical ordered graph with a sequence by using its ordered subgraphs. Finally, generate the canonical ordered graph of a rooted graph by using the sequence representation of the canonical ordered graph.
We next present a canonical representation of rooted graphs in
based on a generalization of the canonical representation of simple rooted trees with
n vertices introduced by Masui et al. [
23]. Recall that
denote a maximal set of all mutually rooted non-isomorphic simple rooted trees with
n vertices. Further, note that for
, it is necessary for a canonical representation of a rooted graph
to contain the information of vertices and self-loops in
H.
Let H be a rooted graph in and denote its root. Further, let be an ordered graph of H with a left-to-right ordering . For an integer and i-th vertex of K following the SDFS ordering on the rooted skeleton , let denote the ordered subgraph of K for convenience.
We introduce a canonical representation of
K by using the information of the number of vertices and self-loops in the ordered subgraphs of
K. For the vertices
of
K indexed by SDFS ordering on the rooted skeleton
, we define the sequence representation SR(
K) of
K to be a sequence of the size of each ordered subgraph
, integer
, of
K:
Examples of a rooted graph
, an ordered graph
of
H with a left-to-right ordering
, and vertices indexed in SDFS ordering and canonical representation SR
of
K are illustrated in
Figure 4a–c.
The next lemma states that the sequence representation of an ordered graph K is a concatenation of:
- (i)
a sequence of the size of the root-subgraphs of K in the left-to-right ordering and
- (ii)
the sequence representation of all root-subgraphs of K following the left-to-right ordering.
Lemma 1. Let K be an ordered graph with vertices and self-loops. For integers and , let denote the i-th root-subgraph of K in the left-to-right ordering. Then, it holds that: Proof. We know that in SDFS ordering, the root vertex is indexed by one, and the siblings of a vertex are indexed consecutively. This implies that the subsequence of the first d entries of SR is equal to . Furthermore, for an integer (resp., ), the SDFS ordering assigns the index to the descendants of the i-th vertex consecutively and greater than the indices of the children (resp., descendants) of the -th vertex. From this, it follows that for an integer (resp., ), the entries of SR appear consecutively after the entries of the sequence (resp., SR) in SR. This implies that for an integer , the subsequence of SR consisting of n consecutive entries starting from is actually SR. Hence, it follows that . □
For the ordered graph
K in
Figure 4c, SR
and deg
. This implies that
K has three root-subgraphs
,
m and
following the left-to-right ordering. From
Figure 4c, we have
, SR
, SR
, and SR
. Thus, we see that SR
.
We rephrase the recursion in Lemma 1 for a sequence of pairs in the following paragraph and claim that this recursion is a sufficient condition for a sequence of pairs to be the sequence representation of some ordered graph. We prove this claim in Theorem 1.
Let and be two integers and be a sequence of pairs of integers with and , integer . We say that the sequence M is -admissible if either or:
- (i)
there exists an integer such that with and
- (ii)
for each integer , the subsequence of M consisting of consecutive entries starting from is -admissible.
Theorem 1. Let and be two integers and be a sequence of pairs of integers with and , integer .
- (i)
Sequence M is the sequence representation SR of some ordered graph K with n vertices and Δ self-loops if and only if M is -admissible.
- (ii)
Whether M is admissible or not can be tested in time.
- (iii)
When M is -admissible, the ordered graph K with SR can be constructed in time.
Proof. For sequence M with an integer such that and , let denote the subsequence of M consisting of consecutive entries starting from . For an ordered graph K and integer , let denote the i-th root-subgraph of K in the left-to-right ordering.
- (i)
The if-part: Suppose that for some ordered graph K with n vertices and self-loops. If , then M is -admissible by the definition of admissibility. Let us assume that . Then, for , it holds that and . Further, by Lemma 1 for each integer , the subsequence of M is equal to SR. Thus, by recursively using Lemma 1 for SR, , we see that the sequence representation is -admissible. Hence, it follows that the sequence representation is -admissible.
The only-if part: We prove the converse of (i) by induction on n.
For , M is -admissible by the definition of admissibility. Note that M is an empty sequence in this case. Let K be an ordered graph with vertices and self-loops. Then, SR is an empty sequence, and hence, .
Suppose that the converse of (i) holds for any positive integer
ℓ. We show that the converse holds for the integer
. Let
M be
-admissible. Then, by the definition of admissibility, there exists an integer
such that
and
. This implies that for an integer
, we have
. Further, for each integer
, the subsequence
is
-admissible by the admissibility of
M. This and the inductive hypothesis that the converse of (i) holds for any integer
imply that for each integer
, there exists an ordered graph
H with
vertices and
self-loops such that
. Let
K denote the ordered graph with
vertices,
self-loops, deg
,
self-loops on the root
, and the
i-th root subgraph
of
K be the ordered subgraph
H such that
. Then, it immediately follows that:
This means that holds, since , showing that the converse holds for the integer .
Hence, by mathematical induction, the converse of (i) holds for any integer .
- (ii)
We prove this result by induction on n.
For , the sequence M is an empty sequence and is -admissible by the definition of admissibility. Therefore, it takes constant time to test admissibility in this case.
Suppose that for , , the admissibility of sequence M can be tested in time. We show that the statement (ii) holds for . To show if M is -admissible, we need to find an integer such that and . Such an integer d can be identified in time. Suppose that such an integer d exists for M. Then, for each integer , we next need to test if the subsequence is -admissible. Note that the size of the sequence is , . By , it holds that , . This and the inductive hypothesis imply that for an integer , the admissibility of the sequence can be tested in time. Thus, the time testing admissibility of M is , since .
Hence, by mathematical induction, the admissibility of a sequence M of size , can be tested in time.
- (iii)
We prove the claim in (iii) by induction on n.
For , M is -admissible and is the sequence representation of the ordered graph K with only one vertex and self-loops. This implies that K can be constructed in time.
Suppose that for , , the statement (iii) holds. We show that the statement (iii) holds for . Let M be an -admissible sequence. Then, there exists an integer , such that and . By (i), there exists an ordered graph K with n vertices and self-loops such that SR. Thus, it holds that . Further, by Lemma 1, for each integer , the i-th root-subgraph of K has vertices and self-loops. Observe that such an integer d exists uniquely due to the admissibility of M. This implies that can be obtained in time.
By SR and Lemma 1, for each integer , it holds that SR. Recall that the size of SR is n, which is equal to , . Further, by , it follows that , . Thus, by the inductive hypothesis for an integer , the subgraph can be constructed from in time. Since and , K can be constructed in time from M.
Hence, by mathematical induction, for integers and and an -admissible sequence M, the ordered graph K with SR can be constructed by M in time. □
Let be an -admissible sequence. Then, there exists an integer such that . Furthermore, by Theorem 1(i), there exists an ordered graph K with n vertices and self-loops such that deg and SR. Note that such an integer d and ordered graph K are unique. We call the integer d the root-degree of M and denote it by d. Moreover, for each integer , the subsequence of M consisting of consecutive entries starting from is equal to the sequence representation of the i-th root-subgraph of M. For an integer , we call such a subsequence of M the i-th root-subsequence of M and denote it by .
By Theorem 1(i), it follows that an ordered graph K with vertices and self-loops can be completely determined by SR. Thus, we define a canonical representation of a rooted graph as follows. For a rooted graph , we define the canonical representation to be an -admissible sequence M such that M is lexicographically maximum among all -admissible sequences that are the sequence representation of ordered graphs of H.
In
Figure 4d, we show the canonical representation
M of the rooted graph
illustrated in
Figure 4a. Further, we show the ordered graph
L such that SR
.
To generate all rooted graphs in , it is enough to generate the canonical representation of each rooted graph by Theorem 1(i). For two integers and , let denote the set of all -admissible sequences that are canonical representation of graphs in . Note that the empty sequence is the only sequence in . In the next lemma, we give a characterization of sequences in .
Lemma 2. Let and be two integers. Let be a sequence of integer pairs with an integer such that . For an integer , let denote the subsequence of M consisting of consecutive entries starting from . Then, if and only if the following hold:
- (i)
, and ;
- (ii)
, and for each integer such that , it holds that ; and
- (iii)
, and for each integer such that and , it holds that .
Proof. The if part: Let . Then, by the definition of admissibility, it holds that . Let H denote the ordered graph with n vertices and self-loops such that SR.
- (i)
By the admissibility of M, we have , . Furthermore, M is the canonical representation of H, and therefore, for the sequence representation of any ordered graph of H, it holds that . This eventually implies that .
- (ii)
By the admissibility of M, it holds that , . Moreover, for the sequence representation of any ordered graph of H such that , it holds that since M is the canonical representation of H. This implies that for each integer such that , it holds that .
- (iii)
We first prove that for each integer , it holds that .
For an integer , let denote the i-th root-subgraph of H following the left-to-right ordering. Then, by Lemma 1 for each integer , it holds that .
Suppose on the contrary that there exists an integer such . This means that there exists an ordered graph L that is rooted isomorphic to , and SR holds. Let denote the sequence obtained from M by replacing the subsequence with SR. Clearly, is -admissible, and holds by the construction of . Let denote the ordered graph obtained by replacing with L in H and preserving the ordering of the children of each vertex in L. Then, we see that is rooted isomorphic to H, and SR. This contradicts the fact that M is a sequence in . Hence, for each integer , it holds that .
Recall that M is the canonical representation of H. This implies that for the sequence representation of any ordered graph of H such that , it holds that . This implies that for each integer such that and , we have .
The only-if part: Let M satisfy (i), (ii), and (iii). We show that . To prove this, we show that M is a canonical representation of some graph in .
By (i) and (ii), we have , and . Furthermore, for each integer , the sequence is -admissible, since by (iii). This implies that M is -admissible.
By Theorem 1(i), there exists a unique ordered graph such that SR for some . This implies that deg, and for each integer , the i-th root subgraph of K has vertices and self-loops. This implies that any ordered graph L that is rooted isomorphic to K has x vertices and y self-loops such that for some .
The condition in (i) implies that for the sequence representation of any ordered graph that is rooted isomorphic to K, it holds that since . For the condition for each integer such that , it holds that in (ii) implies that for the sequence representation of any ordered graph that is rooted isomorphic to K such that , it holds that since . Finally, for the condition for each integer such that and , it holds that in (iii) implies that for the sequence representation of any ordered graph that is rooted isomorphic to K and , it holds that since . This eventually implies that M is the canonical representation of H from which it follows that . □
We next give the structure of the sequences that are lexicographically minimum and maximum among all sequences in .
Lemma 3. Let and be two integers.
- (i)
The sequences and each of length are lexicographically minimum and maximum among all sequences in , respectively.
- (ii)
Whether a sequence in is lexicographically minimum or maximum among all sequences in can be tested in .
Proof. - (i)
It is easy to observe that the sequence N is -admissible. Furthermore, for two integers and , the ranges of the first and the second entries in any sequence in are and , respectively. This implies that the sequence N is lexicographically minimum among all the sequences in . Moreover, the sequence is admissible and lexicographically maximum among all the sequences in . From this, it follows that the sequence is admissible and lexicographically maximum among all the sequences in . Thus, by using this inductive argument, we can conclude that the sequence M is admissible and lexicographically maximum among all the sequences in .
- (ii)
We know that a sequence S in is of length , and therefore, by using a for-loop of size and (i), we can test if S is lexicographically minimum or maximum among all sequences in in time.
□
Let and denote the lexicographically minimum and maximum sequences among all sequences in , respectively.
For a sequence such that , we define the predecessor P of M to be the sequence that is lexicographically maximum among all sequences that are lexicographically smaller than M, i.e., there does not exist a sequence such that holds.
For a sequence , we next give the structure of the predecessor P, if it exists, of M.
Theorem 2. Let and be two integers and be a sequence in . Let d denote the root-degree of M. Then, for the predecessor P, if it exists, we have:
- (a)
If and , , then P does not exist.
- (b)
If for some , and , . Then, for the largest integer such that , it holds that d(P, , , , , , and P, .
- (c)
If for some , , . For the largest integer such that , let , if , and if and . Then, it holds that d(P, , , ; for , we have , , , and P, .
- (d)
Otherwise if , , for some . For the largest integer such that , let . Then, it holds that d(P, , P, , P, and P, .
Proof. - (a)
If and , , then it holds that . Thus, by Lemma 3(i), it holds that , and therefore, P does not exist.
- (b)
If for some , and , , then M is lexicographically minimum among all those sequences for which it holds that . Further, for some implies that P exists. By the definition of a predecessor, observe that P is lexicographically maximum among all those sequences for which it holds that . This implies that for each such sequence S, it holds that either or and P. The former implies that , , while the latter implies that P, . Further, the sequence satisfies Lemma 2(i), and there does not exist a sequence L that satisfies Lemma 2(i) such that holds by the definition of P. This and the definition of k imply that ; the sequence is a non-decreasing sequence, and there there does not exist a sequence such that and for which it holds that holds. This eventually implies that d(P, , and . Thus, by the definition of k, we have , and therefore, we have the required result.
- (c)
If for some and , , then M is lexicographically minimum among all those sequences for which it holds that . Since for some , therefore P exists and is lexicographically maximum among all those sequences for which it holds that . This implies that , and therefore, we have . Furthermore, and P. This implies that for each integer , it holds that P, and the sequence is lexicographically minimum among all those sequences that satisfy Lemma 2(ii) and are lexicographically smaller than . This and the definition of k imply that ; by Lemma 2(ii), the sequence is a non-decreasing sequence, and there does not exist a sequence such that and for which it holds that . This eventually implies that and for . This and the minimality of imply that and .
- (d)
The conditions of this case imply that M is not the lexicographically minimum among all those sequences for which it holds that . This implies that for P, it holds that d(P and , and there does not exist a sequence for which it holds that and . This implies that for each such sequence S, it holds that . This and the definition of k imply that P, . Furthermore, by Lemma 2(iii) and the definition of p, it holds that P, , and there does not exist a sequence such that and , for which it holds that . Since , therefore P is a lexicographically minimum sequence in for which it holds that P. This implies that P. Further, by the minimality of P, P, .
Finally, one can easily verify that the sequence obtained in each of the above cases satisfies Lemma 2(i)–(iii) by construction, and hence, is an element of that is P, which completes the proof. □
Lemma 4. Let and be two integers and M be a sequence in . Then, the predecessor P, if it exits, can be computed in time and space.
The proof of Lemma 4 follows from Algorithm 1 and Lemma 5.
We next present Algorithm 1 to compute the predecessor based on Theorem 2. In this algorithm, for a sequence
with root-degree
d and integer
, the variable
stores the
i-th root-subsequence
of
M, and the variable P
stores the predecessor, if it exists, of
M.
Algorithm 1 Computing the predecessor of an admissible sequence. |
Input: Two integers and and an -admissible sequence . |
Output: The predecessor M if it exists; The predecessor of M does not exist otherwise. |
1: | The root-degree of M; |
2: | if If and , then |
3: | Output The predecessor of M does not exist /* Theorem 2(a) */ |
4: | else/* The predecessor of M exists by Theorem 2*/ |
5: | P |
6: | if for some , and and , then |
| /* Theorem 2(b) */ |
7: | ; ; /* The root-degree |
| d of */ |
8: | ; ; ; ; |
| ; P, . |
9: | else if for some and , then |
| /* Theorem 2(c) */ |
10: | ; ; if ; |
| if ; |
11: | ; |
12: | if then |
13: | ; ; |
| |
14: | end if; |
15: | P, |
16: | else/* If and , for some */ |
| /* Theorem 2(d) */ |
17: | ; |
18: | ; |
| ; P, ; |
19: | P Algorithm 1; |
20: | P; P, |
21: | end if |
22: | Output P as the predecessor of M; |
23: | end if. |
| |
Lemma 5. For two integers and and an -admissible sequence M, Algorithm 1 outputs the predecessor P, if it exits, in time and space.
Proof. Correctness: The correctness of Algorithm 1 immediately follows from Theorem 2.
Complexity analysis: By the definition of the root-degree, we can compute d at Line 3 in time.
We can test if and hold for some in time. Similarly, we can test if , for some in time, since the length of is , and . Hence, we can test the conditions at Lines 2, 6, 9, and 16 in time. This implies that we can check if the predecessor of M exists in time. We next discuss the time complexity of computing the predecessor in each of the cases at Lines 6, 9, and 16.
When the conditions at Line 6 hold, then k and h can be computed in time, since and . This implies that can be computed in time. Furthermore, we can compute L in time by Lemma 3(i). Recall that for P, it holds that . Thus, P can be computed in time.
When the conditions at Line 9 hold, then k, , and t can be computed in time by there definitions. Thus, can be computed from Line 11 to Line 14 in time. Furthermore, L can be computed at Line 15 in time, as discussed above. This implies that P can be computed in time in this case.
Finally, when the conditions at Line 16 hold, then once again, we can compute k in time, since . Further, p can be computed in time by the definition of p. However, P can be obtained by recursively running Algorithm 1 on and . Note that this operation is repeated at most the length of the sequence P, which is in this case, and hence, P can be computed in time. Once again, the computation at Line 20 can be done in time since it holds that . Hence, P can be computed in time.
Note that the space is sufficient to store P, if it exists, which completes the proof. □
Note that we can generate all rooted graphs in by generating their canonical representation by repeatedly using Algorithm 1 starting from the lexicographically maximum sequence L in in time per graph and space in total.
Theorem 3. Let and be two integers. Then, all mutually non-isomorphic graphs with n vertices, Δ self-loops, and a tree skeleton can be generated in time per graph and space in total.
Proof. A tree can be viewed as a rooted tree by considering its centroid as the root [
24]. We know that when
n is odd, then there are only trees with unicentroids; however, when
n is even, then there are trees with unicentroids.
By the definition of a unicentroid, all mutually non-isomorphic graphs with n vertices, self-loops, and a tree skeleton with a unicentroid can be enumerated by generating all graphs H in such that each root subgraph of H has at most number of vertices, i.e., by generating all sequences such that , . Let S denote the sequence that is lexicographically maximum among all those sequences in that represent a tree with a unicentroid. When n is even, then it holds that . Recall that is an empty sequence, and therefore, we have . However, when n is odd, then it holds that . Hence, we can generate all sequences in that represent a graph such that the skeleton of H has a unicentroid by repeatedly using Theorem 2 starting from the sequence S. This implies that we can generate all such sequences in time per sequence and space by using Algorithm 1.
When n is even, then all mutually non-isomorphic graphs with n vertices, self-loops, and a tree skeleton with a bicentroid can be enumerated by generating all sequences such that the root-degree d, and with . In such a case, the sequences and are lexicographically maximum and minimum, respectively, among all those sequences in that represent a graph with a bicentroid. Let be a sequence that represents a graph such that the skeleton of H has a bicentroid and . When , , then it holds that P. However, in the case otherwise, i.e., when , for some , then P can be generated by using Theorem 2(d). Clearly, in both of these cases, P can be generated in time and space. This eventually implies that all sequences that represent a graph in with a bicentroid can be generated in time per sequence and space by repeatedly computing the predecessor of sequences M as described above starting from until .
Hence, we can generate all non-isomorphic graphs with n vertices, self-loops, and a tree skeleton with a unicentroid or bicentroid in time per graph and space in total, which completes that proof. □