
- Graph Theory - Home
- Graph Theory - Introduction
- Graph Theory - History
- Graph Theory - Fundamentals
- Graph Theory - Applications
- Types of Graphs
- Graph Theory - Types of Graphs
- Graph Theory - Simple Graphs
- Graph Theory - Multi-graphs
- Graph Theory - Directed Graphs
- Graph Theory - Weighted Graphs
- Graph Theory - Bipartite Graphs
- Graph Theory - Complete Graphs
- Graph Theory - Subgraphs
- Graph Theory - Trees
- Graph Theory - Forests
- Graph Theory - Planar Graphs
- Graph Theory - Hypergraphs
- Graph Theory - Infinite Graphs
- Graph Theory - Random Graphs
- Graph Representation
- Graph Theory - Graph Representation
- Graph Theory - Adjacency Matrix
- Graph Theory - Adjacency List
- Graph Theory - Incidence Matrix
- Graph Theory - Edge List
- Graph Theory - Compact Representation
- Graph Theory - Incidence Structure
- Graph Theory - Matrix-Tree Theorem
- Graph Properties
- Graph Theory - Basic Properties
- Graph Theory - Coverings
- Graph Theory - Matchings
- Graph Theory - Independent Sets
- Graph Theory - Traversability
- Graph Theory Connectivity
- Graph Theory - Connectivity
- Graph Theory - Vertex Connectivity
- Graph Theory - Edge Connectivity
- Graph Theory - k-Connected Graphs
- Graph Theory - 2-Vertex-Connected Graphs
- Graph Theory - 2-Edge-Connected Graphs
- Graph Theory - Strongly Connected Graphs
- Graph Theory - Weakly Connected Graphs
- Graph Theory - Connectivity in Planar Graphs
- Graph Theory - Connectivity in Dynamic Graphs
- Special Graphs
- Graph Theory - Regular Graphs
- Graph Theory - Complete Bipartite Graphs
- Graph Theory - Chordal Graphs
- Graph Theory - Line Graphs
- Graph Theory - Complement Graphs
- Graph Theory - Graph Products
- Graph Theory - Petersen Graph
- Graph Theory - Cayley Graphs
- Graph Theory - De Bruijn Graphs
- Graph Algorithms
- Graph Theory - Graph Algorithms
- Graph Theory - Breadth-First Search
- Graph Theory - Depth-First Search (DFS)
- Graph Theory - Dijkstra's Algorithm
- Graph Theory - Bellman-Ford Algorithm
- Graph Theory - Floyd-Warshall Algorithm
- Graph Theory - Johnson's Algorithm
- Graph Theory - A* Search Algorithm
- Graph Theory - Kruskal's Algorithm
- Graph Theory - Prim's Algorithm
- Graph Theory - Borůvka's Algorithm
- Graph Theory - Ford-Fulkerson Algorithm
- Graph Theory - Edmonds-Karp Algorithm
- Graph Theory - Push-Relabel Algorithm
- Graph Theory - Dinic's Algorithm
- Graph Theory - Hopcroft-Karp Algorithm
- Graph Theory - Tarjan's Algorithm
- Graph Theory - Kosaraju's Algorithm
- Graph Theory - Karger's Algorithm
- Graph Coloring
- Graph Theory - Coloring
- Graph Theory - Edge Coloring
- Graph Theory - Total Coloring
- Graph Theory - Greedy Coloring
- Graph Theory - Four Color Theorem
- Graph Theory - Coloring Bipartite Graphs
- Graph Theory - List Coloring
- Advanced Topics of Graph Theory
- Graph Theory - Chromatic Number
- Graph Theory - Chromatic Polynomial
- Graph Theory - Graph Labeling
- Graph Theory - Planarity & Kuratowski's Theorem
- Graph Theory - Planarity Testing Algorithms
- Graph Theory - Graph Embedding
- Graph Theory - Graph Minors
- Graph Theory - Isomorphism
- Spectral Graph Theory
- Graph Theory - Graph Laplacians
- Graph Theory - Cheeger's Inequality
- Graph Theory - Graph Clustering
- Graph Theory - Graph Partitioning
- Graph Theory - Tree Decomposition
- Graph Theory - Treewidth
- Graph Theory - Branchwidth
- Graph Theory - Graph Drawings
- Graph Theory - Force-Directed Methods
- Graph Theory - Layered Graph Drawing
- Graph Theory - Orthogonal Graph Drawing
- Graph Theory - Examples
- Computational Complexity of Graph
- Graph Theory - Time Complexity
- Graph Theory - Space Complexity
- Graph Theory - NP-Complete Problems
- Graph Theory - Approximation Algorithms
- Graph Theory - Parallel & Distributed Algorithms
- Graph Theory - Algorithm Optimization
- Graphs in Computer Science
- Graph Theory - Data Structures for Graphs
- Graph Theory - Graph Implementations
- Graph Theory - Graph Databases
- Graph Theory - Query Languages
- Graph Algorithms in Machine Learning
- Graph Neural Networks
- Graph Theory - Link Prediction
- Graph-Based Clustering
- Graph Theory - PageRank Algorithm
- Graph Theory - HITS Algorithm
- Graph Theory - Social Network Analysis
- Graph Theory - Centrality Measures
- Graph Theory - Community Detection
- Graph Theory - Influence Maximization
- Graph Theory - Graph Compression
- Graph Theory Real-World Applications
- Graph Theory - Network Routing
- Graph Theory - Traffic Flow
- Graph Theory - Web Crawling Data Structures
- Graph Theory - Computer Vision
- Graph Theory - Recommendation Systems
- Graph Theory - Biological Networks
- Graph Theory - Social Networks
- Graph Theory - Smart Grids
- Graph Theory - Telecommunications
- Graph Theory - Knowledge Graphs
- Graph Theory - Game Theory
- Graph Theory - Urban Planning
- Graph Theory Useful Resources
- Graph Theory - Quick Guide
- Graph Theory - Useful Resources
- Graph Theory - Discussion
Graph Theory - Data Structures for Graphs
Data Structures for Graphs
In graph theory, a graph is a collection of nodes (or vertices) and edges that connect pairs of nodes. Graphs can be represented using various data structures, which are used to store and manipulate the graph.
The choice of data structure has a significant impact on the performance of graph algorithms, such as search, traversal, and shortest path computations. Choosing the right data structure for a given problem is important for optimizing the time and space complexity of algorithms.
This tutorial will explore different data structures used to represent graphs, their characteristics, advantages, and disadvantages. We will also compare these structures based on their efficiency for various graph algorithms.
What is a Graph?
A graph is a mathematical structure consisting of nodes (also called vertices) and edges (also called links or arcs). The nodes represent entities, and the edges represent the relationships between them. There are two main types of graphs −
- Directed Graph (Digraph): A graph where edges have a direction, meaning they point from one node to another.
- Undirected Graph: A graph where edges do not have a direction; they represent mutual relationships between nodes.
Basic Graph Terminology
Before learning about the data structures, it is important to understand some basic graph terminology −
- Vertex (Node): A single point in the graph that represents an entity.
- Edge (Link/Arc): A connection between two vertices in the graph.
- Degree: The number of edges connected to a vertex. In a directed graph, there is an in-degree and an out-degree.
- Path: A sequence of edges that connects a series of vertices.
- Cycle: A path that begins and ends at the same vertex.
Types of Data Structures
There are primarily two main types of data structures used to represent graphs −
- Adjacency Matrix
- Adjacency List
Each data structure has its strengths and weaknesses, and their choice depends on the type of graph and the operations you need to perform.
Adjacency Matrix
An adjacency matrix is a two-dimensional matrix used to represent a graph. The rows and columns represent the nodes in the graph, and the entries in the matrix indicate whether there is an edge between the corresponding nodes. The matrix has a size of V x V, where V is the number of vertices in the graph.
The matrix is typically a square matrix, where each cell at position (i, j) contains a value (usually 1 or 0) to indicate whether an edge exists between vertex i and vertex j.
- In an undirected graph, if there is an edge between vertex i and vertex j, both positions (i, j) and (j, i) will be set to 1.
- In a directed graph, a 1 at position (i, j) means there is a directed edge from vertex i to vertex j, while (j, i) would be 0 unless there is also a directed edge in the opposite direction.
Advantages:
- Efficient for dense graphs where most pairs of vertices are connected.
- Provides fast access to check the presence of an edge between two vertices in constant time, i.e., O(1).
Disadvantages:
- Requires O(V2) space, even if the graph is sparse.
- Adding or removing edges may require modifications across the matrix.
Following is the example of an adjacency matrix for an undirected graph −
# Adjacency matrix for the undirected graph # Graph: (0) - (1) - (2) # | # (3) graph = [ [0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0] ]
Adjacency List
An adjacency list consists of an array (or list) of vertices, where each vertex has a list of adjacent vertices (its neighbors). This data structure is implemented using an array or hash map, where each entry corresponds to a vertex and stores a list of its neighbors.
Advantages:
- Space-efficient for sparse graphs, requiring O(V + E) space, where E is the number of edges.
- Efficient for graph traversal algorithms like Depth-First Search (DFS) and Breadth-First Search (BFS).
Disadvantages:
- Checking if there is an edge between two nodes may require O(V) time in the worst case.
Following is the example of an adjacency list for an undirected graph −
# Adjacency list for the undirected graph # Graph: (0) - (1) - (2) # | # (3) graph = { 0: [1, 3], 1: [0, 2], 2: [1], 3: [0] }
Edge List
An edge list is a simple data structure where each edge in the graph is represented as a pair (or tuple) of vertices. The edge list stores all edges in a graph, and each edge is represented by two vertices that are connected.
Advantages:
- Space-efficient for storing the graph, as it only stores edges without needing additional structures for nodes.
- Simple to implement and useful for representing sparse graphs.
Disadvantages:
- Checking for the presence of an edge requires iterating through the entire list, which can be time-consuming for large graphs.
Following is the example of an edge list for an undirected graph −
# Edge list for the undirected graph # Graph: (0) - (1) - (2) # | # (3) edges = [(0, 1), (1, 2), (0, 3)]
Incidence Matrix
An incidence matrix is a two-dimensional matrix that represents the relationship between edges and vertices. The rows represent edges, and the columns represent vertices.
An entry in the matrix is typically 1 if the vertex is incident to the edge, or 0 otherwise. In a directed graph, the entry can be 1 for the source vertex and -1 for the destination vertex.
Advantages:
- Can represent both directed and undirected graphs.
- Useful for algorithms that require edge-related operations.
Disadvantages:
- Requires O(E x V) space, which can be inefficient for large graphs.
Following is the example of an incidence matrix for an undirected graph −
# Incidence matrix for the undirected graph # Graph: (0) - (1) - (2) # | # (3) incidence_matrix = [ [1, -1, 0, 0], # Edge (0, 1) [0, 1, -1, 0], # Edge (1, 2) [1, 0, 0, -1], # Edge (0, 3) ]
Comparison of Graph Data Structures
When choosing a graph data structure, several factors need to be considered, including the type of graph, the operations to be performed, and the efficiency requirements.
Following is a comparison of the most common graph data structures −
Data Structure | Space Complexity | Edge Check Time Complexity | Graph Type |
---|---|---|---|
Adjacency Matrix | O(V2) | O(1) | Dense Graphs |
Adjacency List | O(V + E) | O(V) in the worst case | Sparse Graphs |
Edge List | O(E) | O(E) | Simple Representation |
Incidence Matrix | O(E x V) | O(1) for edge related queries | Both Directed and Undirected Graphs |
Choosing the Right Data Structure
The choice of data structure depends on the type of graph and the operations you need to perform −
- Adjacency Matrix: Best suited for dense graphs where you frequently need to check the presence of edges.
- Adjacency List: Ideal for sparse graphs where you need space efficiency and perform graph traversal operations.
- Edge List: Simple representation, suitable for applications that only need to store edges without requiring fast access to nodes.
- Incidence Matrix: Useful for edge-related operations and for representing both directed and undirected graphs.