Bird
Raised Fist0
DSA Typescriptprogramming~5 mins

Bridges in Graph Tarjan's Algorithm in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a bridge in a graph?
A bridge is an edge in a graph that, if removed, increases the number of connected components. It means the edge is critical for keeping the graph connected.
Click to reveal answer
intermediate
What is the main idea behind Tarjan's algorithm for finding bridges?
Tarjan's algorithm uses a depth-first search (DFS) to assign discovery times and low values to each vertex. An edge is a bridge if the low value of the connected vertex is greater than the discovery time of the current vertex.
Click to reveal answer
intermediate
In Tarjan's algorithm, what does the 'low' value represent?
The 'low' value of a vertex is the earliest discovery time reachable from that vertex or its descendants by following zero or more tree edges and then possibly one back edge.
Click to reveal answer
advanced
Why do we compare low[v] > disc[u] to find a bridge between u and v?
If low[v] > disc[u], it means vertex v and its descendants cannot reach any ancestor of u without using the edge (u, v). So, removing (u, v) disconnects the graph, making it a bridge.
Click to reveal answer
beginner
What is the time complexity of Tarjan's algorithm for finding bridges?
The time complexity is O(V + E), where V is the number of vertices and E is the number of edges, because it performs a single DFS traversal.
Click to reveal answer
What does a bridge in a graph do?
AConnects two disconnected graphs
BDecreases the number of connected components if removed
CHas no effect on connectivity
DIncreases the number of connected components if removed
In Tarjan's algorithm, what is the purpose of the discovery time array?
ATo count the number of edges
BTo store the degree of each vertex
CTo store the order in which vertices are visited in DFS
DTo mark visited vertices
What does it mean if low[v] <= disc[u] in Tarjan's algorithm?
AEdge (u, v) is not a bridge
BEdge (u, v) is a bridge
CVertex v is not reachable
DVertex u is a leaf
Which traversal method does Tarjan's algorithm use?
ADepth-first search
BDijkstra's algorithm
CBreadth-first search
DPrim's algorithm
What is the time complexity of Tarjan's algorithm for bridges?
AO(E^2)
BO(V + E)
CO(V^2)
DO(log V)
Explain how Tarjan's algorithm finds bridges in a graph using discovery and low values.
Think about how DFS helps track back edges and earliest reachable ancestors.
You got /5 concepts.
    Describe why an edge is considered a bridge in terms of graph connectivity and Tarjan's algorithm.
    Focus on the meaning of low and discovery times in connectivity.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of Tarjan's algorithm when finding bridges in a graph?
      easy
      A. To sort nodes in topological order
      B. To find the shortest path between two nodes
      C. To detect cycles in a graph
      D. To find edges that, if removed, increase the number of connected components

      Solution

      1. Step 1: Understand what a bridge is in a graph

        A bridge is an edge that, when removed, increases the number of connected components in the graph.
      2. Step 2: Identify Tarjan's algorithm goal

        Tarjan's algorithm is used to find such edges efficiently by using DFS and tracking discovery and low times.
      3. Final Answer:

        To find edges that, if removed, increase the number of connected components -> Option D
      4. Quick Check:

        Bridges = edges increasing components when removed [OK]
      Hint: Bridges disconnect graph if removed [OK]
      Common Mistakes:
      • Confusing bridges with cycles
      • Thinking it finds shortest paths
      • Mixing with topological sorting
      2. Which of the following is the correct TypeScript syntax to declare the discovery time array for Tarjan's algorithm?
      easy
      A. let disc = Array(n).fill('undefined');
      B. let disc: number = new Array(n);
      C. let disc: number[] = new Array(n).fill(-1);
      D. let disc: number[] = new Array(n);

      Solution

      1. Step 1: Understand array declaration with initial values in TypeScript

        To create an array of numbers with size n and initialize all values to -1, use new Array(n).fill(-1).
      2. Step 2: Check type correctness

        TypeScript syntax requires disc to be number[] for an array of numbers.
      3. Final Answer:

        let disc: number[] = new Array(n).fill(-1); -> Option C
      4. Quick Check:

        Array<number> with fill(-1) = correct initialization [OK]
      Hint: Use new Array(n).fill(-1) for initialized arrays [OK]
      Common Mistakes:
      • Using wrong type annotation
      • Not initializing array values
      • Filling with string instead of number
      3. Consider the following TypeScript code snippet using Tarjan's algorithm to find bridges. What will be the output bridges for the given graph?
      const graph = [[1, 2], [0, 2], [0, 1, 3], [2, 4], [3]];
      // Edges: 0-1, 0-2, 1-2, 2-3, 3-4
      // Find bridges
      const bridges = findBridges(graph);
      console.log(bridges);
      medium
      A. [[2, 3], [3, 4]]
      B. [[0, 1], [1, 2]]
      C. [[0, 2], [2, 3]]
      D. []

      Solution

      1. Step 1: Analyze the graph edges and connectivity

        The graph edges are 0-1, 0-2, 1-2 forming a triangle, and 2-3, 3-4 forming a chain. The triangle edges are not bridges because removing one does not disconnect the graph.
      2. Step 2: Identify bridges

        Edges 2-3 and 3-4 are bridges because removing either disconnects node 4 or node 3 from the rest.
      3. Final Answer:

        [[2, 3], [3, 4]] -> Option A
      4. Quick Check:

        Bridges disconnect graph = [[2,3],[3,4]] [OK]
      Hint: Bridges are edges not in any cycle [OK]
      Common Mistakes:
      • Including edges inside cycles as bridges
      • Missing chain edges as bridges
      • Confusing node indices
      4. The following TypeScript function attempts to find bridges using Tarjan's algorithm but has a bug. What is the bug?
      function dfs(u: number, parent: number) {
        visited[u] = true;
        disc[u] = low[u] = time++;
        for (const v of graph[u]) {
          if (!visited[v]) {
            dfs(v, u);
            low[u] = Math.min(low[u], low[v]);
            if (low[v] > disc[u]) {
              bridges.push([u, v]);
            }
          } else if (v !== parent) {
            low[u] = Math.min(low[u], disc[v]);
          }
        }
      }
      medium
      A. The condition 'low[v] > disc[u]' should be 'low[v] >= disc[u]'
      B. The 'visited' array is not initialized before DFS
      C. The 'time' variable is not incremented correctly
      D. The parent node check 'v !== parent' is missing

      Solution

      1. Step 1: Check initialization of visited array

        The code uses 'visited[u]' but does not show initialization. Without initializing, visited may be undefined causing runtime errors.
      2. Step 2: Verify other parts

        The condition 'low[v] > disc[u]' is correct for bridges. The parent check is present. The time increment is correct.
      3. Final Answer:

        The 'visited' array is not initialized before DFS -> Option B
      4. Quick Check:

        Uninitialized visited array causes errors [OK]
      Hint: Always initialize visited before DFS [OK]
      Common Mistakes:
      • Changing correct bridge condition
      • Ignoring visited initialization
      • Removing parent check
      5. Given a connected undirected graph with nodes 0 to 5 and edges: 0-1, 1-2, 2-0, 1-3, 3-4, 4-5, 5-3, which edges are bridges according to Tarjan's algorithm?
      hard
      A. [[1, 3]]
      B. [[3, 4], [4, 5]]
      C. [[0, 1], [1, 2]]
      D. []

      Solution

      1. Step 1: Analyze graph structure

        Edges 0-1, 1-2, 2-0 form a triangle (cycle). Edges 3-4, 4-5, 5-3 form another cycle. Edge 1-3 connects these two cycles.
      2. Step 2: Identify bridges

        Only edge 1-3 is a bridge because removing it disconnects the two cycles. Other edges are part of cycles and not bridges.
      3. Final Answer:

        [[1, 3]] -> Option A
      4. Quick Check:

        Bridge connects two cycles = [[1, 3]] [OK]
      Hint: Bridges connect separate cycles or components [OK]
      Common Mistakes:
      • Marking cycle edges as bridges
      • Missing the connecting bridge edge
      • Confusing node pairs
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard