Floyd algorithm (Turtle and Rabbit Race algorithm)

Source: Internet
Author: User
Floyd algorithm (Turtle and Rabbit Race algorithm)

Note that this algorithm is used to determine the graph of a chain + a ring, the length of the ring or the junction of the ring with the chain, so this Floyd non-Floyd (although all is a person to think out).

(The picture is not mine)

If only the length of the ring is required, just let H and t meet, then let H run a lap, and calculate the number of steps on the line.

What if we want to figure out the junction point of the chain and the ring? First, the pointer H and T start at the same time from S, the speed one is 2, one is 1 (don't care about the details). When T goes to the junction of the chain and the ring, the distance to the right of HT is equal to the distance of St. The distance of St is x, the HT distance on the left is Y, then the length of the ring is x+y. Now let H and t go on until m intersects, so the distance to the TM on the right is obviously Y. Since the length of the ring is X+y, the TM length on the left is X. Now let the two equal-speed point one in S, one in M, while walking, will meet at T, so the junction point on the request.

Because Codevs and double 叒 Corporation on not go, so temporarily no problem.

Floyd algorithm (Turtle and Rabbit Race algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.