- Time complexity:
- Insertion: O(1)
- Deletion: O(1)
- Space complexity: O(V^2)
- Simple to implement
- Fast to access edge (u, v), O(1), fast to add and remove edges
- Space complexity is O(V^2)
- Its not efficient for sparse graphs
- Adjacency Matrix
- Undirected Graph
- Directed Graph
- Adjacency Matrix - Playground
- Adjacency Matrix - Weighted
- Time complexity:
- Insertion: O(V) using list, O(1) using Hash Table
- Deletion: O(V) using list, O(1) using Hash Table
- Space complexity: O(V + E)
- Space complexity is O(V + E)
- Efficient for sparse graphs
- Slow to access edge (u, v), O(V) using list, but can be O(1) using Hash Table
- Time complexity:
- Insertion: O(1)
- Deletion: O(E)
- Space complexity: O(E)
- Space complexity is O(E)
- Fast to add edges
- Slow to access edge (u, v), O(E)