0
0
DSA Cprogramming~3 mins

Why Adjacency List Representation in DSA C?

Choose your learning style9 modes available
The Big Idea

What if you could store millions of connections without drowning in data?

The Scenario

Imagine you want to keep track of friendships in a large school. You try writing down every pair of friends on a big sheet of paper, listing all possible pairs. As the school grows, this list becomes huge and hard to manage.

The Problem

Writing down every possible pair wastes a lot of space and time, especially when most students have only a few friends. Searching for a friend or adding a new friendship becomes slow and confusing.

The Solution

Using an adjacency list, you keep a small list for each student showing only their friends. This way, you save space and quickly find or add friends without scanning a huge list.

Before vs After
Before
int graph[100][100]; // matrix for all pairs
// Check all pairs even if no friendship
for (int i = 0; i < n; i++) {
  for (int j = 0; j < n; j++) {
    if (graph[i][j] == 1) { /* friend */ }
  }
}
After
struct Node {
  int friend_id;
  struct Node* next;
};
struct Node* adjacencyList[100];
// Traverse only friends of a student
struct Node* current = adjacencyList[student];
while (current != NULL) {
  // process current->friend_id
  current = current->next;
}
What It Enables

It enables efficient storage and quick access to connections in large networks without wasting space.

Real Life Example

Social media platforms use adjacency lists to store user connections, so they can quickly show your friends and suggest new ones.

Key Takeaways

Manual pair lists waste space and slow down operations.

Adjacency lists store only actual connections, saving space.

This makes managing large networks fast and efficient.