0
0
DSA Cprogramming~20 mins

Adjacency List Representation in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Adjacency List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adjacency list after adding edges
What is the printed adjacency list after running the following C code that adds edges to an undirected graph?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define V 4

typedef struct Node {
    int dest;
    struct Node* next;
} Node;

Node* createNode(int dest) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->dest = dest;
    newNode->next = NULL;
    return newNode;
}

void addEdge(Node* adjList[], int src, int dest) {
    Node* newNode = createNode(dest);
    newNode->next = adjList[src];
    adjList[src] = newNode;

    newNode = createNode(src);
    newNode->next = adjList[dest];
    adjList[dest] = newNode;
}

void printGraph(Node* adjList[]) {
    for (int i = 0; i < V; i++) {
        printf("%d: ", i);
        Node* temp = adjList[i];
        while (temp) {
            printf("%d -> ", temp->dest);
            temp = temp->next;
        }
        printf("NULL\n");
    }
}

int main() {
    Node* adjList[V] = {NULL};
    addEdge(adjList, 0, 1);
    addEdge(adjList, 0, 2);
    addEdge(adjList, 1, 2);
    addEdge(adjList, 2, 3);
    printGraph(adjList);
    return 0;
}
A
0: 2 -&gt; 1 -&gt; NULL
1: 2 -&gt; 0 -&gt; NULL
2: 3 -&gt; 1 -&gt; 0 -&gt; NULL
3: 2 -&gt; NULL
B
0: 1 -&gt; 2 -&gt; NULL
1: 0 -&gt; 2 -&gt; NULL
2: 1 -&gt; 0 -&gt; 3 -&gt; NULL
3: 2 -&gt; NULL
C
0: 2 -&gt; 1 -&gt; NULL
1: 0 -&gt; 2 -&gt; NULL
2: 3 -&gt; 1 -&gt; 0 -&gt; NULL
3: 2 -&gt; NULL
D
0: 1 -&gt; 2 -&gt; NULL
1: 2 -&gt; 0 -&gt; NULL
2: 3 -&gt; 0 -&gt; 1 -&gt; NULL
3: 2 -&gt; NULL
Attempts:
2 left
💡 Hint
Remember that new nodes are added at the head of the adjacency list for each vertex.
🧠 Conceptual
intermediate
1:00remaining
Number of edges in adjacency list
Given an undirected graph represented by an adjacency list with 5 vertices, the total number of nodes in all adjacency lists combined is 8. How many edges does the graph have?
A5
B8
C16
D4
Attempts:
2 left
💡 Hint
Each edge in an undirected graph appears twice in the adjacency list.
🔧 Debug
advanced
1:30remaining
Identify the error in adjacency list insertion
What error will occur when running this C code snippet that tries to add an edge to an adjacency list?
DSA C
typedef struct Node {
    int dest;
    struct Node* next;
} Node;

void addEdge(Node* adjList[], int src, int dest) {
    Node* newNode = malloc(sizeof(Node));
    newNode->dest = dest;
    newNode->next = adjList[src];
    adjList[src] = newNode;

    newNode->dest = src;
    newNode->next = adjList[dest];
    adjList[dest] = newNode;
}
ANo error, code runs correctly
BSegmentation fault due to using the same node twice
CCompilation error: missing #include <stdlib.h>
DMemory leak due to not freeing nodes
Attempts:
2 left
💡 Hint
Look at how newNode is reused for both adjacency lists.
Predict Output
advanced
2:00remaining
Output after removing an edge from adjacency list
What is the adjacency list printed after removing the edge between vertices 1 and 2 from the graph below?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define V 3

typedef struct Node {
    int dest;
    struct Node* next;
} Node;

Node* createNode(int dest) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->dest = dest;
    newNode->next = NULL;
    return newNode;
}

void addEdge(Node* adjList[], int src, int dest) {
    Node* newNode = createNode(dest);
    newNode->next = adjList[src];
    adjList[src] = newNode;

    newNode = createNode(src);
    newNode->next = adjList[dest];
    adjList[dest] = newNode;
}

void removeEdge(Node* adjList[], int src, int dest) {
    Node **curr = &adjList[src];
    while (*curr && (*curr)->dest != dest) {
        curr = &(*curr)->next;
    }
    if (*curr) {
        Node* temp = *curr;
        *curr = temp->next;
        free(temp);
    }

    curr = &adjList[dest];
    while (*curr && (*curr)->dest != src) {
        curr = &(*curr)->next;
    }
    if (*curr) {
        Node* temp = *curr;
        *curr = temp->next;
        free(temp);
    }
}

void printGraph(Node* adjList[]) {
    for (int i = 0; i < V; i++) {
        printf("%d: ", i);
        Node* temp = adjList[i];
        while (temp) {
            printf("%d -> ", temp->dest);
            temp = temp->next;
        }
        printf("NULL\n");
    }
}

int main() {
    Node* adjList[V] = {NULL};
    addEdge(adjList, 0, 1);
    addEdge(adjList, 1, 2);
    addEdge(adjList, 0, 2);
    removeEdge(adjList, 1, 2);
    printGraph(adjList);
    return 0;
}
A
0: 2 -&gt; 1 -&gt; NULL
1: 0 -&gt; NULL
2: 0 -&gt; NULL
B
0: 1 -&gt; 2 -&gt; NULL
1: 2 -&gt; NULL
2: 1 -&gt; 0 -&gt; NULL
C
0: 2 -&gt; 1 -&gt; NULL
1: 2 -&gt; 0 -&gt; NULL
2: 1 -&gt; 0 -&gt; NULL
D
0: 1 -&gt; 2 -&gt; NULL
1: 0 -&gt; NULL
2: 0 -&gt; NULL
Attempts:
2 left
💡 Hint
Removing edge 1-2 means removing 2 from 1's list and 1 from 2's list.
🧠 Conceptual
expert
1:30remaining
Memory complexity of adjacency list vs adjacency matrix
For a graph with V vertices and E edges, which statement about memory usage is correct?
AAdjacency list uses O(V^2) memory; adjacency matrix uses O(V + E) memory
BBoth adjacency list and adjacency matrix use O(V + E) memory
CAdjacency list uses O(V + E) memory; adjacency matrix uses O(V^2) memory
DBoth adjacency list and adjacency matrix use O(V^2) memory
Attempts:
2 left
💡 Hint
Adjacency matrix stores all possible edges, adjacency list stores only existing edges.