# Java Program to Implement of Gabow Scaling Algorithm

• Last Updated : 15 Jul, 2021

Gabow’s Algorithm is a scaling algorithm that aims in solving a problem by initially considering only the highest order bit of each relevant input value (such as an edge weight). Then it refines the initial solution by looking at the two highest-order bits. It progressively looks at more and more high-order bits, refining the solution in each iteration, until it has examined all bits and computed the correct solution.

Basically, Gabow’s scaling algorithm is a scaling algorithm that solves a problem by initially considering only the highest order bit of each input value as an edge weight, then it refines the initial solution by looking at two highest-order bits and then refines the solution iteratively until it has visited all bits(in the graph) and computes the correct solution

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Procedure:

1. In method getSCComponents() or name it as per convenience
• Initially, we declared a graph by using lists in java collections and
• Then we find strongly connected components by applying dfs() method
2. In the above method, we implemented a depth-first search approach to consider each and every bit in the graph considering preorder and recount.
3. In the main method, we take no of vertices and no of edges and add them to an array list, and we print the getSCComponents() method to print out strongly connected components for the given values of the graph.
4. Print and display the output representing no strongly connected components in the graph

Algorithm:

1. The weight of the minimum weight path from v0 to v using just the most significant bit of the weight is an approximation for the weight of the minimum weight path from v0 to v.
2. Then, Incrementally introduce additional bits of the weight to refine our approximation of the minimum weight paths.
3. At each iteration, for some edge (u, v) we define the difference in approximate distances u.dist − v.dist to be the potential across (u, v).
4. We define the cost of an edge to be its refined weight at some iteration plus the potential across it:
• li(u, v) = wi(u, v) + u.dist − v.dist.
5. Since the sum of costs along with path telescopes, these costs preserve the minimum weight paths in the graph.
6. We guarantee that the cost of an edge is always non-negative.
7. We can repeatedly find minimum weight paths on graphs of cost value.

Example:

## Java

 `// Java Program to Implement Gabow Scaling Algorithm` `// Importing input output classes``import` `java.io.*;``import` `java.util.*;` `// Main Class``// Implementation of Gabow Scaling Algorithm``public` `class` `GFG {` `    ``// Declaring variables` `    ``// 1. Number of vertices``    ``private` `int` `V;``    ``// 2. Preorder number counter``    ``private` `int` `preCount;``    ``private` `int``[] preorder;` `    ``// 3. To check if v is visited``    ``private` `boolean``[] visited;` `    ``// 4. To check strong component containing v``    ``private` `boolean``[] chk;` `    ``// 5. To store given graph``    ``private` `List[] graph;` `    ``// 6. To store all Integer elements``    ``private` `List > sccComp;``    ``private` `Stack stack1;``    ``private` `Stack stack2;` `    ``// Method 1``    ``// To get all strongly connected components``    ``public` `List >``        ``getSCComponents(List[] graph)``    ``{``        ``V = graph.length;``        ``this``.graph = graph;``        ``preorder = ``new` `int``[V];``        ``chk = ``new` `boolean``[V];``        ``visited = ``new` `boolean``[V];``        ``stack1 = ``new` `Stack();``        ``stack2 = ``new` `Stack();``        ``sccComp = ``new` `ArrayList<>();` `        ``for` `(``int` `v = ``0``; v < V; v++)``            ``if` `(!visited[v])``                ``dfs(v);` `        ``return` `sccComp;``    ``}` `    ``// Method 2``    ``// Depth first search algorithm``    ``public` `void` `dfs(``int` `v)``    ``{``        ``preorder[v] = preCount++;``        ``visited[v] = ``true``;``        ``stack1.push(v);``        ``stack2.push(v);` `        ``for` `(``int` `w : graph[v]) {``            ``if` `(!visited[w])``                ``dfs(w);``            ``else` `if` `(!chk[w])``                ``while` `(preorder[stack2.peek()]``                       ``> preorder[w])``                    ``stack2.pop();``        ``}``        ``if` `(stack2.peek() == v) {``            ``stack2.pop();``            ``List component``                ``= ``new` `ArrayList();``            ``int` `w;``            ``do` `{``                ``w = stack1.pop();``                ``component.add(w);``                ``chk[w] = ``true``;``            ``} ``while` `(w != v);``            ``sccComp.add(component);``        ``}``    ``}` `    ``// Method 3``    ``// Main driver method``    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// Declaring and initializing variable to``        ``// number of vertices``        ``int` `V = ``8``;` `        ``// Creating a graph``        ``@SuppressWarnings``(``"unchecked"``)``        ``List[] g = ``new` `List[V];``        ``for` `(``int` `i = ``0``; i < V; i++)``            ``g[i] = ``new` `ArrayList();` `        ``// Accepting all edges``        ``int` `E = ``14``;` `        ``// Custom integers inputs for all edges``        ``int``[] x = ``new` `int``[] { ``0``, ``1``, ``2``, ``3``, ``3``, ``7``, ``2``,``                              ``7``, ``5``, ``6``, ``1``, ``4``, ``4``, ``1` `};``        ``int``[] y = ``new` `int``[] { ``1``, ``2``, ``3``, ``2``, ``7``, ``3``, ``6``,``                              ``6``, ``6``, ``5``, ``5``, ``5``, ``0``, ``4` `};` `        ``for` `(``int` `i = ``0``; i < E; i++) {``            ``int` `x1 = x[i];``            ``int` `y1 = y[i];``            ``g[x1].add(y1);``        ``}` `        ``// Creating an object of main class i the main()``        ``// method``        ``GFG gab = ``new` `GFG();` `        ``// Display message only``        ``System.out.println(``            ``"\nStrongly Connected Components for given edges : "``);` `        ``// now, printing all the strongly connected``        ``// components``        ``List > scComponents``            ``= gab.getSCComponents(g);``        ``System.out.println(scComponents);``    ``}``}`
Output
```Strongly Connected Components for give edges :
[[5, 6], [7, 3, 2], [4, 1, 0]]```

My Personal Notes arrow_drop_up