不完全浅析Tarjan求强连通分量(SCC)

本文介绍了一种使用Tarjan算法寻找图中的强连通分量的方法。通过深度优先搜索(DFS)来标记每个节点的时间戳和可达最浅祖先,进而找到所有强连通子图。

定义dfn[]为每个点的时间戳(搜索次序),low[]为每个点的所能追溯到的,最低dfn的祖先的dfn,也就是最浅祖先
证明我真的不会,只能自己YY,不确保说的每句话都是正确的,如果我的分析有错误请一定要指出来!!!
先走dfs。一个有SCC的图一定有环,所以当搜索到一个已经到过的点时,我们就找到了一个环,此时这个环上所有的点一定都强联通(就好比地球是圆的,所以一直沿某个方向是可以回到起点的),这个环就是一个强联通子图,我们最后找的就是一个极大的环。
用栈保存读入的点,每遇到一个dfn==low的点,就说明找到了这个环的祖先,将这个祖先以及后于其加入栈的点弹出来,并且标记flag[弹出的点]=false,
flag用来确保把某个环剔出去,这样在求其他的环时就不会因为环上某条指向应该被删除的某个环的单向边而被影响。

void tarjan(int x) {
	dfn[x] = low[x] = ++count;
	s[++top] = x;
	flag[x] = 1;
	for(int i=last[x]; i; i=e[i].next){
		int v = e[i].to;
			if(!dfn[v]) {
				tarjan(v);
				if(low[x] > low[v]) 
					low[x] = low[v];
			}
			else if(dfn[v] < low[x] && flag[v]) 
				low[x] = dfn[v];
			
	}
	if(dfn[x] == low[x]) {
		int j, k = 0;
		sccnum++;
		do{
			j = s[top--];
			flag[j] = 0;
			k++;
		}while(j!=x);
	}
}
### Tarjan算法实现强连通分量的详细步骤 Tarjan算法是一种基于深度优先搜索(DFS)的方法,用于寻找有向图中的强连通分量。以下是其实现的核心逻辑: #### 1. 定义关键变量 - **dfn[u]**:表示节点 `u` 的访问时间戳,在 DFS 遍历时记录每个节点第一次被访问的时间。 - **low[u]**:表示节点 `u` 能够回溯到的最早祖先节点的访问时间戳。 这些变量帮助我们判断当前子图是否构成一个强连通分量[^1]。 #### 2. 构建辅助数据结构 - 使用栈来存储当前路径上的节点,当发现某个节点满足条件时,可以弹出栈中属于同一强连通分量的所有节点。 #### 3. 深度优先搜索过程 在 DFS 过程中,对于每一个未访问过的节点 `u`: - 初始化 `dfn[u]` 和 `low[u]` 均为当前时间戳。 - 将节点 `u` 入栈并标记为已访问。 - 对于 `u` 的所有邻接点 `v`: - 如果 `v` 尚未被访问,则递归调用 DFS 并更新 `low[u] = min(low[u], low[v])`。 - 如果 `v` 已经在栈中,则说明存在一条指向祖先的反向边,此时应更新 `low[u] = min(low[u], dfn[v])`。 #### 4. 判断强连通分量 如果 `dfn[u] == low[u]`,则表明以 `u` 为根的子树构成了一个新的强连通分量。此时可以从栈中依次弹出节点直到弹出 `u`,并将它们作为一个独立的强连通分量保存下来[^2]。 --- ### Java代码示例 以下是一个完整的 Java 实现示例: ```java import java.util.*; public class TarjanSCC { private static int index; private static Stack<Integer> stack; public static List<List<Integer>> tarjan(List<List<Integer>> graph, int n) { int[] dfn = new int[n]; Arrays.fill(dfn, -1); int[] low = new int[n]; boolean[] onStack = new boolean[n]; stack = new Stack<>(); List<List<Integer>> sccs = new ArrayList<>(); for (int i = 0; i < n; i++) { if (dfn[i] == -1) { dfs(i, graph, dfn, low, onStack, sccs); } } return sccs; } private static void dfs(int u, List<List<Integer>> graph, int[] dfn, int[] low, boolean[] onStack, List<List<Integer>> sccs) { dfn[u] = low[u] = ++index; stack.push(u); onStack[u] = true; for (int v : graph.get(u)) { if (dfn[v] == -1) { dfs(v, graph, dfn, low, onStack, sccs); low[u] = Math.min(low[u], low[v]); } else if (onStack[v]) { low[u] = Math.min(low[u], dfn[v]); } } if (low[u] == dfn[u]) { List<Integer> scc = new ArrayList<>(); while (true) { int node = stack.pop(); onStack[node] = false; scc.add(node); if (node == u) break; } sccs.add(scc); } } public static void main(String[] args) { int n = 7; List<List<Integer>> graph = new ArrayList<>(n); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } graph.get(0).add(1); graph.get(1).add(2); graph.get(2).add(0); graph.get(2).add(3); graph.get(3).add(4); graph.get(4).add(5); graph.get(5).add(3); graph.get(5).add(6); List<List<Integer>> result = tarjan(graph, n); System.out.println("Strongly Connected Components:"); for (List<Integer> component : result) { System.out.println(component); } } } ``` --- ### Python代码示例 Python 版本的实现如下所示: ```python def tarjan_scc(graph): index_counter = [0] stack = [] on_stack = {} indices = {} lows = {} sccs = [] def strongconnect(vertex): indices[vertex] = index_counter[0] lows[vertex] = index_counter[0] index_counter[0] += 1 stack.append(vertex) on_stack[vertex] = True for neighbor in graph[vertex]: if neighbor not in indices: strongconnect(neighbor) lows[vertex] = min(lows[vertex], lows[neighbor]) elif on_stack[neighbor]: lows[vertex] = min(lows[vertex], indices[neighbor]) if lows[vertex] == indices[vertex]: scc = set() while True: w = stack.pop() on_stack[w] = False scc.add(w) if w == vertex: break sccs.append(scc) for vertex in graph.keys(): if vertex not in indices: strongconnect(vertex) return sccs if __name__ == "__main__": graph = {0: [1], 1: [2], 2: [0, 3], 3: [4], 4: [5], 5: [3, 6], 6: []} components = tarjan_scc(graph) print("Strongly Connected Components:") for comp in components: print(comp) ``` --- ### 性能优势 Tarjan算法具有线性时间复杂度 \( O(V + E) \),其中 \( V \) 是顶点数量,\( E \) 是边的数量。这种高效性能使其成为解决大规模图问题的理想工具[^4]。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值