Java
Java

 

 

This Java program demonstrates how to detect cycles in a directed graph using the Depth First Search (DFS) algorithm. Cycle detection is important for determining if certain processes, such as task scheduling or package dependency resolutions, can proceed without conflicts.

Java Code

import java.util.*;

class Graph {
    private final int V;   // No. of vertices
    private final List<List> adj; // Adjacency list

    Graph(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; ++i)
            adj.add(new ArrayList<>());
    }

    void addEdge(int source, int dest) {
        adj.get(source).add(dest); // Add edge from source to destination
    }

    boolean isCyclicUtil(int i, boolean[] visited, boolean[] recStack) {
        if (recStack[i])
            return true;
        if (visited[i])
            return false;

        visited[i] = true;
        recStack[i] = true;
        List children = adj.get(i);

        for (Integer c: children)
            if (isCyclicUtil(c, visited, recStack))
                return true;

        recStack[i] = false;
        return false;
    }

    boolean isCyclic() {
        boolean[] visited = new boolean[V];
        boolean[] recStack = new boolean[V];
        
        for (int i = 0; i < V; i++)
            if (isCyclicUtil(i, visited, recStack))
                return true;

        return false;
    }
}

public class CycleDetection {
    public static void main(String[] args) {
        Graph graph = new Graph(4);
        graph.addEdge(0, 1);
        graph.addEdge(0, 2);
        graph.addEdge(1, 2);
        graph.addEdge(2, 0);
        graph.addEdge(2, 3);
        graph.addEdge(3, 3);

        if (graph.isCyclic())
            System.out.println("Graph contains a cycle");
        else
            System.out.println("Graph does not contain a cycle");
    }
}

Explanation of the Code

The program is structured as follows:

  • Graph Class: This class encapsulates the representation of a directed graph using an adjacency list. Methods include adding edges and checking for cycles.
  • addEdge Method: Adds a directed edge from a source vertex to a destination vertex.
  • isCyclic Method: Public method that initializes necessary arrays and checks for cycles starting from each vertex not yet visited.
  • isCyclicUtil Method: A recursive method that uses a depth-first search approach to explore all possible paths from the current vertex. It uses a recursion stack to keep track of vertices that are in the current recursion stack of the DFS. If a vertex is encountered again in the current path, a cycle is detected.

This method of cycle detection is efficient and fundamental in ensuring that directed graphs used in various computational and real-world applications are acyclic before proceeding with tasks that require a cycle-free environment.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)