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?
View worked solution
Answer: B
To set the twin pointers efficiently, we need to pair each entry in 's adjacency list with its twin entry in 's adjacency list. There are such entries in total across all adjacency lists.
We can achieve this in time using the following algorithm:
- Create Tuples: For every vertex and each neighbor in 's adjacency list, create a tuple: There are exactly such tuples.
- Sort the Tuples: Sort these tuples using Radix Sort (with two passes of Counting Sort, since the vertex indices are in the range ). The time complexity of this sorting is .
- Link Twins: After sorting, the two twin entries corresponding to any edge will end up adjacent to each other in the sorted array. We can traverse the sorted array in a single pass of steps and set the twin pointers of each adjacent pair to point to each other.
The overall time complexity is .
Free practice · Your answers on this page are not saved.
