Linked Representation
What Is a Linked Representation?
In this method, the graph is stored using linked lists.
For each vertex, we create a separate linked list that stores the nodes that are directly connected to it.
So:
- One vertex = one linked list
- Each node in the list = one neighbor
- All lists together = your entire graph
Itโs like a directory where each person has their own contact list.
โ๏ธ Simple Example Graph
Letโs take this small undirected graph:
A ---- B
| |
C ---- D
Edges: AโB, AโC, BโD, CโD
๐ Linked Representation (Adjacency List Diagram)
Hereโs how the linked representation might look:
Vertex Linked List
------- -----------------------
A โ B โ C โ NULL
B โ A โ D โ NULL
C โ A โ D โ NULL
D โ B โ C โ NULL
Each row shows a vertex and the list of vertices directly connected to it.
This is clean, simple, and space-efficient.
๐ง How It Works Internally
Inside memory, each vertex has a small structure like:
struct Node {
int vertex;
Node* next;
};
For each vertex:
- We create a head pointer
- That pointer leads to a chain (linked list) of its neighbors
So the entire graph becomes a bunch of small linked lists connected side-by-side.
๐ฏ Why Is Linked Representation Useful?
โ 1. Saves space
You only store existing edges.
No huge matrix filled with zeros.
โ 2. Easy to add or remove edges
Just modify the linked list.
โ 3. Perfect for sparse graphs
If your graph has very few edges, this method is ideal.
โ One limitation
Finding if two nodes are connected takes longer compared to a matrix, because you must search through the list.
But for most practical cases, the space savings are worth it.
๐ฑ Explaining It Even More Simply
Imagine each vertex as a small โhomepage.โ
On this homepage, it lists only the links (edges) pointing to other pages (vertices).
This structure feels natural and tidy โ just like a contact list or a friend list on a social app.
