Data Structures & AlgorithmsHardProduct Company SDE pattern

What is printed? (articulation points by brute force removal)

What is printed? (articulation points by brute force removal)
edges = [(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 5), (5, 3), (5, 6)]
V = 7
def comps(skip):
    g = {v: [] for v in range(V) if v != skip}
    for u, v in edges:
        if skip not in (u, v): g[u].append(v); g[v].append(u)
    seen, c = set(), 0
    for s in g:
        if s in seen: continue
        c += 1; st = [s]; seen.add(s)
        while st:
            for v in g[st.pop()]:
                if v not in seen: seen.add(v); st.append(v)
    return c
print([v for v in range(V) if comps(v) > comps(-1)])
  1. A. [1, 3]
  2. B. [1, 3, 5, 6]
  3. C. [1, 3, 5]
  4. D. [3, 5]

Correct answer: C. [1, 3, 5]

A vertex is an articulation point if removing it increases the number of components. Removing 1 separates triangle {0, 2}; removing 3 separates {0, 1, 2} from {4, 5, 6}; removing 5 isolates 6. Brute force is O(V(V + E)); Tarjan's low-link DFS finds them in O(V + E).

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic