Minimum Weight Cycles and Triangles:Equivalences and Algorithms

Minimum Weight Cycles and Triangles: Equivalences and Algorithms

Liam Roditty
Bar Ilan University
Virginia Vassilevska Williams
UC Berkeley
Abstract

We consider the fundamental algorithmic problem of finding a cycle of minimum weight in a weighted graph. In particular, we show that the minimum weight cycle problem in an undirected -node graph with edge weights in or in a directed -node graph with edge weights in and no negative cycles can be efficiently reduced to finding a minimum weight triangle in an node undirected graph with weights in . Roughly speaking, our reductions imply the following surprising phenomenon: a minimum cycle with an arbitrary number of weighted edges can be “encoded” using only three edges within roughly the same weight interval!

This resolves a longstanding open problem posed in a seminal work by Itai and Rodeh [SIAM J. Computing 1978 and STOC’77] on minimum cycle in unweighted graphs.

A direct consequence of our efficient reductions are -time algorithms using fast matrix multiplication (FMM) for finding a minimum weight cycle in both undirected graphs with integral weights from the interval and directed graphs with integral weights from the interval . The latter seems to reveal a strong separation between the all pairs shortest paths (APSP) problem and the minimum weight cycle problem in directed graphs as the fastest known APSP algorithm has a running time of by Zwick [J. ACM 2002].

In contrast, when only combinatorial algorithms are allowed (that is, without FMM) the only known solution to minimum weight cycle is by computing APSP. Interestingly, any separation between the two problems in this case would be an amazing breakthrough as by a recent paper by Vassilevska W. and Williams [FOCS’10], any -time algorithm () for minimum weight cycle immediately implies a -time algorithm () for APSP.

1 Introduction

We consider the algorithmic problem of finding a minimum weight cycle (i.e., weighted girth) in weighted directed and undirected graphs. Surprisingly, although the problem is very fundamental, the state of the art for it dates back to a seminal paper by Itai and Rodeh [14], first presented in STOC’77, that deals only with the unweighted variant of the problem. Itai and Rodeh presented an -time algorithm for an -node unweighted undirected graph and an -time algorithm for an -node unweighted directed graph. (Here is the exponent of square matrix multiplication over a ring, and  [6].) In the same paper, Itai and Rodeh posed the question whether similar results exist for weighted graphs. In this paper we provide a positive answer to this longstanding open problem by presenting -time algorithms for directed graphs with integral edge weights in (and no negative cycles) and for undirected graphs with integral edge weights in .

Our algorithmic results are obtained using new reductions that carefully combine new algorithmic ideas and special combinatorial properties of the minimum weight cycle. More specifically, we reduce the problem to the problem of finding a minimum weight triangle in a node undirected graph with weights in . This reveals also a surprising phenomenon: a minimum cycle with an arbitrary number of weighted edges can be efficiently “encoded” using a cycle of only three edges whose weights are roughly within the same interval! Moreover, our results imply a strong equivalence between the cycle and triangle problems.

Minimum cycle and APSP.

Recently, in FOCS’10 Vassilevska W. and Williams [24] showed that the minimum weight cycle problem is equivalent to many other graph and matrix problems for which no truly subcubic (-time for constant ) algorithms are known. They showed that if there is a truly subcubic algorithm for the minimum weight cycle problem, then many other problems such as the all-pairs-shortest-paths (APSP) problem also have truly subcubic algorithms. Hence, the minimum weight cycle problem has a pivotal role in understanding the complexity of many fundamental polynomial problems in a similar spirit to the role of 3SAT for NP-hard problems.

It is interesting to compare the minimum cycle problem with APSP. In directed graphs, the minimum weight cycle can be computed easily by computing APSP. Given the distance between all pairs of vertices , the weight of the minimum cycle is . Hence, we can compute the minimum weight cycle in cubic time using Floyd-Warshall’s APSP algorithm [8, 23] (or Pettie’s time algorithm [18] if the graph is sparse). If the edge weights are integers in , we can use Zwick’s [27] time algorithm to obtain an algorithm for minimum cycle with the same runtime. Improving Zwick’s running time and in particular obtaining an running time for APSP in directed graphs is one of today’s frontier questions in graph algorithms. Our new -time algorithm for minimum cycle in directed graphs shows that it is not really necessary to compute all pairs shortest paths in order to compute the minimum weight cycle in directed graphs. This seems to reveal a strong separation between APSP and the minimum cycle problem in directed graphs.

