0
0
DSA Typescriptprogramming~20 mins

Articulation Points in Graph in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Articulation Points Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of articulation points detection on a simple graph
Given the following TypeScript code that finds articulation points in an undirected graph, what is the output printed by the program?
DSA Typescript
class Graph {
  vertices: number;
  adjacencyList: Map<number, number[]>;

  constructor(vertices: number) {
    this.vertices = vertices;
    this.adjacencyList = new Map();
    for (let i = 0; i < vertices; i++) {
      this.adjacencyList.set(i, []);
    }
  }

  addEdge(u: number, v: number): void {
    this.adjacencyList.get(u)?.push(v);
    this.adjacencyList.get(v)?.push(u);
  }

  articulationPoints(): number[] {
    const visited = new Array(this.vertices).fill(false);
    const disc = new Array(this.vertices).fill(-1);
    const low = new Array(this.vertices).fill(-1);
    const parent = new Array(this.vertices).fill(-1);
    const ap = new Array(this.vertices).fill(false);
    let time = 0;

    const dfs = (u: number) => {
      visited[u] = true;
      disc[u] = low[u] = time++;
      let children = 0;

      for (const v of this.adjacencyList.get(u) || []) {
        if (!visited[v]) {
          children++;
          parent[v] = u;
          dfs(v);
          low[u] = Math.min(low[u], low[v]);

          if (parent[u] === -1 && children > 1) {
            ap[u] = true;
          }

          if (parent[u] !== -1 && low[v] >= disc[u]) {
            ap[u] = true;
          }
        } else if (v !== parent[u]) {
          low[u] = Math.min(low[u], disc[v]);
        }
      }
    };

    for (let i = 0; i < this.vertices; i++) {
      if (!visited[i]) dfs(i);
    }

    const result: number[] = [];
    for (let i = 0; i < this.vertices; i++) {
      if (ap[i]) result.push(i);
    }
    return result;
  }
}

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

console.log(g.articulationPoints());
A[0, 3]
B[1, 3]
C[0, 2, 3]
D[3, 4]
Attempts:
2 left
💡 Hint
Think about nodes whose removal increases the number of connected components.
🧠 Conceptual
intermediate
1:00remaining
Understanding articulation points in a graph
Which of the following best describes an articulation point in an undirected graph?
AA vertex that is connected to all other vertices directly.
BA vertex with the highest degree in the graph.
CA vertex that, when removed, increases the number of connected components in the graph.
DA vertex that has no edges connected to it.
Attempts:
2 left
💡 Hint
Think about the effect of removing a vertex on graph connectivity.
🔧 Debug
advanced
2:00remaining
Identify the error in articulation points detection code
Consider this TypeScript snippet for articulation points detection. What error will it cause when run?
DSA Typescript
function findAP(graph: Map<number, number[]>, vertices: number): number[] {
  const visited = new Array(vertices).fill(false);
  const disc = new Array(vertices).fill(-1);
  const low = new Array(vertices).fill(-1);
  const parent = new Array(vertices).fill(-1);
  const ap = new Array(vertices).fill(false);
  let time = 0;

  function dfs(u: number) {
    visited[u] = true;
    disc[u] = low[u] = time++;
    let children = 0;

    for (const v of graph.get(u) || []) {
      if (!visited[v]) {
        children++;
        parent[v] = u;
        dfs(v);
        low[u] = Math.min(low[u], low[v]);

        if (parent[u] === -1 && children > 1) ap[u] = true;
        if (parent[u] !== -1 && low[v] >= disc[u]) ap[u] = true;
      } else if (v !== parent[u]) {
        low[u] = Math.min(low[u], disc[v]);
      }
    }
  }

  for (let i = 0; i < vertices; i++) {
    if (!visited[i]) dfs(i);
  }

  const result: number[] = [];
  for (let i = 0; i < vertices; i++) {
    if (ap[i]) result.push(i);
  }
  return result;
}

const g = new Map();
g.set(0, [1, 2]);
g.set(1, [0]);
g.set(2, [0]);

console.log(findAP(g, 4));
AOutput: []
BSyntaxError: Unexpected token
COutput: [0]
DTypeError: Cannot read property 'Symbol.iterator' of undefined
Attempts:
2 left
💡 Hint
Check how adjacency lists are accessed in the for loop.
Predict Output
advanced
2:00remaining
Output of articulation points in a disconnected graph
What is the output of the following TypeScript code that finds articulation points in a disconnected graph?
DSA Typescript
class Graph {
  vertices: number;
  adjacencyList: Map<number, number[]>;

  constructor(vertices: number) {
    this.vertices = vertices;
    this.adjacencyList = new Map();
    for (let i = 0; i < vertices; i++) {
      this.adjacencyList.set(i, []);
    }
  }

  addEdge(u: number, v: number): void {
    this.adjacencyList.get(u)?.push(v);
    this.adjacencyList.get(v)?.push(u);
  }

  articulationPoints(): number[] {
    const visited = new Array(this.vertices).fill(false);
    const disc = new Array(this.vertices).fill(-1);
    const low = new Array(this.vertices).fill(-1);
    const parent = new Array(this.vertices).fill(-1);
    const ap = new Array(this.vertices).fill(false);
    let time = 0;

    const dfs = (u: number) => {
      visited[u] = true;
      disc[u] = low[u] = time++;
      let children = 0;

      for (const v of this.adjacencyList.get(u) || []) {
        if (!visited[v]) {
          children++;
          parent[v] = u;
          dfs(v);
          low[u] = Math.min(low[u], low[v]);

          if (parent[u] === -1 && children > 1) {
            ap[u] = true;
          }

          if (parent[u] !== -1 && low[v] >= disc[u]) {
            ap[u] = true;
          }
        } else if (v !== parent[u]) {
          low[u] = Math.min(low[u], disc[v]);
        }
      }
    };

    for (let i = 0; i < this.vertices; i++) {
      if (!visited[i]) dfs(i);
    }

    const result: number[] = [];
    for (let i = 0; i < this.vertices; i++) {
      if (ap[i]) result.push(i);
    }
    return result;
  }
}

const g = new Graph(7);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(1, 3);
g.addEdge(3, 4);
g.addEdge(4, 5);
g.addEdge(5, 3);
// Node 6 is disconnected

console.log(g.articulationPoints());
A[1, 3]
B[0, 1, 3]
C[3, 4]
D[6]
Attempts:
2 left
💡 Hint
Consider articulation points in each connected component separately.
🚀 Application
expert
3:00remaining
Number of articulation points in a complex graph
Given a graph with 8 vertices and edges: (0-1), (1-2), (2-0), (1-3), (3-4), (4-5), (5-3), (5-6), (6-7), how many articulation points does this graph have?
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Identify vertices whose removal disconnects parts of the graph.