GATE / CS ARCHIVE

GATE CSE 1994 / QUESTION 24(b)

Implementing leaf elimination in linear time

Algorithms · Graph Algorithms · Full GATE CSE 1994 paper

GATE 1994, Q24(b) Descriptive
Report issue

Graph Algorithms

Question 24(b), GATE 1994: For the tree independent-set algorithm that repeatedly removes a current leaf or isolated vertex and adds it if it has no selected neighbor, determine the time complexity and necessary implementation assumptions.
Question 24(b), GATE 1994: For the tree independent-set algorithm that repeatedly removes a current leaf or isolated vertex and adds it if it has no selected neighbor, determine the time complexity and necessary implementation assumptions.
Question 24(b), GATE 1994: For the tree independent-set algorithm that repeatedly removes a current leaf or isolated vertex and adds it if it has no selected neighbor, determine the time complexity and necessary implementation assumptions.
Tap to enlarge
Answer & solution

Answer: Θ(n) time and O(n) working space with adjacency lists, degree counters and a leaf queue; naïve rescanning may take Θ(n²). (no official key was published)

A tree with n vertices has n−1 edges. Store its original adjacency lists, each vertex's current degree, a removed flag, a selected-neighbor (blocked) flag, and a queue of vertices whose current degree is at most one. Initializing these structures takes O(n+|E|)=O(n) time.

Pop a still-unremoved queue entry u, mark it removed, and add it to I exactly when its blocked flag is false. If added, mark its neighbors blocked. For each still-present neighbor, decrement the neighbor's degree; enqueue it when its degree first falls to one. Isolated initial vertices are queued from the start. Any harmless duplicate or stale entries can be skipped with the removed flag; alternatively use a queued flag.

Every vertex is removed once. Its adjacency list is scanned only a constant number of times, and the sum of degrees is 2(n−1). Hence all degree maintenance, blocking updates and queue operations total O(n). Reading all vertices and producing the result requires Ω(n) in the worst case, yielding Θ(n) time. The queue, flags, degree counts and adjacency representation require O(n) space.

The abstract scheme by itself does not force this bound: repeatedly searching all remaining vertices for a leaf or scanning I to test every candidate can take quadratic time. The linear bound is the efficient adjacency-list implementation, not a representation-independent guarantee. This distinction matters because the printed question specifies a tree and an algorithm outline but not data structures.

Original paper, page 7