The minimum cycle problem in undirected graphs differs from the problem in directed graphs in that the reduction to APSP no longer works: an edge might also be the shortest path from to , and might be and not the weighted girth of the graph. This represents a nontrivial hurdle. Nevertheless, in this paper we show how to overcome this hurdle and obtain an time algorithm for undirected graphs with integer weights in . This matches the runtime of the best APSP algorithm in such graphs: In a paper first presented at STOC’92, Seidel [21] showed that APSP in undirected and unweighted -node graphs can be solved in time. In FOCS’99, Shoshan and Zwick [22] (following Galil and Margalit [12]) showed that APSP in undirected -node graphs with integer edge weights in can be solved in time, thus extending Seidel’s running time to weighted undirected graphs.

Our results: reductions and algorithms.

We develop our algorithms by first obtaining extremely efficient reductions from the minimum weight cycle problem to the minimum weight triangle problem which preserve the interval in which the weights lie, within a constant factor.

Undirected graphs. For undirected graphs our results are as follows.

Theorem 1.

Let be an undirected graph with and let be a minimum cycle in . There is an time deterministic algorithm that computes a cycle and constructs graphs on nodes and edge weights in such that either or the minimum out of all weights of triangles in the graphs is exactly .

Since a minimum weight triangle in a graph with weights bounded by can be found via a single distance product computation in time [2, 26], we obtain the following corollary.

Corollary 1.

A minimum weight cycle in an -node undirected graph with integer edge weights in can be found in time.

Directed graphs. Our reduction for undirected graphs relies on the fact that distances are symmetric. It is unlikely that it is possible to modify the reduction so that it works for directed graphs as well. Hence, for directed graphs new ideas are required. The reduction to minimum triangle is not as efficient, however, the resulting algorithm for minimum cycle in directed graphs has the same running time as the one for undirected graphs with nonnegative weights. Our approach for directed graphs can be combined with our approach for undirected graphs to yield an efficient algorithm also for mixed graphs, that is, graphs which contain both directed and undirected edges. The approach works, provided the weights of the mixed graph are nonnegative.

When negative edge weights are allowed, a negative cycle may exist. Finding a minimum weight cycle when its weight is negative is an NP-hard problem, as it solves Hamiltonian cycle. When negative weights are allowed, the minimum cycle problem in the absence of negative cycles is in P for both directed and undirected graphs, but is NP-hard for mixed graphs [4]. Our techniques for directed graphs are strong enough to support negative edge weights within the same running time as when the weights are nonnegative. This is extremely interesting, as the typical way to reduce the general problem to the nonnegative weights problem involves computing node potentials (see e.g. [13]). These potentials however typically increase the magnitude of the weights to even , which would be bad if our goal is to use algorithms that have exponential dependence on the bit representation of the weights, such as . We circumvent the potential approach by focusing on the general problem directly.

We obtain:

Theorem 2.

Let be a directed graph on nodes, . In time one can construct graphs on nodes and edge weights in so that the minimum out of all weights of triangles in the graphs is exactly the weighted girth of .

Our results: equivalences.

Vassilevska W. and Williams [24] showed that the minimum triangle and minimum cycle problems are equivalent, under subcubic reductions. Their reduction from minimum triangle to minimum cycle only increased the number of nodes and the size of the edge weights by a constant factor. However, their reduction from minimum cycle to minimum triangle was not tight; it only proved that an algorithm for minimum triangle would imply an algorithm for minimum cycle. Our reductions, on the other hand, imply a much stronger equivalence between the two problems. This equivalence is especially strong for undirected graphs with integral weights from the range .

Corollary 2.

If there is a time algorithm for the minimum cycle problem in undirected graphs with integral edge weights in , then there is a time algorithm for the minimum triangle problem in such graphs. Conversely, if there is a time algorithm for the minimum triangle problem in undirected graphs with integral edge weights in , then there is an time algorithm for the minimum cycle problem in such graphs.

A natural question is whether the triangle problem is special. Do similar reductions exist between minimum cycle and minimum -cycle for constant ? We answer this in the affirmative.

