Question

In an adjacency list representation of an undirected simple graph G=(V,E)G = (V, E), each edge (u,v)(u, v) has two adjacency list entries: [v][v] in the adjacency list of uu, and [u][u] in the adjacency list of vv. These are called twins of each other. A twin pointer is a pointer from an adjacency list entry to its twin. If ∣E∣=m|E| = m and ∣V∣=n|V| = n, and the memory size is not a constraint, what is the time complexity of the most efficient algorithm to set the twin pointer in each entry in each adjacency list?

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2016 answer key

Solution

Match twin directed edge entries by sorting all entries using Radix Sort in linear time.

In an undirected graph G=(V,E)G = (V, E), every undirected edge {u,v}\{u, v\} is stored as two directed adjacency list nodes: [v][v] in the list for vertex uu, and [u][u] in the list for vertex vv. Since ∣E∣=m|E| = m, there are exactly 2m2m such nodes in total across all ∣V∣=n|V| = n lists.

Each list node can be viewed as an edge tuple (u,v,node_ptr)(u, v, \text{node\_ptr}), where uu is the list owner, vv is the neighbor, and node_ptr\text{node\_ptr} points to the entry in uu's list. Traversing all adjacency lists to collect all 2m2m tuples takes Θ(n+m)\Theta(n + m) time.

To pair each directed entry (u,v)(u, v) with its twin (v,u)(v, u), normalize the endpoints so the pair is keyed by (min⁡(u,v),max⁡(u,v))(\min(u, v), \max(u, v)). Because vertex indices are integers from 11 to nn, we can sort the 2m2m tuples using two passes of Counting Sort (Radix Sort with base nn).

Sorting 2m2m tuples with integer keys in the range [1,n][1, n] takes Θ(n+m)\Theta(n + m) time and Θ(n+m)\Theta(n + m) auxiliary memory. After sorting, twin entries appear consecutively in pairs. A single pass over the sorted array takes Θ(m)\Theta(m) time to set the twin pointer in each node to point to the other node.

Therefore, the total time complexity of the most efficient algorithm is Θ(n+m)\Theta(n + m).

Final answer

B

Common trap

Assuming that setting pointers requires comparing each edge to all other edges taking Θ(m2)\Theta(m^2), or that initializing an n×nn \times n direct pointer matrix requires Θ(n2)\Theta(n^2).

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
51 of 65, printed as CS Q.41
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.