DEV Community

Akashdeep
Akashdeep

Posted on

1

Big O Complexity for Graphs: Adjacency Matrix vs Adjacency List

Graphs can be represented in two main ways: Adjacency Matrix and Adjacency List. Each method has its own pros and cons depending on how much memory it needs and how quickly it can handle different operations.

1. Space Complexity

Adjacency Matrix : O(V^2)
Adjacency List : O(V+E)

  • V: Number of vertices.
  • E: Number of edges.
  • Adjacency Matrix requires a VΓ—V grid, making it less efficient for sparse graphs.
  • Adjacency List only stores edges for each vertex, making it space-efficient for sparse graphs.

2. Time Complexity: Adding a Vertex

Adjacency Matrix : O(V^2)
Adjacency List : O(1)

  • Adjacency Matrix: Adding a vertex may require creating a new 𝑉+1×𝑉+1 matrix and copying the old matrix, which is expensive.
  • Adjacency List: Simply add a new empty list for the new vertex, which is constant time.

3. Time Complexity: Adding an Edge

Adjacency Matrix : O(1)
Adjacency List : O(1)

Both representations allow constant-time edge additions:

  • Matrix: Set the matrix cell to 1 (or edge weight).
  • List: Append the edge to the list.

4. Time Complexity: Removing an Edge

Adjacency Matrix : O(1)
Adjacency List : O(E/V)

  • Adjacency Matrix: Directly unset the cell, which is constant time.
  • Adjacency List: Requires searching through the list for the edge, leading to O(E/V) time on average.

5. Time Complexity: Removing a Vertex

Adjacency Matrix : O(V^2)
Adjacency List : O(V+E)

  • Adjacency Matrix: Removing a vertex involves deleting its row and column, which can require shifting elements in the matrix.
  • Adjacency List: Removing a vertex requires deleting the list for the vertex and traversing all other lists to remove edges to the vertex.

Choosing the Right Representation

  • Adjacency Matrix:
    Suitable for dense graphs where Eβ‰ˆV2.
    Offers constant-time edge operations but consumes more space.

  • Adjacency List:
    Ideal for sparse graphs where Eβ‰ͺV2.
    Space-efficient and generally faster for operations involving vertices.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (1)

Collapse
 
akshay_sabu_061f86c4b261e profile image
Akshay Sabu β€’

informative

Image of Checkly

4 Playwright Locators Explained: Which One Should You Use?

- locator('.cta'): Fast but brittle
- getByText('Click me'): User-facing, but can miss broken accessibility
- getByRole('button', { name: 'Click me' }): Most robust, best for a11y
- getByTestId('cta-button'): Stable, but ignores UX

Watch video

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay