0
0
DSA Cprogramming~20 mins

Cycle Detection in Undirected 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 an undirected graph using DFS?
DSA C
#include <stdio.h>
#include <stdbool.h>
#define V 4

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

int main() {
    int graph[V][V] = {
        {0, 1, 0, 0},
        {1, 0, 1, 1},
        {0, 1, 0, 1},
        {0, 1, 1, 0}
    };
    bool visited[V] = {false};
    bool hasCycle = false;
    for (int i = 0; i < V; i++) {
        if (!visited[i]) {
            if (dfs(i, -1, visited, graph)) {
                hasCycle = true;
                break;
            }
        }
    }
    if (hasCycle) {
        printf("Cycle detected\n");
    } else {
        printf("No cycle detected\n");
    }
    return 0;
}
ACycle detected
BCompilation error
CNo cycle detected
DRuntime error
Attempts:
2 left
💡 Hint
Trace the DFS calls and check if any back edge is found that is not the parent.
Predict Output
intermediate
2:00remaining
Output when graph has no cycle
What will be printed by this C code detecting cycles in an undirected graph without any cycles?
DSA C
#include <stdio.h>
#include <stdbool.h>
#define V 3

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

int main() {
    int graph[V][V] = {
        {0, 1, 0},
        {1, 0, 1},
        {0, 1, 0}
    };
    bool visited[V] = {false};
    bool hasCycle = false;
    for (int i = 0; i < V; i++) {
        if (!visited[i]) {
            if (dfs(i, -1, visited, graph)) {
                hasCycle = true;
                break;
            }
        }
    }
    if (hasCycle) {
        printf("Cycle detected\n");
    } else {
        printf("No cycle detected\n");
    }
    return 0;
}
ACycle detected
BNo cycle detected
CSegmentation fault
DCompilation error
Attempts:
2 left
💡 Hint
Check if any back edge exists that is not the parent.
🔧 Debug
advanced
2:00remaining
Identify the bug causing incorrect cycle detection
This code is intended to detect cycles in an undirected graph using DFS. What is the bug causing it to always print "No cycle detected" even when a cycle exists?
DSA C
#include <stdio.h>
#include <stdbool.h>
#define V 3

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

int main() {
    int graph[V][V] = {
        {0, 1, 1},
        {1, 0, 1},
        {1, 1, 0}
    };
    bool visited[V] = {false};
    bool hasCycle = false;
    for (int i = 0; i < V; i++) {
        if (!visited[i]) {
            if (dfs(i, -1, visited, graph)) {
                hasCycle = true;
                break;
            }
        }
    }
    if (hasCycle) {
        printf("Cycle detected\n");
    } else {
        printf("No cycle detected\n");
    }
    return 0;
}
AThe recursive DFS call result is not returned, so true is never propagated up
BThe visited array is not initialized properly
CThe graph adjacency matrix is incorrect
DThe parent parameter is not used in the DFS function
Attempts:
2 left
💡 Hint
Check if the recursive DFS calls return values are used properly.
🧠 Conceptual
advanced
1:30remaining
Why is parent tracking necessary in cycle detection DFS?
In cycle detection for an undirected graph using DFS, why do we pass the parent node as a parameter and check if a visited neighbor is not the parent?
ATo store the path of the DFS traversal
BTo mark the parent node as visited
CTo avoid counting the edge back to the parent as a cycle
DTo prevent visiting the parent node again in BFS
Attempts:
2 left
💡 Hint
Think about why an edge to the immediate previous node should not be considered a cycle.
🚀 Application
expert
3:00remaining
Number of connected components with cycles
Given an undirected graph represented by adjacency matrix below, how many connected components contain at least one cycle?
DSA C
#define V 7
int graph[V][V] = {
    {0,1,0,0,0,0,0},
    {1,0,1,0,0,0,0},
    {0,1,0,0,0,0,0},
    {0,0,0,0,1,1,0},
    {0,0,0,1,0,1,0},
    {0,0,0,1,1,0,0},
    {0,0,0,0,0,0,0}
};
A0
B3
C2
D1
Attempts:
2 left
💡 Hint
Identify connected components and check each for cycles using DFS.