Question
In an adjacency list representation of an undirected simple graph , each edge has two adjacency list entries: in the adjacency list of , and in the adjacency list of . These are called twins of each other. A twin pointer is a pointer from an adjacency list entry to its twin. If and , 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?
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 , every undirected edge is stored as two directed adjacency list nodes: in the list for vertex , and in the list for vertex . Since , there are exactly such nodes in total across all lists.
Each list node can be viewed as an edge tuple , where is the list owner, is the neighbor, and points to the entry in 's list. Traversing all adjacency lists to collect all tuples takes time.
To pair each directed entry with its twin , normalize the endpoints so the pair is keyed by . Because vertex indices are integers from to , we can sort the tuples using two passes of Counting Sort (Radix Sort with base ).
Sorting tuples with integer keys in the range takes time and auxiliary memory. After sorting, twin entries appear consecutively in pairs. A single pass over the sorted array takes 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 .
B
Assuming that setting pointers requires comparing each edge to all other edges taking , or that initializing an direct pointer matrix requires .
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.