KatInterview

Alive-Node Graph Simulator with Gated Edge Insertion

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designunion-findgraphsimulation

Last updated

Simulate a sequence of node toggles, edge edits, and connectivity queries on an undirected graph where only alive nodes count and edges are only added between alive endpoints.

You are given a graph with n nodes labeled 0 to n - 1 and a list of operations to run in order. Each node is either alive or dead. The graph is undirected and starts with no edges, and every node starts alive.

Implement a function that processes the operations one at a time and returns one result per operation.

Operations

OperationArgumentsEffectResult
"activate"[i]Mark node i alive.None
"deactivate"[i]Mark node i dead.None
"connect"[u, v]Record the edge {u, v} only if both u and v are alive at this moment. Otherwise do nothing.None
"disconnect"[u, v]Remove the edge {u, v} if it is recorded, whether or not its endpoints are alive. Do nothing if it is not recorded.None
"isConnected"[u, v]Whether u and v are linked by a path that uses only alive nodes.bool
"countComponents"[]Number of connected components in the subgraph formed by alive nodes and the recorded edges between them.int

Rules

  • Edges are undirected: {u, v} and {v, u} are the same edge. Connecting an edge that is already recorded changes nothing.
  • Deactivating a node does not erase its recorded edges. They are simply ignored while either endpoint is dead, and they take effect again if that node is reactivated.
  • A connect that is skipped because an endpoint was dead leaves no trace. Reactivating the node later does not create that edge.
  • isConnected(u, v) is False if u or v is dead. isConnected(u, u) is True exactly when u is alive.
  • Self-loops may be recorded and do not affect connectivity.
  • Activating an already-alive node or deactivating an already-dead node changes nothing.
  • countComponents counts only alive nodes (an alive node with no usable edges is its own component). If every node is dead, the answer is 0.
  • If an unknown operation name appears, raise a ValueError.

The output list has the same length as operations, with None for operations that return nothing.

Constraints

  • 1 <= n <= 100000
  • operations and arguments have the same length, at most 100000.
  • All node indices are valid, in the range 0..n-1.
  • arguments[k] is [] for countComponents, a single-element list for activate/deactivate, and a two-element list for the others.

Examples

Example 1

Input: simulate_alive_graph(5, ['connect', 'connect', 'connect', 'isConnected', 'countComponents', 'deactivate', 'isConnected', 'countComponents'], [[0, 1], [1, 2], [3, 4], [0, 2], [], [1], [0, 2], []])

Output: [None, None, None, True, 2, None, False, 3]

Example 2

Input: simulate_alive_graph(3, ['countComponents', 'deactivate', 'countComponents', 'activate', 'connect', 'isConnected'], [[], [1], [], [1], [0, 1], [0, 1]])

Output: [3, None, 2, None, None, True]

Hints

Hint 1

Keep two pieces of state that are cheap to update: a per-node alive flag and a set of recorded edges stored as normalized (min, max) pairs.

Hint 2

A union-find cannot undo merges, and both disconnect and deactivate are deletions. Derive connectivity from the stored edges when a query arrives instead of maintaining it online.

Hint 3

For a query, build a union-find over only the edges whose endpoints are both alive. The component count is the number of alive nodes minus the number of successful unions. Use a dirty flag so consecutive queries share one rebuild.