Sei sulla pagina 1di 2

18.434: Seminar in Theoretical Computer Science Mar.

5, 2015

Cycle Detection in Random Graphs


Adisa Kruayatidee adisa@mit.edu

1 Introduction

Apart from development of algorithms that help protect information, another facet of cryp-
tography research attempts to figure out how to break these algorithms - namely, thinking
like an adversary. To start with, consider hash functions, which are widely used in crypto-
graphic techniques. As applying a hash function f to a large set of keys will almost always
cause collisions, it is worthwhile to be able to detect the collisions, and thus determine how
weak f is.

1.1 Random Graphs and Hash Functions

We model our hash function as a random graph f over a finite domain {0, 1, ..., N − 1}. The
random graph associated with f has edges x → f (x), for all x in the domain. Except for the
case when f is a permutation, a guaranteed way to find collisions is to find a cycle (cycle
entry vertex is collision point.)

2 Floyd’s Two Finger Algorithm

Start two pointers at the same vertex. At each step, we move one forward one vertex
(x → f (x)), while the other moves forward by two vertices (x0 → f (f (x0 ))). If they collide,
we have a cycle.

• Proof: Suppose we begin at vertex 1, and the cycle occurs by an edge from vertex
n back to m + 1, m + 1 < n. Then at time t, the slow pointer will be at vertex
m + (t − m)(n − m), and the fast pointer will be at vertex m + (2t − m)(n − m).
However, if n − m|t and t ≥ m, it is easy to see that the pointers will be at the same
vertex.

1
• Time: Expected O(3n), when tail is short.

• Memory: O(1)

2.1 Finding Cycle Entry

After the pointers collide, move one back to the start vertex. Move both pointers forward
one vertex per time, until they collide at the cycle entry. This takes another 2m = O(2n)
steps each.

3 Nivasch’s Algorithm: Faster cycle detection

Use a single pointer, moving it forward one vertex at a time. Maintain a stack of values seen
in the graph, requiring that it be monotonically increasing. Each time we encounter a value
v larger than the top of the stack, pop all numbers off the top of the stack that are larger
than v, then push v onto the stack. Stop when we have two numbers of the same value on
top of the stack, which indicates a cycle.

• Proof: After we have visited each vertex once and walk through the cycle the second
time, there is no number that can pop the minimum value on the cycle off the stack.
Thus, when we reach this minimum value, we will have two of the same numbers on
top of the stack and will stop.

• Time: Expected O(1.5n). Better constant than Floyd’s!

• Memory: Expected O(log n), with high probability that tends to 1 as n → inf.

3.1 Time Improvement: Partitioning

Instead of one stack, define a way to partition values into k types and use k stacks. Upon
encountering a value, push it on to the appropriate stack. As we will stop when we see
 the
1
minimum of any type on the cycle, we will likely stop sooner. Expected O 1 + k+1 n time.

Potrebbero piacerti anche