Finding and Counting Given Length Cycles 1. multiplication. Even cycles in undirected graphs can be found even faster. A C 4k 2 in an undirected

Similar documents
Algorithmica ~:) 1997 Springer-Verlag New York Inc.

Detecting short directed cycles using rectangular matrix multiplication and dynamic programming

Independent Transversals in r-partite Graphs

Packing triangles in regular tournaments

On the hardness of losing width

On the hardness of losing width

Finding a Heaviest Triangle is not Harder than Matrix Multiplication

Testing Equality in Communication Graphs

Properly colored Hamilton cycles in edge colored complete graphs

On the number of cycles in a graph with restricted cycle lengths

The concentration of the chromatic number of random graphs

Decomposing oriented graphs into transitive tournaments

Acyclic subgraphs with high chromatic number

Lecture 5: January 30

Asymptotically optimal induced universal graphs

The Algorithmic Aspects of the Regularity Lemma

T -choosability in graphs

Graph coloring, perfect graphs

Nowhere 0 mod p dominating sets in multigraphs

Classical Complexity and Fixed-Parameter Tractability of Simultaneous Consecutive Ones Submatrix & Editing Problems

Group Colorability of Graphs

Induced subgraphs of prescribed size

1 Matchings in Non-Bipartite Graphs

On zero-sum partitions and anti-magic trees

Independence numbers of locally sparse graphs and a Ramsey type problem

Packing and decomposition of graphs with trees

Approximation algorithms for cycle packing problems

Probabilistic Proofs of Existence of Rare Events. Noga Alon

A Separator Theorem for Graphs with an Excluded Minor and its Applications

Tree Decomposition of Graphs

Graphs with large maximum degree containing no odd cycles of a given length

k-degenerate Graphs Allan Bickle Date Western Michigan University

On Some Three-Color Ramsey Numbers for Paths

Approximation Algorithms for Asymmetric TSP by Decomposing Directed Regular Multigraphs

ON THE NUMBERS OF CUT-VERTICES AND END-BLOCKS IN 4-REGULAR GRAPHS

Asymptotically optimal induced universal graphs

The Turán number of sparse spanning graphs

Applications of the Lopsided Lovász Local Lemma Regarding Hypergraphs

Bounds on the generalised acyclic chromatic numbers of bounded degree graphs

arxiv: v1 [math.co] 13 May 2016

Size and degree anti-ramsey numbers

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

4 CONNECTED PROJECTIVE-PLANAR GRAPHS ARE HAMILTONIAN. Robin Thomas* Xingxing Yu**

Chain Minors are FPT. B Marcin Kamiński. 1 Introduction. Jarosław Błasiok 1 Marcin Kamiński 1

CIRCULAR CHROMATIC NUMBER AND GRAPH MINORS. Xuding Zhu 1. INTRODUCTION

The Interlace Polynomial of Graphs at 1

The number of edge colorings with no monochromatic cliques

The complexity of acyclic subhypergraph problems

Graphs with Large Variance

Cycles with consecutive odd lengths

Hardness of the Covering Radius Problem on Lattices

Bipartite Subgraphs of Integer Weighted Graphs

4-coloring P 6 -free graphs with no induced 5-cycles

Partial cubes: structures, characterizations, and constructions

Constructive bounds for a Ramsey-type problem

On the Exponent of the All Pairs Shortest Path Problem

Avoider-Enforcer games played on edge disjoint hypergraphs

Out-colourings of Digraphs

Almost Cross-Intersecting and Almost Cross-Sperner Pairs offamilies of Sets

On shredders and vertex connectivity augmentation

Decompositions of graphs into cycles with chords

Choosability and fractional chromatic numbers

Packing and Covering Dense Graphs

Perfect matchings in highly cyclically connected regular graphs

arxiv: v1 [math.co] 2 Dec 2013

ACO Comprehensive Exam October 14 and 15, 2013

Lower Bounds for Testing Bipartiteness in Dense Graphs

Multiply Erdős-Ko-Rado Theorem

THE MAXIMAL SUBGROUPS AND THE COMPLEXITY OF THE FLOW SEMIGROUP OF FINITE (DI)GRAPHS

arxiv: v1 [math.co] 22 Jan 2018

arxiv: v1 [cs.dm] 29 Oct 2012

A note on network reliability

Parameterized Domination in Circle Graphs

An Elementary Construction of Constant-Degree Expanders

Packing edge-disjoint triangles in regular and almost regular tournaments

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

Efficient testing of large graphs

On the Dynamic Chromatic Number of Graphs

On the oriented chromatic index of oriented graphs

USING GRAPHS AND GAMES TO GENERATE CAP SET BOUNDS

Approximability and Parameterized Complexity of Consecutive Ones Submatrix Problems

Homomorphisms in Graph Property Testing - A Survey

6.854J / J Advanced Algorithms Fall 2008

Online Interval Coloring and Variants

Even Cycles in Hypergraphs.

ON WEAK CHROMATIC POLYNOMIALS OF MIXED GRAPHS

