1 Introduction

The complexity of a number of NP-class combinatorial problems can be solved using only a polynomial number of multivector operations in a 2n-dimensional algebra generated by n mutually commuting null-squares, as we have shown in [5]. In particular, by defining a “nilpotent adjacency matrix” associated with a finite graph on n vertices, the problem of enumerating cycles of length k requires multivector operations in the algebra, where α≤3 denotes the exponent associated with matrix multiplication. While α<3 for ordinary matrix multiplication (see [2]), such algorithmic speedups do not necessarily hold for matrices whose elements are multivectors.

In this chapter, the computational complexity of enumerating cycles of arbitrary length in graphs is studied in greater detail by counting algebraic operations at the basis blade level of the algebra as opposed to the multivector level. For practical reference, the theoretical complexity of this approach is compared to that of the algorithms of Bax and Tarjan. Moreover, experimental comparisons using Mathematica illustrate practical advantages of the nilpotent adjacency matrix approach, particularly in the case of sparse graphs.

The algebra used in the construction can be regarded as a commutative subalgebra of the exterior algebra \(\bigwedge\mathbb{R}^{2n}\). Its generators commute and square to zero so that linear combinations of the generators are nilpotent. This nilpotent nature is the key to combinatorial applications.

All Mathematica examples were computed on a 2.4 GHz MacBook Pro with 4 GB of 667 MHz DDR2 SDRAM running Mathematica 6 for MAC OS X with the Combinatorica package. Cycle enumeration is accomplished using the nilpotent adjacency matrix approach, Bax’s approach, and the HamiltonianCycle procedure found in the Mathematica package Combinatorica. Time plots comparing the three approaches are included. Mathematica code used to generate examples can be found online through the second-named author’s web page, http://www.siue.edu/~sstaple.

2 Essential Background

A graph G=(V,E) is a collection of vertices V and a set E of unordered pairs of vertices called edges. Two vertices v i ,v j V are said to be adjacent if there exists an edge e ij ={v i ,v j }∈E. In this case, the vertices v i and v j are said to be incident with e ij .

A k-walk {v 0,…,v k } in a graph G is a sequence of vertices in G with initial vertex v 0 and terminal vertex v k such that there exists an edge (v j ,v j+1)∈E for each 0≤jk−1. A k-walk contains k edges. A k-path is a k-walk in which no vertex appears more than once. A closed k-walk is a k-walk whose initial vertex is also its terminal vertex. A k-cycle is a closed k-path with v 0=v k .

The diameter of a graph G is defined to be the length of the longest path in G. The girth and circumference of a graph are defined to be the lengths of the graph’s shortest and longest cycles, respectively. If the graph has no cycles, its girth and circumference are defined to be ∞.

If G=(V,E) is a graph, A is its adjacency matrix, and SV, define the modified adjacency matrix A S by

Theorem 12.1

(Bax)

Each main diagonal element of

contains the number of Hamiltonian cycles in G if |V|>0.

Bax’s approach to cycle enumeration uses powers of a graph’s adjacency matrix with the principle of inclusion–exclusion to count all Hamiltonian cycles in time and poly(n) storage [1]. Enumerating only those cycles of length k is accomplished by applying Bax’s algorithm to all k-vertex subgraphs. For fixed k, this is since \(\binom{n}{k}\le n^{k}\) for all nk. For k increasing with n, the complexity remains , which can be verified by a rearrangement of Bax’s formula (cf. Exercise 12.1).

Tarjan’s algorithm (based on pruning with look-ahead) enumerates all cycles in a graph on n vertices with time complexity when applied to a graph with C cycles [7]. The storage complexity is , where S is the sum of the lengths of all cycles. Note that the number of cycles on a k-vertex subgraph is potentially of order k! while the number of subgraphs supporting such cycles is of order \(\binom{n}{k}\).