Theorem 3.

Let be any fixed constant. Let be a graph on nodes, . One can construct undirected graphs on nodes and edge weights in so that the minimum out of all weights of -cycles in the graphs is exactly the weighted girth of . Moreover, given the minimum weight -cycle of the graphs , one can find a minimum weight cycle of in additional time. If is directed, the reduction runs in time, and if is undirected, it runs in time.

Our results: approximation.

Another approach to gain efficiency for problems with seemingly no subcubic time exact algorithms has been to develop fast approximation algorithms (see [27, 1, 7] in the context of shortest paths). Lundell and Lingas [17] gave two approximation algorithms for the girth problem: an time -approximation for undirected unweighted graphs, and an time -approximation for undirected graphs with integer weights in the range . Very recently, Roditty and Tov [19] improved the approximation factor to -approximation for the weighted case while keeping the running time unchanged. Due to Zwick’s [27] time -approximation for APSP and the simple reduction from minimum weight cycle in directed graphs to APSP, the girth of a directed graph admits an -approximation in time. Our reduction from Theorem 1 implies the same result for undirected graphs with nonnegative weights as well, following up on the work of Roditty and Tov from SODA’11 [19].

Theorem 4.

There is an time -approximation algorithm for the minimum cycle problem in undirected graphs with integral weights in .

2 Preliminaries

Let be a weighted graph, where is its set of vertices or nodes (we use these terms interchangeably), is its set of edges, and is a weight function. The function can be extended to the entire by setting for every . Unless otherwise noted, refers to the number of nodes in the graph.

An edge can be directed or undirected. An undirected graph is a graph with undirected edges only and a directed graph is a graph with directed edges only. A mixed graph is a graph that may have both directed and undirected edges. All graphs considered in this paper are simple. A graph is simple if it does not contain self loops or multiple copies of the same edge. In a simple mixed graph, a node pair cannot be connected by both a directed and an undirected edge. In both directed and mixed simple graphs, two directed edges and in opposite directions are allowed since they are considered distinct.

We define a cycle in a graph to be an ordered set of vertices , such that for every and . Let be the sum of the weights of the edges of and let be the weight of the heaviest edge. We denote with the weight of the path that traverses the cycle from to by passing from to and so on. In the case that we traverse from to and continue until we reach . Let denote the number of vertices/edges in . A cycle is simple if no node or edge appears twice in . With this definition, an undirected graph cannot have a simple cycle on nodes, where as directed and mixed graphs can, provided the two cycle edges are in opposite directions and hence distinct.

3 Our approach

Our reductions are based on a combinatorial property of cycles in weighted directed, undirected and mixed graphs that might be of independent interest. This property is extremely useful as it shows that crucial portions of the minimum weight cycle are shortest paths. We present this property in the following lemma.

Lemma 1 (Critical edge).

Let be a weighted graph, where , and assume that does not contain a negative cycle. Let be a cycle in of weight and let . There exists an edge on such that and . Furthermore, if is a minimum weight cycle in then and .

Proof.

We can assume, wlog, that . We start to traverse along from until we reach the first edge that satisfies and . Since either we find an edge that satisfies the requirement, where or we reach without finding such an edge. In the latter case and since the edge satisfies the requirement.

It follows immediately from the properties of the edge that and hence we get that as required.

We now bound . We know that . Since we get that . Also, since we get that .

It remains to show that if is a minimum weight cycle, then and . If is a directed graph, then it is straightforward to see that the minimality of implies that for every and in particular and as required. Thus, we only need to consider the case that is an undirected graph. From the first part of the proof we know that . If , then let be the path from to of weight and let be the portion of from to . The union of and is a walk in whose weight is strictly less than . Furthermore, since , and must differ by at least one edge and hence contains some simple cycle of weight less than , a contradiction to the minimality of . The argument for showing that is symmetric. ∎

Lemma 1 shows that it is possible to decompose every cycle into three portions: a single edge of weight at most and two pieces whose weight differs by at most , and which are shortest paths if the cycle is of minimum weight. This observation is crucial for our efficient reductions. Another important piece of Lemma 1 is that every vertex on the cycle has a critical edge. This is especially important in the directed graph case.

