Gabow 算法是一种缩放算法,旨在通过最初仅考虑每个相关输入值(例如边缘权重)的最高位来解决问题。然后它通过查看两个最高位来细化初始解决方案。它逐渐查看越来越多的高阶位,在每次迭代中细化解,直到检查所有位并计算出正确的解。
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
程序:
- 在方法getSCComponents()或它命名为每CONV烯ience
- 最初,我们通过使用Java集合中的列表声明了一个图,并且
- 然后我们通过应用dfs() 方法找到强连通分量
- 在上面的方法中,我们实现了一种深度优先搜索方法来考虑图中的每一位,同时考虑预排序和重计数。
- 在 main 方法中,我们将顶点数和边数添加到数组列表中,然后打印 getSCComponents() 方法以打印出图形给定值的强连通分量。
- 打印并显示表示图中没有强连通分量的输出
算法:
- 仅使用权重的最高有效位的从 v0 到 v 的最小权重路径的权重是从 v0 到 v 的最小权重路径的权重的近似值。
- 然后,增量地引入额外的权重位来改进我们对最小权重路径的近似。
- 在每次迭代中,对于某些边 (u, v),我们将近似距离 u.dist – v.dist 的差异定义为 (u, v) 上的电位。
- 我们将边的成本定义为它在某些迭代中的细化权重加上它的潜力:
- li(u, v) = wi(u, v) + u.dist − v.dist。
- 由于成本与路径望远镜的总和,这些成本保留了图中的最小权重路径。
- 我们保证边的成本总是非负的。
- 我们可以在成本值图上重复找到最小权重路径。
例子:
Java
// Java Program to Implement Gabow Scaling Algorithm
// Impoting 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 componenet 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();
// Accpeting 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);
}
}
输出
Strongly Connected Components for give edges :
[[5, 6], [7, 3, 2], [4, 1, 0]]
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live