A convenient and practical Tarjan-type implementation is the HamiltonianCycle procedure found in the Mathematica package Combinatorica. The algorithm uses backtracking and look-ahead to enumerate all Hamiltonian cycles in a graph on n vertices. The implementation utilized for the examples in this paper enumerates cycles of length k in an n-vertex graph G by applying HamiltonianCycle to all k-vertex subgraphs of G. Implementations of this Tarjan-like approach are referred to henceforth as “CombiTarjan.” Tarjan’s algorithm actually lists cycles, which can result in space complexity.

3 Technical Considerations

Fix positive integer n and let γ={e i :1≤i≤2n} be an orthonormal basis for ℝ2n. Note that the pair (γ,∧) generates a nonabelian semigroup A of order 22n. Let

(12.1)

and note that the pair (Z,∧) generates an abelian subsemigroup ℝΨ n of A. It should be clear that all elements of Ψ n square to zero.

Definition 12.1

Let ℝΨ n denote the real abelian semigroup algebra of Ψ n . For convenience, the generators of Ψ n are rewritten as ζ i =e 2i−1e 2i , and henceforth the wedge operator is implicit.

Remark 12.1

The algebra ℝΨ n is isomorphic to the n-particle zeon algebra appearing in the earlier work [5].

It is evident that the dimension of ℝΨ n is 2n and that an arbitrary element u∈ℝΨ n can be expanded as

(12.2)

where I∈2[n] is a subset of [n]={1,2,…,n} used as a multiindex, u I ∈ℝ, and ζ I =∏ ιI ζ ι .

A canonical basis element ζ I is referred to as a blade. The number of elements in the multiindex I is referred to as the grade of the blade ζ I .

The scalar sum evaluation of an element u∈ℝΨ n , denoted 〈〈u〉〉, is defined by

(12.3)

Definition 12.2

Let G be a graph on n vertices, either simple or directed with no multiple edges, and let {ζ i }, 1≤in, denote the nilpotent generators of ℝΨ n . Define the nilpotent adjacency matrix associated with G by

(12.4)

Recalling Dirac notation, the ith row of will be conveniently denoted by , while the jth column will be denoted by .

A graph-theoretic interpretation of the nilpotent adjacency matrix can be stated thusly: if and only if one can reach v j from v i in one step. Moreover, that “step” algebraically corresponds to multiplication by the null-square generator ζ j . Extending by induction, nonzero terms of correspond to k-step walks from v i to v j in which each walk is “accomplished” in the algebra by computing a product of null-square generators. The null-square property then naturally “sieves out” walks on distinct generators, i.e., self-avoiding walks. This is all made precise in the next theorem.

Theorem 12.2

Let be the nilpotent adjacency matrix of an n-vertex graph G. For any k>1 and 1≤i,jn,

(12.5)

where ω I denotes the number of k-step walks from v i to v j visiting each vertex in I exactly once when initial vertex v i I, and revisiting v i exactly once when v i I. In particular, for any k≥3 and 1≤in,

(12.6)

where ω I denotes the number of k-cycles on vertex set I based at v i I.

Proof

Because the generators of ℝΨ n square to zero, a straightforward inductive argument shows that the nonzero terms of are multivectors corresponding to two types of k-walks from v i to v j : self-avoiding walks (i.e., walks with no repeated vertices) and walks in which v i is repeated exactly once at some step but are otherwise self-avoiding. Walks of the second type are zeroed in the kth step when the walk is closed. Hence, terms of represent the collection of k-cycles based at v i . □

In light of this theorem, the name “nilpotent adjacency matrix” is justified by the following corollary.

Corollary 12.1

Let be the nilpotent adjacency matrix of a simple graph on n vertices. For any positive integer kn, the entries of are homogeneous elements of grade k inΨ n . Moreover, for all k>n.

Another immediate corollary is that

(12.7)

since each k-cycle appears with k choices of base point along the main diagonal of .

In earlier work (see [5, 6]), the authors defined complexity in terms of the number of multivector operations in , or “ops”, required. In contrast, this chapter work considers complexity at the level of the basis blades of .

