[algogeeks] Re: cycle in Linked List generalised
@Amit You should me able to find waether there is a loop or not in a LL with any value of m and n. But if you have to find where the loop is, then you have to chose m and n such that gcd(m,n)=1, as Dave said. Consier the example below 1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26 and 26 points to 3. With this and m=4 and n=8. You will be able to detect loop but not the location where loop is (at node 3) as the to pointers will keep jumping by 4 and 8 steps and gcd94,8)!=1 Let me know your views. On Jul 7, 2:34 pm, jaladhi dave wrote: > There are more efficient ways of doing this. > Refer wikihttp://en.wikipedia.org/wiki/Cycle_detection#Brent.27s_algorithm > > > > On Wed, Jul 7, 2010 at 9:16 AM, Dave wrote: > > @Anand. You've described one way to do it, and maybe the most > > efficient way, but not the only way. > > > Dave > > > On Jul 6, 8:42 pm, Anand wrote: > > > you increment slow pointer by 1 and fast pointer by 2 to find a loop in > > > Linked list. > > > > On Mon, Jul 5, 2010 at 9:02 PM, amit wrote: > > > > Consider the general fast and slow pointer strategy to detect loop in > > > > a LL. > > > > Now , Suppose we move the slowPtr 'm' nodes at a time and move the > > > > fastPtr 'n' nodes at a time. What are the constraints on 'm' and 'n' > > > > so that we that we are able to find whether the list is circular or > > > > not? > > > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "Algorithm Geeks" group. > > > > To post to this group, send email to algoge...@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > algogeeks+unsubscr...@googlegroups.com > > > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/algogeeks?hl=en.-Hide quoted text - > > > > - Show quoted text - > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algoge...@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com > > . > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: cycle in Linked List generalised
There are more efficient ways of doing this. Refer wiki http://en.wikipedia.org/wiki/Cycle_detection#Brent.27s_algorithm On Wed, Jul 7, 2010 at 9:16 AM, Dave wrote: > @Anand. You've described one way to do it, and maybe the most > efficient way, but not the only way. > > Dave > > On Jul 6, 8:42 pm, Anand wrote: > > you increment slow pointer by 1 and fast pointer by 2 to find a loop in > > Linked list. > > > > > > > > On Mon, Jul 5, 2010 at 9:02 PM, amit wrote: > > > Consider the general fast and slow pointer strategy to detect loop in > > > a LL. > > > Now , Suppose we move the slowPtr 'm' nodes at a time and move the > > > fastPtr 'n' nodes at a time. What are the constraints on 'm' and 'n' > > > so that we that we are able to find whether the list is circular or > > > not? > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to algoge...@googlegroups.com. > > > To unsubscribe from this group, send email to > > > algogeeks+unsubscr...@googlegroups.com > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - > > > > - Show quoted text - > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: cycle in Linked List generalised
m!=n -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: cycle in Linked List generalised
Practically speaking, whatever be the value of m and n we can find out the cycle if existing in O(N) time the condition to find the cycle is (m*x) mod N == (n *x) mod N. where x is the number of iterations required, and we can clearly see that whatever be the value of m and n there is always a solution exiting for x, that is N. N here is the length of the cycle. On Wed, Jul 7, 2010 at 9:16 AM, Dave wrote: > @Anand. You've described one way to do it, and maybe the most > efficient way, but not the only way. > > Dave > > On Jul 6, 8:42 pm, Anand wrote: > > you increment slow pointer by 1 and fast pointer by 2 to find a loop in > > Linked list. > > > > > > > > On Mon, Jul 5, 2010 at 9:02 PM, amit wrote: > > > Consider the general fast and slow pointer strategy to detect loop in > > > a LL. > > > Now , Suppose we move the slowPtr 'm' nodes at a time and move the > > > fastPtr 'n' nodes at a time. What are the constraints on 'm' and 'n' > > > so that we that we are able to find whether the list is circular or > > > not? > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "Algorithm Geeks" group. > > > To post to this group, send email to algoge...@googlegroups.com. > > > To unsubscribe from this group, send email to > > > algogeeks+unsubscr...@googlegroups.com > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - > > > > - Show quoted text - > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: cycle in Linked List generalised
@Anand. You've described one way to do it, and maybe the most efficient way, but not the only way. Dave On Jul 6, 8:42 pm, Anand wrote: > you increment slow pointer by 1 and fast pointer by 2 to find a loop in > Linked list. > > > > On Mon, Jul 5, 2010 at 9:02 PM, amit wrote: > > Consider the general fast and slow pointer strategy to detect loop in > > a LL. > > Now , Suppose we move the slowPtr 'm' nodes at a time and move the > > fastPtr 'n' nodes at a time. What are the constraints on 'm' and 'n' > > so that we that we are able to find whether the list is circular or > > not? > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algoge...@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com > > . > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en.- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: cycle in Linked List generalised
gcd(m,n) = 1. Dave On Jul 5, 11:02 pm, amit wrote: > Consider the general fast and slow pointer strategy to detect loop in > a LL. > Now , Suppose we move the slowPtr 'm' nodes at a time and move the > fastPtr 'n' nodes at a time. What are the constraints on 'm' and 'n' > so that we that we are able to find whether the list is circular or > not? -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.