0
0
DSA Cprogramming~20 mins

Adjacency Matrix Representation in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Adjacency Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adjacency matrix after adding edges
What is the printed adjacency matrix after adding edges between nodes 0-1, 1-2, and 2-0 in a graph with 3 nodes?
DSA C
#include <stdio.h>
#define N 3

int main() {
    int adj[N][N] = {0};
    adj[0][1] = 1;
    adj[1][2] = 1;
    adj[2][0] = 1;

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", adj[i][j]);
        }
        printf("\n");
    }
    return 0;
}
A[[0,1,0],[0,0,1],[1,0,0]]
B[[0,1,0],[1,0,1],[1,0,0]]
C[[0,1,0],[0,0,1],[0,0,0]]
D[[0,1,1],[0,0,1],[1,0,0]]
Attempts:
2 left
💡 Hint
Check which pairs have 1s in the matrix based on edges added.
Predict Output
intermediate
2:00remaining
Output after removing an edge in adjacency matrix
Given a 4-node graph with edges 0-1, 1-2, 2-3, and 3-0, what is the adjacency matrix after removing the edge 1-2?
DSA C
#include <stdio.h>
#define N 4

int main() {
    int adj[N][N] = {0};
    adj[0][1] = 1;
    adj[1][2] = 1;
    adj[2][3] = 1;
    adj[3][0] = 1;

    adj[1][2] = 0; // remove edge

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", adj[i][j]);
        }
        printf("\n");
    }
    return 0;
}
A[[0,1,0,0],[0,0,1,0],[0,0,0,1],[1,0,0,0]]
B[[0,1,0,0],[0,0,0,0],[0,0,0,1],[1,0,0,0]]
C[[0,1,0,0],[0,0,0,1],[0,0,0,1],[1,0,0,0]]
D[[0,1,0,0],[0,0,0,0],[0,1,0,1],[1,0,0,0]]
Attempts:
2 left
💡 Hint
Look carefully at the row for node 1 after removal.
🔧 Debug
advanced
2:00remaining
Identify the error in adjacency matrix initialization
What error will this code produce when compiling or running?
DSA C
#include <stdio.h>
#define N 3

int main() {
    int adj[N][N];
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            adj[i][j] = 0;
        }
    }
    return 0;
}
ARuns without error and initializes matrix
BCompilation error: array index out of bounds
CRuntime error: segmentation fault due to out-of-bounds access
DLogical error: matrix not fully initialized
Attempts:
2 left
💡 Hint
Check the loop conditions and array size carefully.
🧠 Conceptual
advanced
2:00remaining
Memory usage of adjacency matrix vs adjacency list
Which statement correctly compares memory usage of adjacency matrix and adjacency list for a graph with V vertices and E edges?
AAdjacency matrix always uses less memory than adjacency list
BAdjacency list uses O(V^2) memory, adjacency matrix uses O(V+E)
CBoth use the same memory regardless of edges
DAdjacency matrix uses O(V^2) memory, adjacency list uses O(V+E)
Attempts:
2 left
💡 Hint
Think about how adjacency matrix stores all possible edges.
Predict Output
expert
3:00remaining
Output of adjacency matrix after symmetric edge insertion
What is the adjacency matrix output after inserting edges to make an undirected graph with edges between nodes 0-1, 1-2, and 2-0 in a 3-node graph?
DSA C
#include <stdio.h>
#define N 3

int main() {
    int adj[N][N] = {0};
    int edges[3][2] = {{0,1}, {1,2}, {2,0}};

    for (int i = 0; i < 3; i++) {
        int u = edges[i][0];
        int v = edges[i][1];
        adj[u][v] = 1;
        adj[v][u] = 1;
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d ", adj[i][j]);
        }
        printf("\n");
    }
    return 0;
}
A[[0,1,1],[1,0,1],[1,1,0]]
B[[0,1,0],[1,0,1],[1,0,0]]
C[[0,1,1],[0,0,1],[1,1,0]]
D[[0,0,1],[1,0,1],[1,1,0]]
Attempts:
2 left
💡 Hint
Undirected edges mean symmetric entries in the matrix.