Definition 12.3

A blade operation in ℝΨ n is defined as computing the sum or product of two basis blades. In particular, for multiindices I and J, each of the following computations is regarded as a blade operation:

(12.8)
(12.9)

Recalling the correlation between subsets of [n] and bit strings of length n, each basis blade ζ I is uniquely associated with a binary string \(\underline{I}\). The cost of a basis blade multiplication in ℝΨ n is then equal to that of computing first the bitwise AND and then the bitwise OR of two n-bit words, which is known to be . Summing a pair of basis blades is similarly .

Given arbitrary elements u,v∈ℝΨ n and letting ν u and ν v denote the respective numbers of nonzero coefficients in the canonical expansions of u and v, the number of blade multiplications required to compute uv is then ν u ν b . The number of blade additions is similarly . Taking the costs of the blade operations themselves into consideration, the complexity of expanding the product uv is thus seen to be .

The Mathematica implementation of ℝΨ n used in the examples contained herein is based on subset operations rather than binary representations of subsets and bit operations. The additional overhead is offset by the relatively low dimensions of the examples.

4 Theoretical Complexity

Lemma 12.1

Enumerating cycles in a simple graph on n vertices using nilpotent adjacency matrix methods has storage complexity .

Proof

The nilpotent matrix method requires construction of n×n matrices whose entries are elements of a 2n-dimensional algebra; i.e., in the worst case, coefficients must be associated with each matrix entry. Consequently, the space complexity is . □

Theorem 12.3

The worst-case time complexity of enumerating cycles of arbitrary length in a graph on n vertices using the nilpotent adjacency matrix method is .

Proof

In light of Theorem 12.2, for any kn, computing requires computing

(12.10)

for all 1≤i,jn. Entries of are homogeneous grade-(k−1) elements of . Moreover, terms in the canonical expansion of must be indexed by subsets containing v , while in all cases, is either 0 or \(\zeta_{v_{j}}\).

Thus, the maximum number of blade multiplications performed in computing the product is \(\binom {n-1}{k-2}\) for each 1≤n.

Computing the product then requires at most \(n^{\alpha}\binom{n-1}{k-2}\) blade multiplications. Applying this result recursively, computing requires

(12.11)

blade multiplications. Since each blade multiplication is of complexity , the result follows. □

Note that an immediate consequence of the theorem is that the worst-case complexity of computing the girth and circumference of a graph on n vertices is also .

Recalling that the diameter of a graph is defined as the length of the graph’s longest path, another corollary is obtained.

Corollary 12.2

The worst-case time complexity of computing the diameter of a graph on n vertices using the nilpotent adjacency matrix method is .

Proof

Letting Δ be the n×n diagonal matrix whose main diagonal entries are Δ ii i , it can be shown that the off-diagonal entries of are homogeneous grade-(k+1) multivectors corresponding to k-paths in the graph associated with . The effect of left multiplication by Δ is to account for the initial vertex of any walk. Hence, the diameter of the graph is given by the largest positive integer k for which is not the zero matrix. □

Note that the complexity of computing may vary depending on various methods of computing powers. The iterated method requires k−1 matrix products to compute

(12.12)

Given the binary representation of positive integer k, the successive squares method requires ⌊log 2 k⌋ matrix products and matrix sums to compute. In particular, letting \(\underline {k}\) be a set of nonnegative integers such that \(k=\sum_{\ell\in \underline{k}}2^{\ell}\), then

(12.13)

While the successive squares method is generally more efficient than the iterated method, the application to nilpotent adjacency matrices is not straightforward. All discussion is henceforth restricted to the iterated method.

Example 12.1

Computation times in seconds are given for enumerating ⌊n/2⌋-cycles in randomly generated n-vertex graphs in Fig. 12.1. The experimental results illustrate practical advantages of the nilpotent adjacency matrix approach.

