Search code examples
algorithmgraphimplementationedges

What is the best Implementation for a Multigraph?


In both space and operation cost which is the best way to implement a multigraph that has more edges than vertices?

In the worst case it would have 5000 edges and 1000 vertices. I was thinking of an adjacency list because it has a great time for most of the operations like add edges, check adjacency between edges, add vertices (almost all the time), etc.... but it still consumes a space of |v^2|.

Am I on the right track? Is there a better implementation? Any tips on the best way to implement the the adjacency list?


Solution

  • A ratio E/V = 5 means really really sparse graphs, which is a plus for lists. In general adjacency lists are overall better than adjacency matrices.

    Now the cost of insertion is O(degree(vertex)) but with so few edges it will be negligible. Don't look further, use adjacency lists.