Java Program to Implement Tarjan Algorithm

This is a Java Program to Implement Tarjan Algorithm. Tarjan Algorithm is used for finding all strongly connected components in a graph.

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

  1. /**
  2.  *     Java Program to Implement Tarjan Algorithm
  3.  **/
  4.  
  5. import java.util.*;
  6.  
  7. /** class Tarjan **/
  8. class Tarjan
  9. {
  10.     /** number of vertices **/
  11.     private int V;    
  12.     /** preorder number counter **/
  13.     private int preCount;
  14.     /** low number of v **/
  15.     private int[] low;
  16.     /** to check if v is visited **/
  17.     private boolean[] visited;      
  18.     /** to store given graph **/
  19.     private List<Integer>[] graph;
  20.     /** to store all scc **/
  21.     private List<List<Integer>> sccComp;
  22.     private Stack<Integer> stack;
  23.  
  24.     /** function to get all strongly connected components **/
  25.     public List<List<Integer>> getSCComponents(List<Integer>[] graph) 
  26.     {
  27.         V = graph.length;
  28.         this.graph = graph;
  29.         low = new int[V];
  30.         visited = new boolean[V];
  31.         stack = new Stack<Integer>();
  32.         sccComp = new ArrayList<>();
  33.  
  34.         for (int v = 0; v < V; v++)
  35.               if (!visited[v])
  36.                 dfs(v);
  37.  
  38.         return sccComp;
  39.     }
  40.     /** function dfs **/
  41.     public void dfs(int v) 
  42.     {
  43.         low[v] = preCount++;
  44.         visited[v] = true;
  45.         stack.push(v);
  46.         int min = low[v];
  47.         for (int w : graph[v]) 
  48.         {
  49.             if (!visited[w])
  50.                 dfs(w);
  51.             if (low[w] < min) 
  52.                 min = low[w];
  53.         }
  54.         if (min < low[v]) 
  55.         { 
  56.             low[v] = min; 
  57.             return; 
  58.         }        
  59.         List<Integer> component = new ArrayList<Integer>();
  60.         int w;
  61.         do
  62.         {
  63.             w = stack.pop();
  64.             component.add(w);
  65.             low[w] = V;                
  66.         } while (w != v);
  67.         sccComp.add(component);        
  68.     }    
  69.     /** main **/
  70.     public static void main(String[] args)
  71.     {    
  72.         Scanner scan = new Scanner(System.in);
  73.         System.out.println("Tarjan algorithm Test\n");
  74.         System.out.println("Enter number of Vertices");
  75.         /** number of vertices **/
  76.         int V = scan.nextInt();
  77.  
  78.         /** make graph **/
  79.         List<Integer>[] g = new List[V];        
  80.         for (int i = 0; i < V; i++)
  81.             g[i] = new ArrayList<Integer>();        
  82.         /** accpet all edges **/
  83.         System.out.println("\nEnter number of edges");
  84.         int E = scan.nextInt();
  85.         /** all edges **/
  86.         System.out.println("Enter "+ E +" x, y coordinates");
  87.         for (int i = 0; i < E; i++)
  88.         {
  89.             int x = scan.nextInt();
  90.             int y = scan.nextInt();
  91.             g[x].add(y);
  92.         }
  93.  
  94.         Tarjan t = new Tarjan();        
  95.         System.out.println("\nSCC : ");
  96.         /** print all strongly connected components **/
  97.         List<List<Integer>> scComponents = t.getSCComponents(g);
  98.            System.out.println(scComponents);        
  99.     }    
  100. }

Tarjan algorithm Test
 
Enter number of Vertices
8
 
Enter number of edges
14
Enter 14 x, y coordinates
0 1
1 2
2 3
3 2
3 7
7 3
2 6
7 6
5 6
6 5
1 5
4 5
4 0
1 4
 
SCC :
[[5, 6], [7, 3, 2], [4, 1, 0]]

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.