Fig. 12.1
figure 1

Times (in seconds) required to enumerate ⌊n/2⌋-cycles in randomly generated n-vertex graphs having equiprobable edges (p=0.25)

4.1 Average-Case Complexity in “Suitably Sparse” Graphs

Discussion now turns to the average-case complexity of cycle enumeration in homogeneous random graphs. A homogeneous random graph is a graph on n vertices with independent equiprobable edges of probability p. That is, each pair of vertices in the graph has equal probability p of being adjacent.

The next example illustrates the role of graph sparseness in the algorithmic comparisons. For fixed values of n and k, the time required to count k-cycles in an n-vertex graph depends on graph density for the CombiTarjan and nilpotent adjacency matrix methods, but is essentially constant for Bax’s algorithm.

Example 12.2

Mean run times over 20 trials of counting ⌊n/2⌋-cycles in simple graphs are compared in Fig. 12.2 (top). In Fig. 12.2 (bottom), the number of vertices is fixed at 10, and edge existence probability varies from p=0.1 to p=0.5.

Fig. 12.2
figure 2figure 2

Top: Mean run times over 20 trials enumerating ⌊n/2⌋-cycles in randomly generated n-vertex graphs having equiprobable edges (p=0.25). Bottom: 20-Run mean run times of counting 5-cycles in 10-vertex graphs with edge probabilities running from p=0.1 to p=0.5. Plotmarkers: B—Bax, C—CombiTarjan, •—ℝΨ n

The next theorem describes a class of random graphs for which the nilpotent adjacency matrix method is more efficient than .

Theorem 12.4

Let q∈(0,1) be fixed. Let n be a positive integer, and let 3≤kn. Let be a homogeneous random graph on n vertices with independent equiprobable edges of probability \(p\le \frac{q}{k-1}\). Then, the average-case complexity of enumerating cycles of length less than or equal to k in using the nilpotent adjacency matrix method is .

Proof

As in the proof of Theorem 12.3, the result is obtained by considering the number of nonzero coefficients in . To consider the average-case complexity, we consider expected numbers of nonzero coefficients according to the probability model indicated. In particular, the average number of blade multiplications performed in computing

is the product of the expected numbers of nonzero coefficients in the canonical expansions of and .

Let be a homogeneous random graph on n vertices with independent equiprobable edges of nonzero probability \(p\le\frac{q}{k-1}\) for fixed q∈(0,1).

Claim

Let n≥3, and let 2≤kn. For any 1≤i,jn, the expected number of nonzero coefficients in the canonical expansion of satisfies the following inequality:

(12.14)

Proof of Claim

By Theorem 12.2, the expected number of nonzero coefficients in the canonical expansion of is equal to the expected number of k-vertex subsets IV such that there exists a k-step walk from v i to v j I visiting each vertex of I exactly once when v i I and revisiting v i exactly once when v i I.

The special case k=2 is treated first. The expected number of nonzero coefficients in the canonical expansion of is equal to the expected degree of v i when i=j and equal to the expected number of two step walks on distinct vertices v i v v j when ij; i.e.,

(12.15)

The desired inequality for k=2 is then established by observing that p<q, whence

(12.16)

Turning now to the more general case 2<kn, the expected number of vertex sets I on which k-walks v i v j exist with no repeated vertices except possibly v i at an intermediate step is determined by partitioning the collection of these walks into two classes: (i) walks on k edges and (ii) walks on k−1 edges (in which case, vertex v i is revisited on the second step).

Unless otherwise indicated, k-walks will refer only to those walks w:v i v j with no revisited vertex except possibly v i exactly once at an intermediate step.

Note that the total number of k-walks w:v i v j in K n revisiting no vertex except possibly v i at an intermediate step is given by

(12.17)

since these walks are specified by ordered k-tuples of vertices with v j in the kth position. Hence, k−1 intermediate vertices visited are chosen from V∖{v j } with (k−1)! possible permutations.

