Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!newsfeed.internetmci.com!vixen.cso.uiuc.edu!uchinews!not-for-mail
From: cdjeris@midway.uchicago.edu (Christopher Jeris)
Subject: Cycle-detection algorithm?
X-Nntp-Posting-Host: quads.uchicago.edu
Message-ID: <Dn07Dx.C6q@midway.uchicago.edu>
Summary: Not a homework question, even though it looks like one!
Keywords: SICP list cycle algorithm
Sender: news@midway.uchicago.edu (News Administrator)
Organization: The University of Chicago
Date: Mon, 19 Feb 1996 03:37:09 GMT
Lines: 25

I'm working through SICP in my spare time to learn some things about
programming, and came across the following exercise:

3.19 Write a procedure that determines, _in_constant_space_, whether a
given list structure contains a cycle.

This is easy to do in nonconstant space (just keep track of all the
pairs along your path, and see whether you run into one you've seen
before) -- in fact, this is exercise 3.18.  I came up with an idea
that runs in _almost_ constant space, namely encoding the chain of
`car' and `cdr' followed by a path into a large integer (2^n for a
`cdr' in the nth place) -- but this doesn't seem to be "real" constant
space to me.

Maybe this is more an algorithms question than a Scheme question (it's
not a homework question!) so could anyone point me to a reference for
an algorithm which does the trick in constant space?  I'm a math
student only recently interested in CS, and I don't know much about
algorithms -- the one reference I checked (Sedgewick's "Algorithms in
Modula-3") wasn't any help.

Thank you...

Christopher Jeris		University of Chicago Math:
 c-jeris@uchicago.edu		 The mother of all functors awaits you.
