4. Coding a Graph #

Created Monday 30 March 2020

There are 3 types of implementations:

  1. Edge List

We can save all the vertices in a vector. Edges are stored as 2-tuples of neighbours(vertices).

  1. Adjacency List

Store a list of vertex. Also, store a table containing vertiex-{neighbours of vertex}. Neighbours are a vector of the neigbhouring vertices. This is very nice.

  1. Adjacency Matrix

We don’t need to store the vertices seperately. Both V, E are stored in the same structure.

Verdict: Adjacency matrix is faster than Adjacency list. We use it if we have no space constraint. We use a list when space is scarce. Generally matrices are used for competitions, where speed matters.

We will be using adjacency matrix as it is the most easy to implement.


Note: The representations discussed are applicable for all types of graphs - directed, undirected, multi, self-loop etc.