Denote as Class I those walks on k independent equiprobable edges. Class I walks either revisit no vertices or may revisit v i at some step other than the second step. Let W 1 denote the total number of these walks in K n . Denote as Class II those walks on k−1 independent equiprobable edges. Class II walks revisit vertex v i at the second step. Let W 2 denote the number of Class II walks in K n .

Note first that W=W 1+W 2. Given a homogeneous random graph , it is now evident that

(12.18)

When a collection of k-walks v i v j exists on k independent equiprobable edges,

(12.19)

Similarly,

(12.20)

Together, (12.18), (12.19), and (12.20) give

(12.21)

The expected number of vertex subsets supporting these walks therefore satisfies the following inequality:

(12.22)

With the assumption \(p\le\frac{q}{k-1}\) for fixed q>0 and substitution of W from (12.18), one thereby obtains

(12.23)

This completes the proof of the claim. □

By considering the expected number of nonzero coefficients in the canonical expansion of for 3≤kn, it is now evident that the expected number of blade multiplications performed in computing is bounded above by

(12.24)

Hence, the expected number of blade multiplications performed in computing the matrix product is bounded above by \(n^{2} q^{k-1}\binom{n}{k-1}\). Applying this result recursively, the average number of blade multiplications required to compute is found to be bounded above by \(n^{2}\sum_{\ell=1}^{k-1}q^{\ell}\binom{n}{\ell}\).

Observing that

(12.25)

cycle enumeration is of average-case complexity in terms of blade operations required. Recalling the complexity of blade operations thus completes the proof. □

Example 12.3

Average computation times (over 200 trials) of enumerating cycles of length k∈{3,…,max ({3,n/2})} in homogeneous random graphs satisfying the conditions of Theorem 12.4 with constant q=0.7 are depicted in Fig. 12.3 (top). Also plotted is the curve y=cn 3(1+q)n with c=2.4862⋅10−9 determined by least squares.

Fig. 12.3
figure 3figure 3

Top: Counting cycles of random length k∈{3,…,max ({3,n/2})} in n-vertex graphs with edge probability p=q/(k−1). The continuous curve is y=cn 3(1+q)n, where q=0.7 and c=2.4862⋅10−9, obtained by least squares method. Bottom: Counting cycles of length k∈{3,…,max (3,⌊n/2⌋)} in n-vertex graphs with edge probability p=q/(k−1), where q=0.7. Plotmarkers: B—Bax, C—CombiTarjan, •—ℝΨ n

Example 12.4

A comparison of average computation times (over 20 trials) for the three methods of enumerating cycles of length k∈{3,…,max ({3,n/2})} in homogeneous random graphs satisfying the conditions of Theorem 12.4 with constant q=0.7 are depicted in Fig. 12.3 (bottom).

As the next theorem shows, the fixed cycle length case is very well-behaved in terms of complexity.

Theorem 12.5

For fixed k∈ℕ, the worst-case complexity of enumerating k-cycles in an n-vertex graph by the nilpotent adjacency matrix method is .

Proof

The case k=3 is clear from the special case in the proof of Theorem 12.4. When k>3, the maximum number of nonzero coefficients in the canonical expansion of is \(\binom{n-1}{k-2}\). Asymptotically, . Hence, computing requires computing at most

(12.26)

blade products. □

5 Implementation Notes

In general, coding the geometric product (or any noncommutative operation) in Mathematica is most reliably done using one of Mathematica’s undefined symbols. However, since the product in ℝΨ n is commutative, a much more efficient implementation is possible. The nilpotent adjacency matrix approach was implemented herein by overloading the Times operator of Mathematica to facilitate multiplication of blades from ℝΨ n . Figure 12.4 details Mathematica code for implementing the multiplication in ℝΨ n .

Fig. 12.4
figure 4

Mathematica code defining ℝΨ n multiplication by overloading the Times operator

