0
0
DSA Typescriptprogramming~20 mins

Bridges in Graph Tarjan's Algorithm in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tarjan Bridges Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Tarjan's Bridges Algorithm on a Simple Graph
What is the output (list of bridges) of the following TypeScript code that finds bridges in an undirected graph using Tarjan's algorithm?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();
  time = 0;

  addEdge(u: number, v: number) {
    if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []);
    if (!this.adjacencyList.has(v)) this.adjacencyList.set(v, []);
    this.adjacencyList.get(u)!.push(v);
    this.adjacencyList.get(v)!.push(u);
  }

  findBridges(): [number, number][] {
    const visited = new Set<number>();
    const disc = new Map<number, number>();
    const low = new Map<number, number>();
    const parent = new Map<number, number | null>();
    const bridges: [number, number][] = [];

    const dfs = (u: number) => {
      visited.add(u);
      this.time++;
      disc.set(u, this.time);
      low.set(u, this.time);

      for (const v of this.adjacencyList.get(u) || []) {
        if (!visited.has(v)) {
          parent.set(v, u);
          dfs(v);
          low.set(u, Math.min(low.get(u)!, low.get(v)!));
          if (low.get(v)! > disc.get(u)!) {
            bridges.push([u, v]);
          }
        } else if (v !== parent.get(u)) {
          low.set(u, Math.min(low.get(u)!, disc.get(v)!));
        }
      }
    };

    for (const node of this.adjacencyList.keys()) {
      if (!visited.has(node)) {
        parent.set(node, null);
        dfs(node);
      }
    }

    return bridges;
  }
}

const g = new Graph();
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(4, 5);

console.log(g.findBridges());
A[[1, 2], [1, 3]]
B[[3, 4], [3, 5]]
C[[1, 2]]
D[]
Attempts:
2 left
💡 Hint
Think about which edges, if removed, increase the number of connected components.
🧠 Conceptual
intermediate
1:30remaining
Understanding Low and Discovery Times in Tarjan's Algorithm
In Tarjan's algorithm for finding bridges, what does the 'low' value of a node represent?
AThe total number of edges connected to the node.
BThe earliest discovery time reachable from the node including itself and its descendants.
CThe maximum depth of the node in the DFS tree.
DThe number of connected components in the graph.
Attempts:
2 left
💡 Hint
Think about what 'low' helps to detect in the graph.
🔧 Debug
advanced
2:30remaining
Identify the Bug in Tarjan's Bridge Detection Code
What error will the following TypeScript code produce when run, and why?
DSA Typescript
class Graph {
  adjacencyList: Map<number, number[]> = new Map();
  time = 0;

  addEdge(u: number, v: number) {
    if (!this.adjacencyList.has(u)) this.adjacencyList.set(u, []);
    if (!this.adjacencyList.has(v)) this.adjacencyList.set(v, []);
    this.adjacencyList.get(u)!.push(v);
    this.adjacencyList.get(v)!.push(u);
  }

  findBridges(): [number, number][] {
    const visited = new Set<number>();
    const disc = new Map<number, number>();
    const low = new Map<number, number>();
    const parent = new Map<number, number | null>();
    const bridges: [number, number][] = [];

    const dfs = (u: number) => {
      visited.add(u);
      this.time++;
      disc.set(u, this.time);
      low.set(u, this.time);

      for (const v of this.adjacencyList.get(u) || []) {
        if (!visited.has(v)) {
          parent.set(v, u);
          dfs(v);
          low.set(u, Math.min(low.get(u)!, low.get(v)!));
          if (low.get(v)! >= disc.get(u)!) {
            bridges.push([u, v]);
          }
        } else if (v !== parent.get(u)) {
          low.set(u, Math.min(low.get(u)!, disc.get(v)!));
        }
      }
    };

    for (const node of this.adjacencyList.keys()) {
      if (!visited.has(node)) {
        parent.set(node, null);
        dfs(node);
      }
    }

    return bridges;
  }
}

const g = new Graph();
g.addEdge(1, 2);
g.addEdge(1, 3);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(4, 5);

console.log(g.findBridges());
AThe code throws a runtime error because 'low.get(v)' can be undefined.
BThe code outputs an empty list because the DFS never visits any node.
CThe code outputs extra edges as bridges due to incorrect comparison operator in if condition.
DThe code runs correctly and outputs [[1, 2], [1, 3]]
Attempts:
2 left
💡 Hint
Check the condition that decides if an edge is a bridge.
🚀 Application
advanced
1:30remaining
Number of Bridges in a Complete Graph
If you run Tarjan's algorithm to find bridges on a complete graph with 5 nodes (every node connected to every other node), how many bridges will it find?
A0
B10
C5
D1
Attempts:
2 left
💡 Hint
Think about cycles in a complete graph.
🧠 Conceptual
expert
2:00remaining
Why Tarjan's Algorithm Uses DFS Time Instead of BFS
Why does Tarjan's algorithm for finding bridges use Depth-First Search (DFS) and discovery times instead of Breadth-First Search (BFS)?
ADFS uses less memory than BFS, which is critical for bridge detection.
BBFS is slower than DFS for all graph algorithms, so DFS is preferred.
CBFS cannot be implemented recursively, so it is not suitable for Tarjan's algorithm.
DDFS naturally builds a tree structure with discovery times that help identify back edges and cycles, which BFS does not provide.
Attempts:
2 left
💡 Hint
Consider how discovery times and back edges help detect bridges.