0
0
DSA Cprogramming~20 mins

Shortest Path in DAG Using Topological Order in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DAG Shortest Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of shortest path distances from source in DAG
What is the output of the following C code that finds shortest paths from source 0 in a DAG using topological order?
DSA C
#include <stdio.h>
#include <limits.h>
#define V 6

void shortestPath(int graph[V][V], int src) {
    int dist[V];
    int visited[V] = {0};
    int stack[V], top = -1;

    // Function to perform DFS and fill stack
    void dfs(int v) {
        visited[v] = 1;
        for (int i = 0; i < V; i++) {
            if (graph[v][i] != INT_MAX && !visited[i]) {
                dfs(i);
            }
        }
        stack[++top] = v;
    }

    for (int i = 0; i < V; i++) {
        if (!visited[i]) dfs(i);
    }

    for (int i = 0; i < V; i++) dist[i] = INT_MAX;
    dist[src] = 0;

    while (top >= 0) {
        int u = stack[top--];
        if (dist[u] != INT_MAX) {
            for (int v = 0; v < V; v++) {
                if (graph[u][v] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
                    dist[v] = dist[u] + graph[u][v];
                }
            }
        }
    }

    for (int i = 0; i < V; i++) {
        if (dist[i] == INT_MAX) printf("INF ");
        else printf("%d ", dist[i]);
    }
    printf("\n");
}

int main() {
    int graph[V][V] = {
        {INT_MAX, 5, 3, INT_MAX, INT_MAX, INT_MAX},
        {INT_MAX, INT_MAX, 2, 6, INT_MAX, INT_MAX},
        {INT_MAX, INT_MAX, INT_MAX, 7, 4, 2},
        {INT_MAX, INT_MAX, INT_MAX, INT_MAX, -1, 1},
        {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, -2},
        {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX}
    };
    shortestPath(graph, 0);
    return 0;
}
A"0 5 3 10 7 5 "
B"0 5 3 9 6 4 "
C"0 INF INF INF INF INF "
D"0 5 3 13 7 5 "
Attempts:
2 left
💡 Hint
Trace the topological order and update distances step-by-step from source 0.
🧠 Conceptual
intermediate
1:30remaining
Why use topological order for shortest path in DAG?
Why is topological order used to find shortest paths in a Directed Acyclic Graph (DAG)?
ABecause it sorts vertices by their distance from the source automatically.
BBecause it ensures we process vertices only after all their predecessors, allowing correct distance updates without cycles.
CBecause it detects cycles in the graph to avoid infinite loops.
DBecause it finds the longest path in the graph first.
Attempts:
2 left
💡 Hint
Think about how processing order affects distance updates in graphs without cycles.
🔧 Debug
advanced
2:00remaining
Identify the bug causing incorrect shortest path output
The following code attempts to find shortest paths in a DAG but produces wrong results. What is the bug?
DSA C
void shortestPath(int graph[V][V], int src) {
    int dist[V];
    int visited[V] = {0};
    int stack[V], top = -1;

    void dfs(int v) {
        visited[v] = 1;
        for (int i = 0; i < V; i++) {
            if (graph[v][i] != INT_MAX && !visited[i]) {
                dfs(i);
            }
        }
        stack[++top] = v;
    }

    for (int i = 0; i < V; i++) {
        if (!visited[i]) dfs(i);
    }

    for (int i = 0; i < V; i++) dist[i] = INT_MAX;
    dist[src] = 0;

    while (top >= 0) {
        int u = stack[top--];
        for (int v = 0; v < V; v++) {
            if (graph[u][v] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
                dist[v] = dist[u] + graph[u][v];
            }
        }
    }

    for (int i = 0; i < V; i++) {
        if (dist[i] == INT_MAX) printf("INF ");
        else printf("%d ", dist[i]);
    }
    printf("\n");
}
AThe graph adjacency matrix uses 0 for no edge instead of INT_MAX.
BThe DFS does not mark nodes as visited, causing infinite recursion.
CThe stack is not used to store topological order.
DThe code does not check if dist[u] is INT_MAX before relaxing edges, causing invalid additions.
Attempts:
2 left
💡 Hint
Check the relaxation step carefully for invalid distance updates.
📝 Syntax
advanced
1:00remaining
Identify the syntax error in the topological sort DFS function
Which option contains the syntax error in this DFS function used for topological sorting?
DSA C
void dfs(int v) {
    visited[v] = 1
    for (int i = 0; i < V; i++) {
        if (graph[v][i] != INT_MAX && !visited[i]) {
            dfs(i);
        }
    }
    stack[++top] = v;
}
AMissing braces around for loop body
BIncorrect increment operator in stack[++top]
CMissing semicolon after visited[v] = 1
DMissing return type for dfs function
Attempts:
2 left
💡 Hint
Look carefully at each line for missing punctuation.
🚀 Application
expert
3:00remaining
Number of shortest paths from source to target in DAG
Given a DAG with weighted edges (all weights positive), how can you modify the shortest path algorithm using topological order to also count the number of distinct shortest paths from source to each vertex?
AMaintain a count array initialized with 0 except source=1; when relaxing edges, if a shorter path is found, update count[v] = count[u]; if equal path length found, add count[u] to count[v].
BUse a BFS instead of topological order and increment count for each visited node.
CStore all paths explicitly in a list and count them after shortest path calculation.
DRun the shortest path algorithm multiple times, each time removing edges used in previous shortest paths.
Attempts:
2 left
💡 Hint
Think about how to track number of ways while updating shortest distances.