0
0
DSA Cprogramming~20 mins

Graph Terminology Vertices Edges Directed Undirected Weighted in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Graph Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adjacency list for directed weighted graph
What is the printed adjacency list after running the code below for a directed weighted graph?
DSA C
typedef struct Edge {
    int dest;
    int weight;
    struct Edge* next;
} Edge;

typedef struct Vertex {
    int id;
    Edge* edges;
} Vertex;

#include <stdio.h>
#include <stdlib.h>

void add_edge(Vertex* graph, int src, int dest, int weight) {
    Edge* new_edge = (Edge*)malloc(sizeof(Edge));
    new_edge->dest = dest;
    new_edge->weight = weight;
    new_edge->next = graph[src].edges;
    graph[src].edges = new_edge;
}

void print_graph(Vertex* graph, int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ->", graph[i].id);
        Edge* temp = graph[i].edges;
        while (temp) {
            printf(" %d(weight=%d)", temp->dest, temp->weight);
            temp = temp->next;
        }
        printf(" -> NULL\n");
    }
}

int main() {
    int n = 3;
    Vertex graph[3];
    for (int i = 0; i < n; i++) {
        graph[i].id = i;
        graph[i].edges = NULL;
    }
    add_edge(graph, 0, 1, 5);
    add_edge(graph, 0, 2, 3);
    add_edge(graph, 1, 2, 2);
    print_graph(graph, n);
    return 0;
}
A
0 -&gt; NULL
1 -&gt; 2(weight=2) -&gt; NULL
2 -&gt; 0(weight=3) 1(weight=5) -&gt; NULL
B
0 -&gt; 1(weight=5) 2(weight=3) -&gt; NULL
1 -&gt; 2(weight=2) -&gt; NULL
2 -&gt; NULL
C
0 -&gt; 1(weight=5) -&gt; NULL
1 -&gt; 2(weight=2) 0(weight=3) -&gt; NULL
2 -&gt; NULL
D
0 -&gt; 2(weight=3) 1(weight=5) -&gt; NULL
1 -&gt; 2(weight=2) -&gt; NULL
2 -&gt; NULL
Attempts:
2 left
💡 Hint
Remember edges are added at the head of the linked list, so the last added edge appears first.
🧠 Conceptual
intermediate
1:00remaining
Number of edges in an undirected graph
If an undirected graph has 5 vertices and each vertex is connected to every other vertex, how many edges does the graph have?
A20
B10
C5
D15
Attempts:
2 left
💡 Hint
In an undirected graph, each edge connects two vertices and is counted once.
🔧 Debug
advanced
1:30remaining
Identify error in directed graph edge addition
What error will occur when running the code below that tries to add an edge in a directed graph?
DSA C
typedef struct Edge {
    int dest;
    struct Edge* next;
} Edge;

typedef struct Vertex {
    int id;
    Edge* edges;
} Vertex;

void add_edge(Vertex* graph, int src, int dest) {
    Edge* new_edge = malloc(sizeof(Edge));
    new_edge->dest = dest;
    new_edge->next = graph[dest].edges;
    graph[src].edges = new_edge;
}

int main() {
    Vertex graph[2];
    graph[0].edges = NULL;
    graph[1].edges = NULL;
    add_edge(graph, 0, 1);
    return 0;
}
ANo error, runs correctly
BCompilation error: missing #include <stdlib.h>
CSegmentation fault due to incorrect next pointer assignment
DMemory leak due to missing free()
Attempts:
2 left
💡 Hint
Check which vertex's edges list is used for the new edge's next pointer.
Predict Output
advanced
1:30remaining
Output of adjacency matrix for weighted undirected graph
What is the printed adjacency matrix after running the code below for a weighted undirected graph?
DSA C
#include <stdio.h>

int main() {
    int n = 3;
    int matrix[3][3] = {0};
    matrix[0][1] = 4;
    matrix[1][0] = 4;
    matrix[1][2] = 7;
    matrix[2][1] = 7;
    matrix[0][2] = 0;
    matrix[2][0] = 0;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    return 0;
}
A
0 4 0 
4 0 7 
0 7 0 
B
0 0 0 
4 0 7 
0 7 0 
C
0 4 0 
0 0 7 
0 7 0 
D
0 4 7 
4 0 7 
7 7 0 
Attempts:
2 left
💡 Hint
Remember adjacency matrix for undirected graph is symmetric.
🧠 Conceptual
expert
1:00remaining
Minimum edges for strongly connected directed graph
What is the minimum number of edges required to make a directed graph with 4 vertices strongly connected?
A4
B6
C3
D12
Attempts:
2 left
💡 Hint
Strongly connected means every vertex reachable from every other vertex.