Java Program to Implement Floyd Cycle Algorithm

This is a Java Program to Implement Floyd Cycle Algorithm. Cycle detection is the algorithmic problem of finding a cycle in a sequence of iterated function values. Floyd’s cycle-finding algorithm, also called the “tortoise and the hare” algorithm, is a pointer algorithm that uses only two pointers, which move through the sequence at different speeds.

Here is the source code of the Java Program to Implement Floyd Cycle Algorithm . The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to Implement Floyd Cycle Algorithm 
 **/
 
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
 
/** Class FloydCycle **/
public class FloydCycle
{
    private List<Integer> func;
    private int lam, mu; 
    /** Constructor **/
    public FloydCycle(List<Integer> list, int x0)
    {
        func = list;
        /** print sequence **/
        printSequence(x0);
        /** find cycle **/
        findCycle(x0);
        /** display results **/
        display();
    }
    /** function to find cycle **/
    private void findCycle(int x0)
    {
        int tortoise = f(x0);    
        int hare = f(f(x0));    
        while (tortoise != hare)
        {
            tortoise = f(tortoise);
            hare = f(f(hare));
        }
        int mu = 0;
        tortoise = x0;
        while (tortoise != hare)
        {
            tortoise = f(tortoise);
            hare = f(hare);
            mu += 1;
        }
        int lam = 1;
        hare = f(tortoise);
        while (tortoise != hare)
        {
            hare = f(hare);
            lam += 1;
        }
        this.lam = lam;
        this.mu = mu;
    }
    /** function to return value of function f(x) **/
    private int f(int p)
    {
        return func.get(p);
    }
    /** function to print first n sequence **/
    public void printSequence(int x0)
    {
        int n = func.size();
        int tempx = x0;
        System.out.print("\nFirst "+ n +" elements in sequence : \n"+ tempx);
        for (int i = 0; i < n; i++)
        {
            tempx = f(tempx);
            System.out.print(" "+ tempx);
        }
        System.out.println();        
    }
    /** function to display results **/
    public void display()
    {
        System.out.println("\nLength of cycle : "+ lam);
        System.out.println("Position : "+ (mu + 1));
    }
    /** Main function **/
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Floyd Cycle Algorithm Test\n");
        System.out.println("Enter size of list");
        int n = scan.nextInt();
        List<Integer> list = new ArrayList<Integer>();
        System.out.println("\nEnter f(x)");
        for (int i = 0; i < n; i++)
            list.add(scan.nextInt());
        System.out.println("\nEnter x0");
        int x0 = scan.nextInt();
        FloydCycle fc = new FloydCycle(list, x0);
    }
}
Floyd Cycle Algorithm Test
 
Enter size of list
9
 
Enter f(x)
6 6 0 1 4 3 3 4 0
 
Enter x0
2
 
First 9 elements in sequence :
2 0 6 3 1 6 3 1 6 3
 
Length of cycle : 3
Position : 3

Related posts:

Adding a Newline Character to a String in Java
Call Methods at Runtime Using Java Reflection
Java Program to Implement SynchronosQueue API
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement Euler Circuit Problem
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Login For a Spring Web App – Error Handling and Localization
Java Program to Implement Sieve Of Eratosthenes
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Sending Emails with Java
Java Program to Represent Graph Using Linked List
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Sorted List
An Example of Load Balancing with Zuul and Eureka
Thao tác với tập tin và thư mục trong Java
Array to String Conversions
Java Program to Represent Graph Using Adjacency List
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Jackson Ignore Properties on Marshalling
JUnit 5 for Kotlin Developers
An Introduction to Java.util.Hashtable Class
Java Program to Check Cycle in a Graph using Topological Sort
Spring Boot Integration Testing with Embedded MongoDB
Display Auto-Configuration Report in Spring Boot
So sánh ArrayList và LinkedList trong Java
Spring Boot - OAuth2 with JWT
Java Program to Find All Pairs Shortest Path
Merging Streams in Java