Analysis of the Modified Tarjan Algorithm for Bridge Finding
The variation of Tarjan's algorithm you've described is indeed an interesting space optimization of the classic approach. Let me analyze its validity and check if it has been documented before.
Understanding the Classic Tarjan Algorithm
In the standard Tarjan algorithm for finding bridges in undirected graphs, two arrays are maintained[1][2][4]:
dfn[u]: Records the order in which nodes are first discovered during DFS
low[u]: Records the lowest discovery time reachable from u through its subtree and at most one back edge
An edge (u,v) is identified as a bridge when low[v] > dfn[u], meaning there's no alternative path from v to u or any ancestor of u[1][4][5].
Your Proposed Variation
Your proposed modification uses only one array (dow[]) instead of two, along with a local variable lfn during recursion. The key aspects are:
- When first visiting node u: Set
dow[u] = ++timer and initialize lfn = dow[u]
- For each neighbor v:
- If v is unvisited: Recurse and update
lfn = min(lfn, dow[v])
- If v is visited: Update
lfn directly
- Bridge detection: An edge (u,v) is a bridge if
dow[u] < dow[v]
- Before returning from recursion: Update
dow[u] = lfn
This approach effectively transforms the dow array to serve dual purposes - initially as discovery time and later as low-link values.
Analysis of Correctness
Your approach appears valid based on the properties of DFS in undirected graphs. The key insight leveraged is that in undirected graphs, if node v is already processed but its DFS call hasn't finished, v must be an ancestor of the current node[2][5][7].
The correctness hinges on:
- Before completing DFS on node u,
dow[u] functions as dfn[u]
- After completing DFS on node u,
dow[u] functions as low[u]
- The bridge condition
dow[u] < dow[v] is equivalent to dfn[u] < low[v] when checking a tree edge from u to v
Has This Been Done Before?
While the search results don't explicitly mention this exact variation with a single array, the concept of optimizing Tarjan's algorithm is well-explored. Tarjan's algorithm has been implemented in various ways with slight modifications across different contexts[2][7][8].
The variation you've described appears to be a valid space optimization. However, I haven't found documentation of this specific implementation in the provided search results. This doesn't mean it hasn't been done before, just that it's not mentioned in the available materials.
Potential Concerns
One potential issue with this approach is readability and maintainability. By having a single array that changes meaning during algorithm execution, the code may be harder for others to understand and modify. Also, the memory savings (O(V) vs O(2V)) may not be significant enough to justify the increased complexity in many practical scenarios.
However, from an algorithmic correctness standpoint, your approach seems valid and leverages the properties of DFS on undirected graphs correctly.