Java Program to Detect Cycle in a Graph using Topological Sort

«
»
This Java program,to perform the topological Sort on a given graph by the DFS method.The topological sort is performed on a directed acyclic graph.

Here is the source code of the Java program to check for cycle in topological sort. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3. import java.util.Stack;
  4.  
  5. public class TopoCycle
  6. {
  7.     private Stack<Integer> stack;
  8.  
  9.     public TopoCycle() 
  10.     {
  11.         stack = new Stack<Integer>();
  12.     }
  13.  
  14.     public boolean checkCycle(int adjacency_matrix[][], int source)
  15.     {
  16.         int number_of_nodes = adjacency_matrix[source].length - 1;
  17.         int[] topological_sort = new int [number_of_nodes + 1];
  18.         int pos = 1;
  19.         int j;
  20.         boolean cycle = false;
  21.  
  22.  
  23.         int visited[] = new int[number_of_nodes + 1];
  24.         int element = source;
  25.         int i = source;
  26.         visited[source] = 1;
  27.         stack.push(source);
  28.  
  29.  
  30.         while (!stack.isEmpty())
  31.         {
  32.             element = stack.peek();
  33.             while (i <= number_of_nodes)
  34.             {
  35.                 if (adjacency_matrix[element][i] == 1 && visited[i] == 1)
  36.                 {
  37.                     if (stack.contains(i))
  38.                     {
  39.                         System.out.println("The Graph Contains a cycle");
  40.                         cycle = true; 
  41.                         return cycle;
  42.                     }
  43.                 }
  44.                 if (adjacency_matrix[element][i] == 1 && visited[i] == 0)
  45.                 {
  46.                     stack.push(i);
  47.                     visited[i] = 1;
  48.                     element = i;
  49.                     i = 1;
  50.                     continue;
  51.                 }
  52.                 i++;
  53.             }
  54.             j = stack.pop();	
  55.             topological_sort[pos++] = j;
  56.             i = ++j;
  57.         }
  58.         System.out.println("The Graph does not Contain cycle");
  59.         return cycle;
  60.     }	
  61.  
  62.     public static void main(String...arg)
  63.     {
  64.         int number_no_nodes, source;
  65.         Scanner scanner = null;
  66.         try 
  67.         {
  68.             System.out.println("Enter the number of nodes in the graph");
  69.             scanner = new Scanner(System.in);
  70.             number_no_nodes = scanner.nextInt();
  71.  
  72.             int adjacency_matrix[][] = new int[number_no_nodes + 1][number_no_nodes + 1];
  73.             System.out.println("Enter the adjacency matrix");
  74. 	    for (int i = 1; i <= number_no_nodes; i++)
  75.                 for (int j = 1; j <= number_no_nodes; j++)
  76.                     adjacency_matrix[i][j] = scanner.nextInt();
  77.  
  78.             System.out.println("Enter the source for the graph");
  79.             source = scanner.nextInt();
  80.  
  81. 	    TopoCycle topoCycle = new TopoCycle();
  82.             topoCycle.checkCycle(adjacency_matrix, source);
  83.  
  84.         }catch(InputMismatchException inputMismatch)
  85.         {
  86.             System.out.println("Wrong Input format");
  87.         }
  88.         scanner.close();
  89.     }
  90. }


$javac TopoCycle.java
$java TopoCycle
Enter the number of nodes in the graph
5
Enter the adjacency matrix
0 1 0 1 0
0 0 1 0 0 
0 0 0 0 1
0 1 0 0 1
0 0 0 1 0 
Enter the source for the graph
1
The Graph contains a cycle

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.