Armed with Lemma 1 we can describe the general framework of our approach. Suppose that we have some way to compute a function that satisfies:

• For every ,

• There exists a vertex on the minimum cycle whose critical edge endpoints satisfy and .

In Section 4 we show how to compute a function in time for undirected graphs with integral weights from , and in Section 5 we show how to compute a function in time for directed graphs with integral weights from and no negative cycles.

Now consider the following (multi-)graph where and are disjoint copies of . For every which was computed we place an edge between and and (for directed graphs) also one between and . These edges get weight and correspond to the two large portions of the minimum cycle. Further, for every edge in , we add an edge from to with weight , i.e. induces a copy of ; these edges correspond to the critical edge of the minimum cycle. In our reduction for directed graphs we further transform into a simple undirected tripartite graph.

Consider from the second bullet above. By Lemma 1, Hence will contain as a triangle of weight . Our reductions in the next two sections give transformations which ensure that every triangle in corresponds to a simple cycle in and that is preserved as a triangle. Since the values are upper bounds on the distances, is a minimum weight triangle in . The graph however can have really large weights; can be as large as in general. Thus our transformations also apply a weight reduction technique which reduces all edge weights to the interval . This technique is different for undirected and directed graphs.

Finding a minimum cycle.

Our reductions show that the minimum cycle problem can be efficiently reduced to the minimum triangle problem in a different graph with roughly the same number of nodes and weight sizes. Here we briefly discuss how one can actually find a minimum weight triangle in an -node graph with integral edge weights in the interval . With our reductions, this will give algorithms for the minimum cycle problem as well.

Let be the adjacency matrix of defined as whenever and otherwise. A well known approach to finding a minimum weight triangle mimics Itai and Rodeh’s algorithm for unweighted triangle finding [14]. It first computes the distance product of with itself, , to find for every pair of nodes the minimum length of a path with at most edges between them. Then, the weight of a minimum triangle is exactly

 mini,jA[j,i]+(A⋆A)[i,j].

Finding the actual triangle takes only time after one finds minimizing the above expression. Thus the running time is dominated by the time required for computing . The algorithm of Alon, Galil and Margalit [2] (following Yuval [26]) does this in time, whenever the entries of are integers in . Hence a minimum triangle, can be found in time.

4 Minimum weight cycle in undirected graphs with weights in {1,…,M}

Let be an undirected graph with integral edge weights from the range . In this section we show that in time we can compute a cycle whose weight is at most twice the weight of the minimum weight cycle and a new undirected graph with integral weights from the range whose minimum triangle if exists corresponds to the minimum weight cycle in , with constant probability. (To boost the probability of success, we actually create graphs .) If does not have a triangle then the cycle that we have computed is the minimum weight cycle of . We start by presenting an time algorithm that given an integer either reports a cycle of length or computes all distances up to . The computed distances are used to form the graph .

Cycle or Distance Computation.

The algorithm works in iterations and in each iteration it repeats the same procedure from a new vertex of the graph. This procedure is a simple adaptation of Dijkstra’s algorithm. The input in each iteration is a source vertex and an integer value . The algorithm either reports a cycle of length at most or computes the distances from to every vertex that is within distance from . Lingas and Lundell [17] used a similar approach in order to compute a -approximation of the minimum weight cycle. Their algorithm, however, either returns a cycle of length at most or computes the distances from to every vertex that is within distance from . This small difference between the two algorithms is crucial for our needs. The algorithm is given in Algorithm 1. The running time of the algorithm is . The algorithm repeats the procedure Cycle? times, each time with a different vertex. Every run of Cycle? takes at most time since it stops with the first cycle it detects.

In the next Lemma we prove an important property of the algorithm.

Lemma 2.

For any integer , Min-Cycle either finds a cycle of weight at most , or computes all distances of length at most .

Proof.

A cycle is reported when a vertex is extracted from the priority queue and for one of its edges that is being relaxed the value of before the relaxation is not infinity. As any distance and any distance estimation are at most , if a cycle is reported it must be of length at most . If a cycle is not reported, then our algorithm is almost identical to Dijkstra’s algorithm. The only difference is that our algorithm relaxes an edge when is extracted from the priority queue if and only if , while Dijkstra’s algorithm relaxes all edges of with no restriction. This implies that our algorithm computes all distances that are smaller or equal . ∎

