return to all data-structures

return to concepts

The Graph Data Structure

Overview

Graphs come in many forms, all of which share the following properties:

When to Use Graphs

Types of Graphs

Graph Representations

Graph Traversal

Time Complexity

In the following table, V and E refer to the lengths of the vertices and edges sets respectively. In mathematical notation, these values would be referred to by |V| and |E|.

Graph Representation Operation Time Complexity
Adjacency List Store Graph O(V + E)
  Add Vertex O(1)
  Add Edge O(1)
  Remove Vertex O(E)
  Remove Edge O(V)
  Check Vertex Adjacency O(V)
Adjacency Matrix Store Graph O(V2)
  Add Vertex O(V2)
  Add Edge O(1)
  Remove Vertex O(V2)
  Remove Edge O(1)
  Check Vertex Adjacency O(1)
Incidence Matrix Store Graph O(V * E)
  Add Vertex O(V * E)
  Add Edge O(V * E)
  Remove Vertex O(V * E)
  Remove Edge O(V * E)
  Check Vertex Adjacency O(E)

As you can see, different graph representations have different strengths. Make sure to choose the appropriate representation based off which operations you’ll be performing the most!

Practice Questions

Check out Graph Data Structure Interview Questions and Practice Problems by Medium user Coding Freak for a compliation of common graph-related problems.

References

  1. HackerEarth: Graph Representation
  2. Wikipedia: Adjacency list
  3. Wolfram MathWorld: Incidence Matrix

return to all data-structures

return to concepts