C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 11th, 2004, 09:56 PM
Sebatian Sebatian is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 Sebatian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cycle detection

I was wondering if anyone knew any good cycle detection algorithims for an array of linked lists. If anyone knows any good links or code (even pseudocode) it would help me out tons! I need an algorithim that will detect a cycle not just in a element of the array (its not possible in my situation) but the array as a whole.

for something like this...
ListType *Wait_For;
ÏWait_For= new ListType[Max];



Reply With Quote
  #2  
Old August 12th, 2004, 02:56 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Hmm...not something I've particularly looked at but my thoughts would be to do it as follows.

Start at the first element of the array and mark the first element of the list. Follow its next pointer and mark that and so on. When you reach the end of a linked list go to the next element in the array and continue marking.

If you reach the last element of the array and the last element of the linked list then there are no cycles. If you encounter a marked node before that then there is a cycle.

No idea if this works or how well since its early but it should give you a good starting point if nothing else,

-KM-

Reply With Quote
  #3  
Old August 19th, 2004, 03:27 AM
mravi mravi is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 mravi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up Here a quick and efficient solution

A simple technique for detecting infinite loops inside lists, symlinks, state machines, etc. For example, to detect a loop inside a linked list:
  • make a pointer to the start of the list and call it the hare
  • make a pointer to the start of the list and call it the tortoise
  • advance the hare by two and the tortoise by one for every iteration
  • there's a loop if the tortoise and the hare meet
  • there's no loop if the hare reaches the end
This algorithm is particularly interesting because it is O(n) time and O(1) space, which appears to be optimal; more obvious algorithms are O(n) space. This problem is called Tortoise and Hare problem and this link will be quite useful for you. http://c2.com/cgi/wiki?TortoiseAndHare

Reply With Quote
  #4  
Old September 28th, 2004, 04:44 PM
kumaruhtx kumaruhtx is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 1 kumaruhtx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation can u please make it clear enough

can u please the hare and tortoise solution more clear with an example



Quote:
Originally Posted by mravi
A simple technique for detecting infinite loops inside lists, symlinks, state machines, etc. For example, to detect a loop inside a linked list:
  • make a pointer to the start of the list and call it the hare
  • make a pointer to the start of the list and call it the tortoise
  • advance the hare by two and the tortoise by one for every iteration
  • there's a loop if the tortoise and the hare meet
  • there's no loop if the hare reaches the end
This algorithm is particularly interesting because it is O(n) time and O(1) space, which appears to be optimal; more obvious algorithms are O(n) space. This problem is called Tortoise and Hare problem and this link will be quite useful for you. http://c2.com/cgi/wiki?TortoiseAndHare

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Cycle detection


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT