Search code examples
algorithmdata-structuresgraph

Fast Convertion From Adjacency List to Edge List


I have an undirected weighted graph implemented with adjacency list which I need to convert to an edge list. The problem is, for each edge (A, B), I have two entries in adjacency list: one indicating an edge to B from A and other from A to B. So, while converting this to edge list, I need to search the whole partially completed edge list to prevent duplicates.

Is there any way I can make that faster/ less complicated? I need to keep the adjacency list because my algorithm of detecting cycles in graph relies on it.


Solution

  • Enumerate you vertices add to your list only edges (u, v) such that u <= v.