U.C. Berkeley Better-than-Worst-Case Analysis Handout 3 Luca Trevisan May 24, 2018

Large induced forests in sparse graphs

Complexity of conditional colorability of graphs

Chapter 1. Comparison-Sorting and Selecting in. Totally Monotone Matrices. totally monotone matrices can be found in [4], [5], [9],

Cycle lengths in sparse graphs

Dynamic Programming on Trees. Example: Independent Set on T = (V, E) rooted at r V.

Increasing the Span of Stars

On vertex coloring without monochromatic triangles

Relating minimum degree and the existence of a k-factor

Multi-coloring and Mycielski s construction

The minimum G c cut problem

The Minimum Rank, Inverse Inertia, and Inverse Eigenvalue Problems for Graphs. Mark C. Kempton

A shorter proof of Thomassen s theorem on Tutte paths in plane graphs

MINIMALLY NON-PFAFFIAN GRAPHS

Transcription:

Algorithmica (1997 17: 09 3 Algorithmica 1997 Springer-Verlag New York Inc. Finding and Counting Given Length Cycles 1 N. Alon, R. Yuster, and U. Zwick Abstract. We present an assortment of methods for finding and counting simple cycles of a given length in directed and undirected graphs. Most of the bounds obtained depend solely on the number of edges in the graph in question, and not on the number of vertices. The bounds obtained improve upon various previously known results. Key Words. Graph algorithms, Cycles. 1. Introduction. The problem of deciding whether a given graph G = (V, E contains a simple cycle of length k is among the most natural and easily stated algorithmic graph problems. If the cycle length k is part of the input, then the problem is clearly NPcomplete as it includes in particular the Hamiltonian cycle problem. For every fixed k, however, the problem can be solved in either O(VE time [11] or O(V ω log V [], where ω<.376 is the exponent of matrix multiplication. The main contribution of this paper is a collection of new bounds on the complexity of finding simple cycles of length exactly k, where k 3 is a fixed integer, in a directed or an undirected graph G = (V, E. These bounds are of the form O(E α k or of the form O(E βk d(g γ k, where d(g is the degeneracy of the graph (see below. The bounds improve upon previously known bounds when the graph in question is relatively sparse or relatively degenerate. We let C k stand for a simple cycle of length k. When considering directed graphs, a C k is assumed to be directed. We show that a C k in a directed or undirected graph G = (V, E, if one exists, can be found in O(E /k time, if k is even, and in O(E /(k+1 time, if k is odd. For finding triangles (C 3 s, we get the slightly better bound of O(E ω/(ω+1 = O(E 1.41, where ω<.376 is the exponent of matrix multiplication. Even cycles in undirected graphs can be found even faster. A C 4k in an undirected graph G = (V, E, if one exists, can be found in O(E (1/k(1+1/k time. A C 4k,ifone exists, can be found in O(E (1/k 1/(k+1 time. In particular, we can find an undirected C 4 in O(E 4/3 time and an undirected C 6 in O(E 13/8 time. The degeneracy d(g of an undirected graph G = (V, E is the smallest number d for which there exists an acyclic orientation of G in which all the out-degrees are at most d. The degeneracy d(g of a graph G is linearly related to the arboricity a(g of the graph, i.e., a(g = (d(g, where a(g is the minimal number of forests needed 1 This work was supported in part by The Basic Research Foundation administrated by The Israel Academy of Sciences and Humanities. School of Mathematical Sciences, Raymond and Beverly Sackler Faculty of Exact Sciences, Tel Aviv University, Tel Aviv 69978, Israel. {noga,raphy,zwick}@math.tau.ac.il. Received October 18, 1994; revised May 5, 1995. Communicated by N. Megiddo.

10 N. Alon, R. Yuster, and U. Zwick Table 1. Finding small cycles in directed graphs some of the new results. Cycle Complexity Cycle Complexity C 3 E 1.41, E d(g C 7 E 1.75, E 3/ d(g C 4 E 1.5, E d(g C 8 E 1.75, E 3/ d(g C 5 E 1.67, E d(g C 9 E 1.8, E3/ d(g 3/ C 6 E 1.67, E 3/ d(g 1/ C 10 E 1.8, E5/3 d(g /3 to cover all the edges of G. The degeneracy of a directed graph G = (V, E is defined to be the degeneracy of the undirected version of G. The degeneracy of a graph is an important parameter of the graph that appears in many combinatorial results. It is easy to see that for any graph G = (V, E we have d(g E 1/. For graphs with relatively low degeneracy we can improve upon the previously stated results. A C 4k in a directed or undirected graph G = (V, E that contains one can be found in O(E 1/k d(g time. A C 4k+1, if one exists, can be found in O(E 1/k d(g 1+1/k time. Similar results are obtained for finding C 4k s and C 4k 1 s. In particular, C 3 s and C 4 s can be found in O(E d(g time and C 5 s in O(E d(g time. Some of the results mentioned are summarized in Tables 1 and. As any planar graph has a vertex whose degree is at most 5, the degeneracy of any planar graph is at most 5. As a consequence of the above bounds we get, in particular, that C 3 s, C 4 s, and C 5 s in planar graphs can be found in O(V time. This in fact holds not only for planar graphs but for any nontrivial minor-closed family of graphs. Another contribution of this paper is an O(V ω algorithm for counting the number of C k s, for k 7, in a graph G = (V, E. A preliminary version of this work appeared in [1].. Comparison with Previous Works. Monien [11] obtained, for any fixed k 3, an O(VE algorithm for finding C k s in a directed or undirected graph G = (V, E. In a previous work [] we showed, using the color-coding method, that a C k, for any fixed k 3, if one exists, can also be found in O(V ω expected time or in O(V ω log V worst-case time, where ω<.376 is the exponent of matrix multiplication. Our new O(E /k algorithm is better than both the O(VE and the O(V ω algorithms when the input graph G = (V, E is sufficiently sparse. It is interesting to note that, for k 6, Monien s O(VE bound is superseded by either the O(V ω algorithm, when the graph is dense, or by the O(E 1/ k/ algorithm, when the graph is sparse. Table. Finding small cycles in undirected graphs some of the new results. Cycle Complexity Cycle Complexity C 4 E 1.34 C 8 E 1.7 C 6 E 1.63 C 10 E 1.78

Finding and Counting Given Length Cycles 11 For every k 7, each one of the four bounds (including the bound that involves the degeneracy beats the others on an appropriate family of graphs. In a previous work [16] we have also shown that cycles of an even length in undirected graphs can be found even faster. Namely, for any even k 4, if an undirected graph G = (V, E contains a C k, then such a C k can be found in O(V time. Our O(E (1/k(1+1/k bound for C 4k and O(E (1/k 1/(k+1 bound for C 4k are again better when the graph is sparse enough. Itai and Rodeh [8] showed that a triangle (a C 3 in a graph G = (V, E that contains one can be found in O(V ω or O(E 3/ time. We improve their second result and show that the same can be done, in directed or undirected graphs, in O(E ω/(ω+1 = O(E 1.41 time. Chiba and Nishizeki [6] showed that triangles (C 3 s and quadrilaterals (C 4 s in graphs that contain them can be found in O(E d(g time. As d(g = O(E 1/ for any graph G, this extends the result of Itai and Rodeh. We extend the result of Chiba and Nishizeki and show that C 4k 1 s and C 4k s can be found in O(E 1/k d(g time. We also show that C 4k+1 s can be found in O(E 1/k d(g 1+1/k time. This gives, in particular, an O(E d(g algorithm for finding pentagons (C 5 s. Our results apply to both directed and undirected graphs. Itai and Rodeh [8] and also Papadimitriou and Yannakakis [13] showed that C 3 s in planar graphs can be found in O(V time. Chiba and Nishizeki [6] showed that C 3 s as well as C 4 s in planar graphs can be found in O(V time. Richards [14] showed that C 5 s and C 6 s in planar graphs can be found in O(V log V time. We improve upon the result of Richards and show that C 5 s in planar graphs can be found in O(V time. In a previous work [] we showed, using color-coding, that, for any k 3, a C k in a planar graph, if one exists, can be found in either O(V expected time or O(V log V worst-case time. The fact that the number of triangles in a graph can be counted in O(V ω time is trivial. In [] we showed, using color-coding, that, for any k 3, a C k, if one exists, can be found in either O(V ω expected time or in O(V ω log V worst-case time. Here we show that for any k 7 the number of C k s in a graph can be counted in O(V ω time. The counting method used here yields, in particular, a way of finding C k s for k 7, in O(V ω worst-case time. Sundaram and Skiena [15] have recently presented some more fixed-subgraph isomorphism algorithms. The results presented here, and in [] and [16], improve some of their results. Eppstein [7] has recently shown that the fixed-subgraph isomorphism problem for planar graphs, i.e., given a fixed graph H and a planar graph G = (V, E, find a subgraph of G isomorphic to H, can be solved, for every fixed H,inO(Vtime. 3. Finding Cycles in Sparse Graphs. Monien [11] obtained his O(VE algorithm by the use of representative collections. Such collections are also used by our algorithms. In what follows, a p-set is a set of size p. DEFINITION 3.1 [11]. Let F be a collection of p-sets. A subcollection ˆF F is q- representative for F if, for every q-set B, there exists a set A F such that A B = if and only if there exists a set A ˆF with this property.

1 N. Alon, R. Yuster, and U. Zwick It follows from a combinatorial lemma of Bollobás [3] that any collection F of p-sets, no matter how large, has a q-representative subcollection of size at most ( p+q p. Monien [11] describes an O(pq q i=0 pi F -time algorithm for finding a q-representative subcollection of F whose size is at most q i=0 pi. Relying on Monien s result we obtain the following lemma: LEMMA 3.. Let F be a collection of p-sets and let G be a collection of q-sets. Consider p and q to be fixed. In O( F + G time, we can either find two sets A F and B G such that A B = or decide that no two such sets exist. PROOF. We use Monien s algorithm to find a q-representative subcollection ˆF of F whose size is at most q i=0 pi and a p-representative subcollection Ĝ of G whose size is at most p i=0 qi. This takes only O( F + G time (as p and q are constants. It is easy to see that if there exist A F and B G such that A B =, then there also exist A ˆF and B Ĝ such that A B =. To see this note that if A B =, then, by the definition of q-representatives, there must exist a set A ˆF such that A B = and then there must exist a set B Ĝ such that A B = as required. After finding the representative collections ˆF and Ĝ it is therefore enough to check whether they contain two disjoint sets. This can be easily done in constant time (as p and q are constants. We also need the following lemma that follows immediately from the work of Monien [11]. LEMMA 3.3 [11]. Let G = (V, E be a directed or undirected graph, let v V, and let k 3. AC k that passes through v, if one exists, can be found in O(E time. We are finally able to present our improved algorithm. THEOREM 3.4. Deciding whether a directed or undirected graph G = (V, E contains simple cycles of length exactly k 1 and of length exactly k, and finding such cycles if it does, can be done in O(E 1/k time. PROOF. We describe an O(E 1/k -time algorithm for finding a C k in a directed graph G = (V, E. The details of all the other cases are similar. Let = E 1/k. A vertex in G whose degree is at least is said to be of high degree. The graph G = (V, E contains at most E/ = O(E 1 1/k high-degree vertices. We check, using Monien s algorithm (Lemma 3.3, whether any of these high-degree vertices lies on a simple cycle of length k. For each vertex this costs O(E operations and the total cost is O(E / = O(E 1/k. If one of these vertices does lie on a cycle of length k we are done. Otherwise, we remove all the high-degree vertices and all the edges adjacent to them from G and obtain a subgraph G that contains a C k if and only if G does. The maximum degree of G is at most = E 1/k and there are therefore at most E k 1 = E 1/k simple directed paths of length k in G. We can find all these simple paths in O(E 1/k

Finding and Counting Given Length Cycles 13 time. We divide these paths into groups according to their endpoints. This can be done using radix sort in O(E 1/k time and space. We get a list of all the pairs of vertices connected by simple directed paths of length exactly k. For each such pair u,v,weget a collection F u,v of (k 1-sets. Each (k 1-set in F u,v corresponds to the k 1 intermediate vertices that appear on simple directed paths of length k from u to v. For each pair u,v that appears on the list, we check whether there exist two directed paths of length k, one from u to v and the other from v to u, that meet only at their endpoints. Such two paths exist if there exist A F u,v and B F v,u such that A B =. This can be checked, as shown in Lemma 3., in O( F u,v + F v,u time. As the sum of the sizes of all these collections is O(E 1/k, the total complexity is again O(E 1/k. This completes the proof. In the case of triangles we can get a better result by using fast matrix multiplication. THEOREM 3.5. Deciding whether a directed or an undirected graph G = (V, E contains a triangle, and finding one if it does, can be done is O(E ω/(ω+1 = O(E 1.41 time. PROOF. Let = E (ω 1/(ω+1. A vertex is said to be of high degree if its degree is more than and of low degree otherwise. Consider all directed paths of length in G whose intermediate vertex is of low degree. There are at most E such paths and they can be found in O(E time. For each such path, check whether its endpoints are connected by an edge in the appropriate direction. If no triangle is found in this way, then any triangle in G must be composed of three high-degree vertices. As there are at most E/ high-degree vertices, we can check whether there exists such a triangle using matrix multiplication in O((E/ ω time. The total complexity of the algorithm is therefore ( ( E ω O E + = O(E ω/(ω+1. This completes the proof. We have not been able to utilize matrix multiplication to improve upon the result of Theorem 3.4 for k 4. This constitutes an interesting open problem. 4. Finding Cycles in Graphs with Low Degeneracy. An undirected graph G = (V, E is d-degenerate (see p. of [4] if there exists an acyclic orientation of it in which d out (v d for every v V. The smallest d for which G is d-degenerate is called the degeneracy or the max-min degree of G and is denoted by d(g. It can be easily seen (see again [4] that d(g is the maximum of the minimum degrees taken over all the subgraphs of G. The degeneracy d(g of a graph G is linearly related to the arboricity a(g of the graph, i.e., a(g = (d(g, where a(g is the minimal number of forests needed to cover all the edges of G. The degeneracy of a directed graph G = (V, E is defined to be the degeneracy of the undirected version of G. It is easy to see that the degeneracy of any planar graph is at most 5. Clearly, if G is d-degenerate, then

14 N. Alon, R. Yuster, and U. Zwick E d V. The following simple lemma, whose proof is omitted, is part of the folklore (see, e.g., [10]. LEMMA 4.1. Let G = (V, E be a connected undirected graph G = (V, E. An acyclic orientation of G such that for every v V we have d out (v d(g can be found in O(E time. The main result of this section is the following theorem: THEOREM 4.. Let G = (V, E be a directed or an undirected graph. (i Deciding whether G contains a simple cycle of length exactly 4k, and finding such a cycle if it does, can be done in O(E 1/k d(g 1 1/k time. (ii Deciding whether G contains simple cycles of length exactly 4k 1 and of length exactly 4k, and finding such cycles if it does, can be done in O(E 1/k d(g time. (iii Deciding whether G contains a simple cycle of length exactly 4k + 1, and finding such a cycle if it does, can be done in O(E 1/k d(g 1+1/k time. PROOF. We show how to find a C 4k+1 in a directed graph G = (V, E, if one exists, in O(E 1/k d(g 1+1/k time. The proofs of the other claims are easier. If d(g E 1/(k+1, we can use the algorithm of Theorem 3.4 whose complexity is O(E 1/(k+1 O(E 1/k d(g 1+1/k. Assume therefore that d(g E 1/(k+1. Let = E 1/k /d(g 1+1/k.Asd(G E 1/(k+1, we have that d(g. A vertex is said to be of high degree if its degree is more than and of low degree otherwise. As in the proof of Theorem 3.4, we can check in O(E / time whether any of the high-degree vertices lies on a C 4k+1. If none of them lies on a C 4k+1, we can remove all the high-degree vertices along with the edges adjacent to them from G and obtain a graph whose maximal degree is at most. The degeneracy of a graph can only decrease when vertices and edges are deleted and d(g is therefore an upper bound on the degeneracy of the graph obtained. Suppose therefore that G is a graph with maximal degree and degeneracy d(g. To find a C 4k+1 in G, it is enough to find all directed simple paths of length k and k + 1inGand then check, using the algorithm described in the proof of Lemma 3., whether there exist a path of length k and a path of length k + 1 that meet only at their endpoints. In O(E time we can get an acyclically oriented version G of G in which the outdegree of each vertex is at most d(g. The orientations of the edges in G and G may be completely different. The number of paths, not necessarily directed, of length k + 1inG, is at most E k ( k i d(g k i = O(E k d(g k. i i=0 To see this, consider the orientations, in G, of the edges on a (k + 1-path in G. In at least one direction, at most k of the edges are counterdirected. The number of paths of length k + 1 in which exactly i among the last k edges are counterdirected is at

Finding and Counting Given Length Cycles 15 most E (k i i d(g k i. The binomial coefficient ( k i stands for the possible choices for the position of the counterdirected edges in the path. Similarly, the number of paths of length k in G is O(E k d(g k 1. We can lower the number of paths of length k + 1 and k we have to consider by utilizing the fact that a C 4k+1 can be broken into two paths of length k + 1 and k in many different ways. In particular, let C be a directed C 4k+1 in G and consider the orientations of its edges in G.As4k+1is odd and as G is acyclic, C must contain three consecutive edges e k, e k+1, and e k+, the first two of which have the same orientation while the third one has an opposite orientation. It is therefore enough to consider all (k + 1-paths that start with at least two backward oriented edges and all k-paths that start with at least one backward oriented edge. The orientations referred to here are in G. The number of paths of length k+1ingwhose first two edges are backward oriented in G is O(E k 1 d(g k+1. To see this, note that any such path is composed of a directed path {e k, e k+1 } of length, attached to an arbitrarily oriented path {e 1,...,e k 1 } of length k 1. The number of paths of length k 1 is, as shown earlier, at most O(E k 1 d(g k 1 and the number of directed path of length with a specified starting point is at most d(g. Similarly, there are at most O(E k 1 d(g k k-paths that start with a backward oriented edge. It should be clear from the above discussion that all the required (k+1- and k-paths, whose total number is O(E k 1 d(g k+1, can be found in O(E k 1 d(g k+1 time. Paths which are not properly directed, in G, are thrown away. Properly directed paths are sorted, using radix sort, according to their endpoints. Using Lemma 3. we then check whether there exist a directed (k + 1-path and a directed k-path that close a directed simple cycle. All these operations can again be performed in O(E k 1 d(g k+1 time. Recalling that = E 1/k /d(g 1+1/k, we get that the overall complexity of the algorithm is ( E O + E k 1 d(g k+1 = O(E 1/k d(g 1+1/k. This completes the proof of the theorem. As an immediate corollary we get: COROLLARY 4.3. If a directed or undirected planar graph G = (V, E contains a pentagon (ac 5, then such a pentagon can be found in O(V worst-case time. By combining the ideas of this section, the O(E ω/(ω+1 algorithm of Theorem 3.5, and the color-coding method [] we can also obtain the following result. THEOREM 4.4. Let G = (V, E be a directed or undirected graph. AC 6 in G, if one exists, can be found in either O((E d(g ω/(ω+1 = O((E d(g 1.41 expected time or O((E d(g ω/(ω+1 log V = O((E d(g 1.41 log V worst-case time. PROOF. We show how to find a C 6 in an undirected graph G = (V, E, if one exists,

16 N. Alon, R. Yuster, and U. Zwick Fig. 1. The possible orientations of C 6 in G. in O((E d(g ω/(ω+1 expected time, or O((E d(g ω/(ω+1 log V worst-case time. The proof of the directed case is similar. In O(E time we can get an acyclically oriented version G = (V, E of G in which the out-degree of each vertex is at most d(g. Suppose that G contains a C 6. The six possible nonisomorphic orientations of this C 6 in G are shown in Figure 1. We refer to these orientations as A 1,...,A 6. Our algorithm checks, for each 1 i 6, whether G contains an A i and if so finds one. We show how to find an A 1 in G, if one exists. The other cases are similar, and in fact easier. As in [], we color the vertices of G randomly using six colors (i.e., every vertex receives a number between 1 and 6, all numbers equally likely. Let c(v denote the color of vertex v. Let A be a specific copy of an A 1 in G. We say that A is well-colored if its vertices are consecutively colored by 1 through 6, and the color 1 is assigned to one of the three vertices having only outgoing edges in A. (By consecutively colored we mean that each v A with c(v < 6 has a neighbor u A with c(u = c(v + 1. The probability that A is well-colored is 6/6 6. We now show how to detect a well-colored copyofana 1 deterministically, if one exists. Create a new undirected graph G = (V, E defined as follows: V ={v V: c(v {,4,6}} E ={(u,v: c(u=6, c(v =, ( w V(c(w = 1, (w,u, (w, v E } {(u,v: c(u=, c(v = 4, ( w V(c(w = 3, (w,u, (w, v E } {(u,v: c(u=4, c(v = 6, ( w V(c(w = 5, (w,u, (w, v E }. Clearly, V V. To create G, we examine each edge (w, u E with c(w odd. Suppose c(w = 1 and c(u = 6. We create edges in G between u and all vertices v such that (w, v E and c(v =. There are at most d(g 1 such vertices. We therefore have E < E d(g and G can be constructed in O(E d(g time from G. Clearly, there exists an undirected triangle in G iff there exists a well-colored A 1 in G. We can detect such a triangle in G in O((E ω/(ω+1 = O((Ed(G ω/(ω+1 time using the algorithm of Theorem 3.5. If such a triangle is not found, we repeat the whole process using a new random coloring. If G contains an A 1, then such an A 1 will be found after an expected number of 6 5 = 7776 attempts. We have thus shown how to detect an A 1 in G, if one exists, in O((E d(g ω/(ω+1 = O((E d(g ω/(ω+1 expected time. As shown in [], such a coloring scheme can be derandomized at the price of an O(log V factor.

Finding and Counting Given Length Cycles 17 5. Finding Cycles in Sparse Undirected Graphs. To obtain the results of this section we rely on the following combinatorial lemma of Bondy and Simonovits [5]. LEMMA 5.1 [5]. Let G = (V, E be an undirected graph. If E 100k V 1+1/k, then G contains a C l for every integer l [k, n 1/k ]. By combining the algorithm described in the proof of Theorem 4. with an algorithm given in [16] we obtain the following theorem. THEOREM 5.. Let G = (V, E be an undirected graph. (i AC 4k in G, if one exists, can be found in O(E (1/k(1+1/k time. (ii AC 4k in G, if one exists, can be found in O(E (1/k 1/(k+1 time. PROOF. We prove the second claim. The proof of the first claim is similar. Let d = 00k E 1/(k+1.Ifd(G d, then, by the definition of degeneracy, there is a subgraph G = (V, E of G in which the minimal degree is at least d. Such a subgraph can be easily found in O(E time (see, e.g., [10]. Clearly, E dv / 100k V E 1/(k+1 and therefore E (100k V 1+1/k 100k (V 1+1/k. By Lemma 5.1 we get that G contains a C 4k and such a C 4k can be found in O(V = O(E /(k+1 time using the algorithm given in [16]. If, on the other hand, d(g d, then a C 4k in G, ifone exists, can be found in O(E 1/k d = O(E (1/k 1/(k+1 time using the algorithm of Theorem 4.. It is easy to check that E /(k+1 E (1/k 1/(k+1 with equality holding only if k = 1. In both cases the complexity is therefore O(E (1/k 1/(k+1 as required. COROLLARY 5.3. Let G = (V, E be an undirected graph. (i A quadrilateral (C 4 in G, if one exists, can be found in O(E 4/3 time. (ii A hexagon (C 6 in G, if one exists, can be found in O(E 13/8 time. 6. Counting Small Cycles. Let G = (V, E be a simple undirected graph and let A = A G be the adjacency matrix of G. Assume for simplicity that V ={1,...,n}. Denote by a (k = (A k the elements of the kth power of A. The trace tr(a k of A k, which is the sum of the entries along the diagonal of A k, gives us the number of closed walks of length k in G. If we could also compute the number of nonsimple closed walks of length k in G we would easily obtain the number of simple closed paths of length k in G. This number is just k times the number of C k s in G. Before describing a way of counting the number of nonsimple closed walks of length k, where k 7, in a graph G in O(V ω time, we need the following definitions: DEFINITION 6.1. Let G 1 = (V 1, E 1 and G = (V, E be two simple graphs. A mapping f : V 1 E 1 V E is a homomorphism if for every v V 1 we have f (v V and for every e = (u,v E 1 we have f (e = ( f (u, f (v E.If f is onto V E, we say that G is a homomorphic image of G 1.

18 N. Alon, R. Yuster, and U. Zwick Fig.. The 4-cyclic graphs. DEFINITION 6.. A graph H = (V H, E H is said to be k-cyclic, for k 3, if it is a homomorphic image of the cycle C k. The number of different homomorphisms from C k to H is denoted by c k (H. Clearly, H is k-cyclic if and only if c k (H >0. It is easy to check, for example, that C 3 is k-cyclic for every k 3 except k = 4. It is also not difficult to check that c 3 (C 3 = 6 (and more generally c k (C k = k for every k 3 and that c 5 (C 3 = 30. The only 3-cyclic graph is C 3 itself. The k-cyclic graphs, for 4 k 7, are given in Figures 5. Let n G (H denote the number of subgraphs of G isomorphic to H. Clearly, the total number of closed walks of length k in G is tr(a k = H c k (Hn G (H. If c k (H >0, then H is connected and has at most k edges. Also, H cannot be a tree on k + 1 vertices as each edge leading to a leaf must be the image of at least two edges in C k. Hence, V H kand in fact, V H < k unless H = C k. We therefore obtain, for an undirected graph G = (V, E: [ n G (C k = 1 k tr(a k ] (1 c k (Hn G (H. V H <k A very similar formula can be obtained for directed graphs. We show how to compute n G (H, for all k-cyclic graphs H with 3 k 7, in O(V ω time. Hence, we obtain the following theorem. THEOREM 6.3. The number of C k s, for 3 k 7 in an undirected (or directed graph G = (V, E, can be found in O(V ω time. PROOF. We consider the undirected case. The directed case is similar and, in fact, slightly simpler (as there are less k-cyclic graphs. Clearly, the traces tr(a k, for 3 k 7, can be computed in O(V ω time using fast matrix multiplication. It remains to show how to find n G (H for all k-cyclic graphs H, where 3 k 7, excluding the cycles C 3,...,C 7 themselves, in O(V ω time. The k-cyclic graphs shown in Figures 5, which are not simple cycles, are denoted by H 1,...,H 15 (they are ordered according to the number of edges they contain. Fig. 3. The 5-cyclic graphs.

Finding and Counting Given Length Cycles 19 Fig. 4. The 6-cyclic graphs. The following list shows how to obtain n G (H i, for 1 1 15, and n G (C k, for 3 k 7. In all cases the formulae reference at most O(V values of a (p for some 1 p k and can hence be computed in O(V ω time. We let d i = a ( ii denote the degree of vertex i. 1. n G (C 3 = 1 6 tr(a3.. 3. 4. 5. n G (H 1 = E = n G (H = 1 i<j n ( di. a (1. n G (C 4 = 1 8 [tr(a4 4n G (H n G (H 1 ]. n G (H 3 = (i, j E (d i 1(d j 1 3n G (C 3. Fig. 5. The 7-cyclic graphs.

0 N. Alon, R. Yuster, and U. Zwick 6. 7. 8. n G (H 4 = ( di 3. [ ] n G (H 5 = 1 a (3 ii (d i. Note that a (3 ii is twice the number of triangles that pass through vertex i. n G (C 5 = 1 10 [tr(a5 10n G (H 5 30n G (C 3 ]. 9. 10. 11. n G (H 6 = (i, j E ( a (. Note that a ( is the number of common neighbors of i and j, which is also the number of paths of length between i and j. [ ( ] n G (H 7 = 1 a (3 di ii. n G (H 8 = (i, j E a ( (d i (d j n G (H 6. 1. Note that we must subtract n G (H 6 to avoid the case in which the two degree-one vertices of H 8 are, actually, the same vertex. n G (H 9 = (d i j i ( a (. 13. Note that j i n G (H 10 = ( a ( is exactly the number of quadrilaterals in which i participates. ( 1 a(3 ii ( j i a ( 6n G (C 3 n G (H 5 4n G (H 6. Note that ( 1 a(3 ii ( j i a( is simply the number of triangles through i times the number of paths of length that begin with i. However, we must only count such a triangle and such a path if they are disjoint, so we must subtract appropriate occurrences of C 3, H 5, and H 6.

Finding and Counting Given Length Cycles 1 14. n G (H 11 = ( 1 a(3 ii n G (H 6. 15. Since we have already shown how to compute n G (H for all the 6-cyclic graphs, excluding C 6, we can use (1 to compute n G (C 6. 16. n G (H 1 = a ( a (3 9n G (C 3 n G (H 5 4n G (H 6. 17. 18. 19. (i, j E Here we count the number of triangles through (i, j and multiply each triangle by the number of walks of length 3 between i and j. Since these walks need not be simple, or may intersect the triangle, we may actually be counting C 3 s, H 5 s, or H 6 s. Therefore, we subtract the appropriate values. n G (H 14 = n G (H 13 = (i, j E ( a (. 3 (d i B i n G (H 1, where B i is the number of C 5 s passing through i. The expression for B i is [ B i = 1 a (5 ii 10a (3 ii 4a (3 ii (d i a ( (d j ] ( 1 a(3 jj a (. (i,j E (i,j E n G (H 15 = ( 1 a(3 ii ( j i ( a ( 6n G (H 6 n G (H 1 6n G (H 13. Using slightly more effort, it can be shown that, in O(V ω time, we can also count the number of C k s, for 3 k 7, that pass through each vertex of G. We have, in fact, done this in the preceding proof for k = 3, 4, 5. If the graph G contains a C k, for some 3 k 7, we can therefore find, in O(V ω time, a vertex through which such a C k passes. We can then locate a C k in the graph in additional O(E time using Monien s method (Lemma 3.3. Similar formulae can be obtained, of course, for the number of octagons (C 8 s and even larger cycles. To compute the number of octagons, however, we have to compute first the number of K 4 s in the graph, since a K 4 is 8-cyclic. We do not know how to do this is O(V ω time.

N. Alon, R. Yuster, and U. Zwick It is easy to count the number of K 4 s in a graph in O(V ω+1 time: for each vertex, count the number of triangles among its neighbors, sum these numbers, and divide by 4. Counting the number of K 4 s in a graph, or, in fact, deciding whether a graph contains a K 4,ino(V ω+1 time, is an interesting open problem. For counting the number of larger cycles using our method, we would have to count the number of larger cliques in the graph. Nešeťril and Poljak [1] give an O(V ω l/3 - time algorithm for deciding whether a graph G = (V, E contains a K l. It is easy to check that their method can also be used to count the number of such cliques contained in the graph. By combining the method of Nešeťril and Poljak [1] with the ideas used in Section 4, we get the following result. THEOREM 6.4. The number of K l s in an undirected graph G = (V, E can be counted in either O(V (d(g ω (l 1/3 or O(E (d(g ω (l /3 time. Using an idea similar to the one used in Theorem 3.5, Kloks et al. [9] have recently obtained an O(E (ω+1/ = O(E 1.69 time algorithm for counting the number of K 4 s contained in a graph G = (V, E. They also obtain improved results for finding larger cliques and other induced subgraphs. References [1] N. Alon, R. Yuster, and U. Zwick. Finding and counting given length cycles. Proceedings of the nd European Symposium on Algorithms, Utrecht, Lecture Notes in Computer Science, Vol. 855, pages 354 364. Springer-Verlag, 1994. [] N. Alon, R. Yuster, and U. Zwick. Color-coding. Journal of the ACM, 4:844 856, 1995. [3] B. Bollobás. On generalized graphs. Acta Mathematica Academiae Scientarium Hungaricae, 16:447 45, 1965. [4] B. Bollobás. Extremal Graph Theory. Academic Press, New York, 1978. [5] J. A. Bondy and M. Simonovits. Cycles of even length in graphs. Journal of Combinatorial Theory, Series B, 16:97 105, 1974. [6] N. Chiba and L. Nishizeki. Arboricity and subgraph listing algorithms. SIAM Journal on Computing, 14:10 3, 1985. [7] D. Eppstein. Subgraph isomorphism in planar graphs and related problems. Proceedings of the 6th Annual ACM SIAM Symposium on Discrete Algorithms, San Francisco, CA, pages 63 640, 1995. [8] A. Itai and M. Rodeh. Finding a minimum circuit in a graph. SIAM Journal on Computing, 7:413 43, 1978. [9] T. Kloks, D. Kratsch, and H. Müller. Finding and counting small induced subgraphs efficiently. Proceedings of the 1st International Workshop on Graph-Theoretic Concepts in Computer Science, Aachen, Lecture Notes in Computer Science, Vol. 1017, pages 14 3. Springer-Verlag, 1995. [10] D. W. Matula and L. L. Beck. Smallest-last ordering and clustering and graph coloring algorithms. Journal of the ACM, 30:417 47, 1983. [11] B. Monien. How to find long paths efficiently. Annals of Discrete Mathematics, 5:39 54, 1985. [1] J. Nešetřil and S. Poljak. On the complexity of the subgraph problem. Commentationes Mathematicae Universitatis Carolinae, 6(:415 419, 1985. [13] C. H. Papadimitriou and M. Yannakakis. The clique problem for planar graphs. Information Processing Letters, 13:131 133, 1981.

Finding and Counting Given Length Cycles 3 [14] D. Richards. Finding short cycles in a planar graph using separators. Journal of Algorithms, 7:38 394, 1986. [15] G. Sundaram and S. S. Skiena. Recognizing small subgraphs. Networks, 5:183 191, 1995. [16] R. Yuster and U. Zwick. Finding even cycles even faster. Proceedings of the 1st International Colloquium on Automata, Languages and Programming, Jerusalem, Lecture Notes in Computer Science, Vol. 80, pages 53 543. Springer-Verlag, Berlin, 1994. Journal version to appear in SIAM Journal on Discrete Mathematics.