Click here to Skip to main content
15,913,941 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Timetable algorithm Pin
Dexterus11-Mar-08 4:14
Dexterus11-Mar-08 4:14 
GeneralRe: Timetable algorithm Pin
Dan Neely11-Mar-08 5:41
Dan Neely11-Mar-08 5:41 
GeneralRe: Timetable algorithm Pin
Paul Conrad12-Mar-08 5:36
professionalPaul Conrad12-Mar-08 5:36 
QuestionMath + F# = ? Pin
Bassam Abdul-Baki4-Mar-08 8:23
professionalBassam Abdul-Baki4-Mar-08 8:23 
AnswerRe: Math + F# = ? Pin
Paul Conrad7-Mar-08 11:13
professionalPaul Conrad7-Mar-08 11:13 
GeneralFloyd's Cycle Detection (Linked List) Pin
User 238229224-Feb-08 6:45
User 238229224-Feb-08 6:45 
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
User 238229227-Feb-08 2:43
User 238229227-Feb-08 2:43 
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
Alan Balkany28-Feb-08 4:52
Alan Balkany28-Feb-08 4:52 
OK, I'll give it a shot. The basic algorithm moves two pointers through a linked list. One pointer takes single steps and the other takes double steps. If there's a cycle, the two pointers will eventually point to the same node. The algorithm doesn't tell where the loop is.

Here's my extension to Floyd's Algorithm. You mentioned O(n^2) so I assume time is more important to you than space.

1. Count the number of times the single-step pointer is advanced. When the loop is detected you know its length is definitely less than or equal to this counter.

2. When the loop is detected, you know the node both pointers reference is definitely in the loop. Save this pointer.

3. At this point create a hash table big enough to hold pointers to all nodes in the loop. (You can do this because the counter gives you an upper bound on the loop size.)

4. Continue chaining through the loop in single steps. Add each node pointer to the hash table. Compare each node pointer to the saved pointer. When you find a match, you know the whole loop is in the hash table.

5. Go back to the start of the list, and chain through it again, checking if each node pointer is in the hash table. When this happens, you've identified where the cycle begins.

Alternative: If you have a tiny loop at the end of a very very long linked list and you want to optimize space, after Step 2 go through the loop again and count the number of nodes in the loop. The hash table only has to hold this number of pointers, which is much smaller than the count from Step 1. (It will be a bit slower because you have to traverse the loop an extra time to count its length.)

The running time is O(n). We can call this the Floyd-Balkany Algorithm.
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
Luc Pattyn29-Feb-08 10:40
sitebuilderLuc Pattyn29-Feb-08 10:40 
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
Alan Balkany29-Feb-08 10:44
Alan Balkany29-Feb-08 10:44 
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
Luc Pattyn29-Feb-08 10:54
sitebuilderLuc Pattyn29-Feb-08 10:54 
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
User 238229229-Feb-08 20:25
User 238229229-Feb-08 20:25 
GeneralRe: Floyd's Cycle Detection (Linked List) Pin
Luc Pattyn1-Mar-08 0:43
sitebuilderLuc Pattyn1-Mar-08 0:43 
GeneralMaths Homework - My boys (not mine - honest!!) [modified] Pin
Malcolm Smart24-Feb-08 2:18
Malcolm Smart24-Feb-08 2:18 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Giorgi Dalakishvili24-Feb-08 3:48
mentorGiorgi Dalakishvili24-Feb-08 3:48 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Luc Pattyn24-Feb-08 4:58
sitebuilderLuc Pattyn24-Feb-08 4:58 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Malcolm Smart24-Feb-08 5:34
Malcolm Smart24-Feb-08 5:34 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Luc Pattyn24-Feb-08 5:45
sitebuilderLuc Pattyn24-Feb-08 5:45 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Malcolm Smart24-Feb-08 6:49
Malcolm Smart24-Feb-08 6:49 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Luc Pattyn24-Feb-08 6:52
sitebuilderLuc Pattyn24-Feb-08 6:52 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Malcolm Smart24-Feb-08 7:07
Malcolm Smart24-Feb-08 7:07 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
Luc Pattyn24-Feb-08 7:19
sitebuilderLuc Pattyn24-Feb-08 7:19 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
kinar12-Mar-08 19:43
kinar12-Mar-08 19:43 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
SilimSayo20-Mar-08 4:02
SilimSayo20-Mar-08 4:02 
GeneralRe: Maths Homework - My boys (not mine - honest!!) Pin
kinar20-Mar-08 12:28
kinar20-Mar-08 12:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.