The reduction to minimum triangle.

Our goal is to prove Theorem 1. The main part of the proof is describing an algorithm that computes an upper bound for the minimum weight cycle and an instance of minimum triangle, such that either the girth of the graph is exactly the upper bound, or with constant probability the minimum triangle weight in is the girth of . Below we only present as having large weights. Later on, we find a value with which we use Lemma 2, so that is a bound on the minimum cycle weight that is tight within . As mentioned in Section 3, this value allows us to reduce the weights of so that they fall in the interval .

Reminder of Theorem 1 Let be an undirected graph with and let be a minimum cycle in . There is an time deterministic algorithm that computes a cycle and constructs graphs on nodes and edge weights in such that either or the minimum out of all weights of triangles in the graphs is exactly .

The weight of the minimum cycle is an integer value from the range . From Lemma 2 it follows that we can use algorithm Min-Cycle to perform a binary search over this range in order to find the largest value for which Min-Cycle does not report a cycle but computes all distances of length at most (see Figure 1). This implies that by running Min-Cycle we obtain a cycle of weight at most . Hence, we only need to show that it is possible to detect the minimum cycle in the case that its weight is or less. Let us first prove some consequences of the fact that Min-Cycle does not report a cycle.

Lemma 3.

Let be a minimum cycle in . Suppose that Min-Cycle does not report a cycle. There are three distinct vertices such that and .

Proof.

Let be the critical edge for given by Lemma 1. Assume first that and . If either or then the edge is relaxed. Assume that we are in the case that . Then after is relaxed . If before the relaxation of the algorithm stops and reports a cycle. If before the relaxation of then a cycle will be detected as well but only when the edge is relaxed. This edge must be relaxed since which implies that will be extracted and its edge will satisfy the relaxation requirement and will be relaxed. We conclude that if either or then Min-Cycle must report a cycle, giving a contradiction.

We now turn to the case that either or . Assume wlog that , that is, . From Lemma 1 we know that and . We also know that there is at least one additional vertex between and on the cycle . We now apply Lemma 1 on the vertex . It is easy to see that in that case the edge will be the critical edge of as well. We now have three different vertices and the rest of this case is identical to the first case. ∎

As a first attempt, we create the new graph as follows. The vertex set contains two copies and of . For , let be the set of edges with both endpoints in . The set is empty and the set is , that is, if and only if . Let be the set of edges with one endpoint in and one endpoint in . Let and . If the distance between and was computed by Min-Cycle then we add an edge to with weight . We show that there is triangle in that corresponds to the minimum cycle of and has the same weight.

Claim 5.

Let be a minimum cycle in . Assume that . There exists a triangle in on vertices of of weight .

Proof.

Without loss of generality, let be the vertex from Lemma 3, and let and be the other two vertices. From Lemma 3 we know that all three vertices are distinct and that and . Combining this with the fact that is a minimum cycle and we get that and . When Cycle? is run from it computes and . Hence, there must be a triangle of weight in on the vertices , and . ∎

The claim above shows only one direction, that is, if there is a minimum cycle of weight at most in then there is a corresponding triangle in on vertices and , that correspond to vertices of with the same weight. This situation is depicted in Figure 2(a). To complete the reduction we must show that there are no false positives: triangles in of smaller weight which do not correspond to a minimum cycle of . Unfortunately, this is not the case and may have such false triangles with smaller weight. This situation is depicted in Figure 2(b). Let . If there is a shortest path of length at most from to whose last edge is then there is a triangle in . To see that notice that there are two different shortest paths one from to and one from to , both of length at most . In such a case the graph includes the edges and and together with the edge they form a triangle. Moreover, such a triangle has the same structure as a valid triangle and might be of smaller weight, thus, a triangle detection algorithm cannot distinguish between a valid triangle and a false triangle. In what follows we first show that this is the only situation in which a false triangle is formed and then we show a construction that avoids such false triangles.

In the above pathological case the only reason that the triangle did not correspond to a cycle, was because we had two different paths and that both start in the same vertex and the last vertex of one of these paths was the vertex right before the last vertex of the other path. In the next lemma we show that this is the only bad case.

Lemma 4.

