Concept

Pros / Cons of Adjacency Matrix and List

Adjacency Matrix :

  • Time complexity for adding / removing an edge in an adjacency matrix is in O(1) time
  • Easy to implement
  • Take lots of memory in order to store big graphs regardless of whether the graph is sparse or dense **
  • Time complexity for adding/ removing a vertex is O(V ^2) where V is a vertex

Adjacency List :

  • Time complexity for adding / removing an edge in an adjacency matrix is in O( E / V) time where E is an edge and V is a vertex
  • Takes less memory to store the graph
  • Time complexity for adding a vertex is O(V) where V is a vertex and removing a vertex is O(E) where E is an edge
  • A list of adjacent vertices can be obtained in O(1) time

** Sparse graphs have a number of edges that is much less than the square of the number of vertices while dense graphs have a number of edges that is comparable to the square of the number of vertices.

0

1

Updated 2021-06-01

Tags

Python Programming Language

Data Science