There are some algorithms, like Edmond's Algorithm, or Boruvka's Algorithm which require the programmer to create a graph which is obtained by contraction of some nodes into a single node, and later expanding it back.

A formal description of contraction is as follows:

Let G be a graph with vertices V and edges E . Let C be a connected component of G . A contraction of G with respect to C is defined as the graph on V - nodes(C) + C* , where C* is a "supernode" representing the contracted component. The edges which do not involve vertices in C are as is. The edges which had an endpoint in C are now connected to C* .

It is not clear to me how to implement such algorithmic primitives using representations like adjacency lists.

What would be an elegant and efficient way to represent graphs so that they can be contracted, while remembering the relevant data for expanding them?