Representation of Graph Data Structure"๐
"Representation of Graph Data Structure"๐
A graph data structure represents relationships between entities. It consists of:
-
Vertices (Nodes): entities
-
Edges (Links): connections between entities
There are several common ways to represent a graph, each with trade-offs.
1. Adjacency Matrix
A 2D array V × V where V is the number of vertices.
Example
๐Graph with vertices A, B, C:
-
1indicates an edge,0indicates no edge -
For weighted graphs, store weights instead of
1
Properties
-
Space: O(V²)
-
Edge lookup: O(1)
-
Best for: Dense graphs
2. Adjacency List
Each vertex stores a list of adjacent vertices.
Example
Properties
-
Space: O(V + E)
-
Edge lookup: O(deg(V))
-
Best for: Sparse graphs
3. Edge List
Graph is represented as a list of edges.
Example
Properties
-
Space: O(E)
-
Edge lookup: O(E)
-
Best for: Simple algorithms like Kruskal’s
4. Incidence Matrix
A matrix with rows as vertices and columns as edges.
Example
Properties
-
Space: O(V × E)
-
Used in: Mathematical graph theory
Comparison Table
| Representation | Space | Edge Check | Best Use Case |
|---|---|---|---|
| Adjacency Matrix | O(V²) | O(1) | Dense graphs |
| Adjacency List | O(V + E) | O(deg V) | Sparse graphs |
| Edge List | O(E) | O(E) | MST, simple input |
| Incidence Matrix | O(V × E) | O(E) | Theoretical graphs |
๐Directed vs Undirected
-
Undirected graph: edges are bidirectional
-
Directed graph: edges have direction (u → v).
๐Representation of Graph Data Structure (Short)
A graph is represented using vertices (nodes) and edges (connections). Common representations are:
-
Adjacency Matrix
-
Uses a 2D array
-
1means edge exists,0means no edge -
Best for dense graphs
-
-
Adjacency List
-
Each vertex stores a list of connected vertices
-
Saves memory
-
Best for sparse graphs
-
-
Edge List
-
Graph stored as a list of
(u, v)pairs -
Simple and easy to use
-
Used in Kruskal’s algorithm
-
-
Incidence Matrix
-
Rows = vertices, columns = edges
-
Mainly used in theory
-
Comments
Post a Comment