Machine-translated by Google. Questions are authored in English.
Top

Discussion

In an adjacency list representation of an undirected simple graph \(G = (V,E)\), each edge \((u, v)\) has two adjacency list entries: [\(v\)] in the adjacency list of \(u\), and [\(u\)] in the adjacency list of \(v\). These are called twins of each other. A twin pointer is a pointer from an adjacency list entry to its twin. If \(|E| = m\) and \(|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?

  • A.\(\Theta(n^{2})\)
  • B.\(\Theta(n + m)\)
  • C.\(\Theta(m^{2})\)
  • D.\(\Theta(n^{4})\)

Answer: B

The most efficient algorithm runs in \(\Theta(n + m)\).

Any algorithm must at least read all \(n\) lists and all \(2m\) entries, so \(\Omega(n + m)\) is a lower bound. It can be met:

  1. For each entry [\(v\)] in the list of \(u\), form the key \((\min(u,v), \max(u,v))\); the two twins of an edge get the same key.
  2. Sort all \(2m\) entries by key with a two-pass bucket (radix) sort on vertex numbers, which takes \(O(n + m)\).
  3. Twins are now adjacent in the sorted order, so one pass sets each pair of twin pointers.

A hash table keyed on the vertex pair achieves the same expected bound, and memory is stated not to be a constraint. \(\Theta(n^{2})\) is what a full adjacency-matrix lookup costs, and \(\Theta(m^{2})\) comes from searching a twin’s whole list for every entry; both are slower than necessary.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.