Custom Graph
About
Graphs are non-linear data structures consisting of vertices (nodes) and edges (connections between nodes). They can represent real-world relationships like social networks, road maps, and computer networks.
Types of Graph Representation
There are multiple ways to implement graphs in a computer program, each with its own advantages and trade-offs. The three most common implementations are:
Adjacency Matrix
Adjacency List
Edge List
Adjacency Matrix Representation
An Adjacency Matrix is a 2D array (V × V matrix) where V is the number of vertices. Each cell (i, j) indicates whether there is an edge between vertex i and vertex j.
Structure
For an unweighted graph, the matrix contains 0 (no edge) or 1 (edge exists).
For a weighted graph, the matrix stores the weight of the edge instead of 1.
Example: Adjacency Matrix for an Undirected Graph
Consider the following undirected graph:
A
0
1
4
0
B
1
0
3
1
C
4
3
0
2
D
0
1
2
0
Adjacency List Representation
An Adjacency List represents a graph as an array of linked lists (or lists in general). Each vertex has a list of all the adjacent vertices (neighbors).
Structure
For the same undirected graph:
The adjacency list would look like:
For a directed graph, the edges point in one direction only.
Adjacency List Representation
Edge List Representation
An Edge List is a list of all edges, where each edge is represented as a pair (or triplet for weighted graphs).
Structure
For the same undirected graph:
The edge list would look like:
1
A
B
1
2
A
C
4
3
B
C
3
4
B
D
1
5
C
D
2
Choosing the Right Implementation
Use Adjacency Matrix when
The graph is dense (i.e., E≈V^2).
Fast edge lookup is required O(1).
The graph doesn't change frequently.
Use Adjacency List when
The graph is sparse (i.e., E≪ V^2E).
Space efficiency is a concern.
Need to iterate over neighbors frequently.
Use Edge List when
The graph changes dynamically (adding/removing edges frequently).
Need a compact edge representation.
Algorithms like Kruskal’s Algorithm (which sorts edges) are required.
Last updated
Was this helpful?