Let be three distinct vertices. Let and be simple shortest paths between and and and respectively. Let and and let . Then, contains a simple cycle of weight at most .

Proof.

Let be with its edges reversed. Look at and . There are two options. Either one path is a subpath of the other, or there is a node such that is a subpath of both, and is followed by in and by in .

Consider the first case. Wlog, is a subpath of (the other inclusion is symmetric). Since , the subpath between and on has at least 2 edges, and adding edge produces a simple cycle of weight less than the sum of the two original path weights.

Consider the second case when exist as above. If there is some node between and on which also appears in after , then let be the first such node. Then no node on between and appears between and in . The two disjoint simple paths between and form a simple cycle on at least nodes since . The weight of this cycle is less than the sum of the two original path weights.

Finally, suppose no such exists. Then the subpaths of and between and and and share no vertices and hence adding edge closes a simple cycle of weight at most . ∎

Lemma 4 implies that our reduction to minimum triangle will work, provided that we can ensure that for every triangle in , the last node before on the shortest path from to in is distinct from . To do this, we use the color-coding method from the seminal paper of Alon, Yuster and Zwick [3]. The idea is as follows. Let be two distinct colors and suppose we assign to every node of one of these colors independently and uniformly at random. Fix four vertices . The probability that and is .

Now we will modify from before. Recall that . For every node of we add a copy to , so that is a copy of . Furthermore, if color we also add a copy to . We now define the set of edges . Let be the set of edges between and , for . The edge set is empty, so . Let such that is the last edge of the shortest path from to . The sets and are defined as follows:

 E12={(x1,z2)∣color(z′)=C1∧color(z)=C2}E22={(x2,z2)∣color(x)=C2∧color(z)=C2}.

The weight of an edge is . The weight of an edge is . We now prove that does not contain false triangles.

Lemma 5.

If is triangle in then there exists a simple cycle in such that and .

Proof.

Any triangle in either have one vertex from and two vertices from or all three vertices from . In the latter case the triangle is also in so we focus in the former case, that is, is a triangle in such that and . Let be the vertices that correspond to and in . Let () be the last vertex before () on the shortest path () between and () in . The fact that and implies that and . Hence we get that and . Combining this with the fact that we get that the paths , and the edge satisfy the requirements of Lemma 4, and there is a simple cycle of weight at most in . ∎

Now that we have shown that does not contain false triangles we prove that the minimum weight cycle in corresponds to a triangle in . (This can be viewed as proving Claim 5 for the new construction of ).

Claim 6.

Let be a minimum cycle in . Assume that . Then there exists a triangle in on vertices of of weight , with constant probability.

Proof.

Without loss of generality, let be the vertex from Lemma 3, and let and be the other two vertices. As in the proof of Claim 5, and and these values are computed by Min-Cycle . The random coloring is successful when , , and . The probability that this happens is . The triangle is in exactly when the coloring is successful, and hence is represented by that triangle in with constant probability. The weight of the triangle is . ∎

Weight reduction.

Currently, the maximum edge weight in can be as large as as the weights of edges in are distances in . To complete the reduction, we show that it is possible to reweight the edges of without changing the minimum triangle so that the edge weights will be integers from the range .

The key idea is to use Lemma 3 in two different ways. As we previously mentioned, Lemma 3 implies that and . Moreover, the bounds and imply that and . Thus, we can remove from every edge of weight strictly more than and every edge of weight or smaller with no effect on the minimum triangle in . We now decrease the weights of all the edges that were left in by . The weight of every triangle in with a node from has decreased by exactly . Hence, the minimum triangle out of those with a node in remains the same. The weights of edges in are now integers from the interval , and the rest of the edge weights are still in . If the minimum weight triangle in now has nodes only from , then this triangle was also the minimum weight one in before the reweighting, and hence corresponds to a minimum weight cycle, with high probability. Otherwise, the minimum weight triangle in has a node from . The minimum out of these triangles was also the minimum one among the triangles with a node in also before the reweighting. Hence it also corresponds to a minimum weight cycle, with high probability. This completes the description of our construction.

Derandomization.

The reduction can be made deterministic, just as in the color-coding paper of Alon et al.  [3], by using a -perfect hash family, a family of hash functions from to so that for every with