0
0
DSA Cprogramming~3 mins

Why Adjacency Matrix Representation in DSA C?

Choose your learning style9 modes available
The Big Idea

What if you could instantly know if two people are connected without searching through endless lists?

The Scenario

Imagine you want to keep track of friendships in a group of 100 people by writing down who is friends with whom on a big sheet of paper.

You try to list every pair manually to see if they are friends or not.

The Problem

Writing down every pair manually is slow and confusing.

It is easy to miss some pairs or write wrong information.

Finding if two people are friends takes searching through a long list.

The Solution

An adjacency matrix is like a neat table where rows and columns represent people.

Each cell tells if two people are friends with a simple yes or no.

This makes checking friendships quick and organized.

Before vs After
Before
int isFriend(int personA, int personB, int friendships[][2], int size) {
    for (int i = 0; i < size; i++) {
        if ((friendships[i][0] == personA && friendships[i][1] == personB) ||
            (friendships[i][0] == personB && friendships[i][1] == personA)) {
            return 1;
        }
    }
    return 0;
}
After
int adjacencyMatrix[MAX][MAX];

int isFriend(int personA, int personB) {
    return adjacencyMatrix[personA][personB];
}
What It Enables

It enables instant answers to connection questions between any two points in a network.

Real Life Example

Social media platforms use adjacency matrices to quickly find if two users are connected or to suggest new friends.

Key Takeaways

Manual listing of connections is slow and error-prone.

Adjacency matrix stores connections in a clear, fast-access table.

It makes checking relationships between nodes very quick.