Complete the code to initialize the discovery time for the current node in Tarjan's algorithm.
disc[u] = [1]In Tarjan's algorithm, we assign the current time as the discovery time for node u.
Complete the code to update the low-link value after visiting a neighbor in Tarjan's algorithm.
low[u] = min(low[u], [1])
The low-link value of u is updated with the minimum of its current low-link and the low-link of neighbor v.
Fix the error in the condition that detects a bridge edge in Tarjan's algorithm.
if low[v] [1] disc[u] { // (u, v) is a bridge }
An edge (u, v) is a bridge if the low-link value of v is greater than the discovery time of u.
Fill both blanks to correctly skip the parent node during DFS and update low-link for back edges.
for _, v := range graph[u] { if v == [1] { continue } if disc[v] == -1 { dfs(v, u) low[u] = min(low[u], [2]) } else { low[u] = min(low[u], disc[v]) } }
u instead of parent to skip.We skip the parent node to avoid revisiting it. After DFS on v, update low[u] with low[v].
Fill all three blanks to correctly initialize and run Tarjan's algorithm to find bridges.
func findBridges(graph [][]int) [][]int {
n := len(graph)
disc := make([]int, n)
low := make([]int, n)
for i := 0; i < n; i++ {
disc[i] = [1]
low[i] = [2]
}
time := 0
var bridges [][]int
var dfs func(u, [3] int)
dfs = func(u, parent int) {
// DFS body omitted for brevity
}
for i := 0; i < n; i++ {
if disc[i] == -1 {
dfs(i, -1)
}
}
return bridges
}Discovery and low arrays are initialized to -1 to mark unvisited nodes. The DFS function takes parent as the second argument.