Once the ℝΨ n multiplication is implemented, the algorithm for counting cycles is very straightforward. The corresponding code is seen in Fig. 12.5.

Fig. 12.5
figure 5

Mathematica code for counting k-cycles via nilpotent adjacency matrix method

Bax’s algorithm is implemented using the formula obtained in Exercise 12.1. Counting all k-cycles in a graph G having adjacency matrix A is accomplished by computing the quantity

(12.27)

The corresponding code is seen in Fig. 12.6.

Fig. 12.6
figure 6

Mathematica code for counting k-cycles via algorithm of Bax

The CombiTarjan method was implemented by first extracting all k-vertex subgraphs and summing recovered numbers of Hamiltonian cycles on them. The code appears in Fig. 12.7.

Fig. 12.7
figure 7

Mathematica implementation of “CombiTarjan” approach

The comparisons seen in Fig. 12.1, Fig. 12.3, and Fig. 12.2 were generated as follows: For any given trial, a random simple graph is first generated by constructing a random symmetric binary matrix. The corresponding nilpotent adjacency matrix is then constructed.

The Mathematica system cache was cleared before counting by each method. The system time was stored in a variable, the appropriate method was called, and the subsequent system time was stored in another variable. Relevant data were then appended to a table. Test points were incorporated after each method to ensure that all three methods were returning the same results.

6 Conclusion

Leslie Valiant first defined the complexity class ♯P when dealing with the complexity of counting solutions to NP decision problems [8]. The problem of deciding whether or not a graph contains a Hamiltonian cycle is one such decision problem [3].

Generally, the problem of counting cycles in finite graphs is known to be ♯P-complete, and no polynomial-time algorithm is known to exist for solving this problem. Algorithms with time complexity and space complexity are known to exist (see [1, 4]).

The time complexity of Tarjan’s algorithm is proportional to the number of cycles contained in the graph. Unlike Bax’s algorithm, which simply counts cycles, Tarjan’s algorithm actually lists the cycles. As a result, the storage and time complexity of Tarjan’s algorithm are potentially factorial rather than exponential.

The nilpotent adjacency matrix method, like Bax’s algorithm, counts cycles without listing them, providing an advantage over Tarjan’s approach in terms of storage-complexity (assuming the counting of cycles is all that is required). Unlike Bax’s algorithm, which has time complexity in all cases, the average-case complexity is significantly less when dealing with “suitably-sparse” graphs. The storage complexity of the nilpotent adjacency matrix approach lies between Bax and Tarjan, since it is proportional to the number of vertex subsets supporting cycles in the graph.

As illustrated by the experimental results, the nilpotent adjacency matrix method often has computational advantages over other classical algorithms. Much work remains to be done in characterizing theoretical complexity on various families of graphs.

7 Exercises

12.1

Let A be the adjacency matrix of a simple graph G on n vertices as seen in Theorem 12.1. Show that for any vertex v i in G and positive integer kn,

(12.28)

12.2

For positive integer n, let Z={e 2i−1e 2i :1≤in} as in (12.1). Prove that the pair (Z,∧) forms an abelian semigroup.

12.3

Let u∈ℝΨ n be an element of the form u=a 1ζ1+a 2ζ2, where a 1 and a 2 are nonzero scalars. Show that u 2≠0 and u 3=0.

12.4

Suppose that n and k are positive integers satisfying 1<kn and u∈ℝΨ n is an element of the form \(u=\sum _{i=1}^{k} a_{i}\zeta_{i}\) where a i ≠0 for i=1,…,k. Prove the following:

  1. i.

    u k=k!a 1a k ζ{1,…,k}

  2. ii.

    u m=0 for all m>k.

12.5

Construct a nilpotent adjacency matrix for the graph appearing in Fig. 12.8 and count the 7-cycles contained therein by computing . Verify your result by hand as well as by applying the Bax and CombiTarjan algorithms.

Fig. 12.8
figure 8

A simple graph on 14 vertices