Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!news.mathworks.com!tank.news.pipex.net!pipex!dish.news.pipex.net!pipex!news00.sunet.se!sunic!news99.sunet.se!news1.transpac.net!news2.transpac.net!pravda.mixware.se!not-for-mail
From: Joachim Flodqvist <joachim@mixware.se>
Subject: Re: Cycle-detection algorithm?
Message-ID: <3128AF37.3299@mixware.se>
Date: Mon, 19 Feb 1996 18:11:19 +0100
Organization: mixware.se
X-Mailer: Mozilla 2.0b5 (WinNT; I)
MIME-Version: 1.0
References: <Dn07Dx.C6q@midway.uchicago.edu>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 35

Well, well..

Start by using two pointers; ptr1 & ptr2.

Implement the following algorithm:
*Send ptr1 forward one step
*Check for equality.
*Send both ptr1 and ptr2 forward one step.
*Check for equality.
Loop

Incidentally this was a question on an exam
a couple of years ago. (I'm also a student at
RIT, the Royal Institute of Technology in
Stockholm, Sweden) I implemented a more
complicated version of the above scheme that
used an array (fixed size) of pointers, each
with a step-size. I didn't get any points
because the guy who corrected my exam didn't
understand what I was trying to do.
Ah, well... that's life.

Btw. I'm curious if an algorithm using an
array of different step-size pointers would
(on average) find the cycle faster than the
two-pointer method. That was the purpose of
my algorithm, but I never got around to try
and prove it. (never liked Ordo that much :))

/Joachim

-- 
Reporter <to Mahatma Gandhi>: Mr. Gandhi, what do you think
	of Western civilization ?
Gandhi: I think it would be a good idea.

