0
0
DSA Cprogramming~20 mins

Cycle Detection in Directed Graph in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cycle Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of cycle detection using DFS
What is the output of the following C code that detects a cycle in a directed graph using DFS?
DSA C
#include <stdio.h>
#include <stdbool.h>

#define V 4

bool dfs(int v, bool graph[V][V], bool visited[V], bool recStack[V]) {
    if(!visited[v]) {
        visited[v] = true;
        recStack[v] = true;

        for(int i = 0; i < V; i++) {
            if(graph[v][i]) {
                if(!visited[i] && dfs(i, graph, visited, recStack))
                    return true;
                else if(recStack[i])
                    return true;
            }
        }
    }
    recStack[v] = false;
    return false;
}

bool isCyclic(bool graph[V][V]) {
    bool visited[V] = {false};
    bool recStack[V] = {false};

    for(int i = 0; i < V; i++) {
        if(dfs(i, graph, visited, recStack))
            return true;
    }
    return false;
}

int main() {
    bool graph[V][V] = {
        {0, 1, 0, 0},
        {0, 0, 1, 0},
        {0, 0, 0, 1},
        {1, 0, 0, 0}
    };

    if(isCyclic(graph))
        printf("Cycle detected\n");
    else
        printf("No cycle detected\n");

    return 0;
}
ACycle detected
BNo cycle detected
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Trace the DFS calls and check if any node is revisited in the current recursion stack.
🧠 Conceptual
intermediate
1:30remaining
Understanding recursion stack in cycle detection
In cycle detection for a directed graph using DFS, what is the purpose of the recursion stack (recStack) array?
ATo keep track of all visited nodes globally
BTo keep track of nodes currently in the recursion path to detect back edges
CTo store the order of nodes visited for topological sort
DTo count the number of edges from each node
Attempts:
2 left
💡 Hint
Think about how a cycle is detected during DFS traversal.
🔧 Debug
advanced
2:00remaining
Identify the error in cycle detection code
What error will occur when running this modified cycle detection code?
DSA C
#include <stdio.h>
#include <stdbool.h>

#define V 3

bool dfs(int v, bool graph[V][V], bool visited[V], bool recStack[V]) {
    visited[v] = true;
    recStack[v] = true;

    for(int i = 0; i < V; i++) {
        if(graph[v][i]) {
            if(!visited[i] && dfs(i, graph, visited, recStack))
                return true;
            else if(recStack[i])
                return true;
        }
    }
    recStack[v] = false;
    return false;
}

bool isCyclic(bool graph[V][V]) {
    bool visited[V] = {false};
    bool recStack[V] = {false};

    for(int i = 0; i < V; i++) {
        if(dfs(i, graph, visited, recStack))
            return true;
    }
    return false;
}

int main() {
    bool graph[V][V] = {
        {0, 1, 0},
        {0, 0, 1},
        {0, 0, 0}
    };

    if(isCyclic(graph))
        printf("Cycle detected\n");
    else
        printf("No cycle detected\n");

    return 0;
}
ANo error, prints 'Cycle detected'
BStack overflow due to infinite recursion
CArray index out of bounds error
DCompilation error due to missing header
Attempts:
2 left
💡 Hint
Check the loop bounds in isCyclic function.
🚀 Application
advanced
1:30remaining
Detect cycle in a graph with multiple disconnected components
Given a directed graph with 5 nodes and edges: 0->1, 1->2, 3->4, 4->3, which statement about cycle detection is true?
AThe graph has a cycle only in the component containing nodes 3 and 4
BThe graph has no cycles because components are disconnected
CThe graph has a cycle in the component containing nodes 0,1,2
DThe graph has cycles in both components
Attempts:
2 left
💡 Hint
Check edges in each disconnected component separately.
🧠 Conceptual
expert
1:30remaining
Why can't BFS alone detect cycles in directed graphs reliably?
Why is BFS not typically used alone to detect cycles in directed graphs?
ABFS can only detect cycles in undirected graphs
BBFS always detects cycles incorrectly in directed graphs
CBFS requires more memory than DFS for cycle detection
DBFS cannot detect back edges which indicate cycles in directed graphs
Attempts:
2 left
💡 Hint
Think about how cycle detection relies on back edges and recursion stack.