0
0
DSA Cprogramming~10 mins

DFS Depth First Search on Graph in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the DFS function parameter for the graph adjacency list.

DSA C
void dfs(int node, int visited[], vector<vector<int>> [1], int n) {
Drag options to blanks, or click blank then click option'
Alist
Bgraph
Cadj
Dedges
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined or inconsistent with the rest of the code.
2fill in blank
medium

Complete the code to mark the current node as visited inside DFS.

DSA C
visited[node] = [1];
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited[node] as 0 which means unvisited.
3fill in blank
hard

Fix the error in the DFS recursive call to visit adjacent nodes.

DSA C
for(int i = 0; i < adj[node].size(); i++) {
    int adjNode = adj[node][i];
    if(!visited[[1]]) {
        dfs(adjNode, visited, adj, n);
    }
}
Drag options to blanks, or click blank then click option'
AadjNode
Bnode
Ci
Dvisited
Attempts:
3 left
💡 Hint
Common Mistakes
Checking visited[node] instead of visited[adjNode].
Using the loop index 'i' as the node index.
4fill in blank
hard

Fill both blanks to correctly initialize and call DFS for all nodes in the graph.

DSA C
int visited[n];
for(int i = 0; i < n; i++) {
    visited[i] = [1];
}
for(int i = 0; i < n; i++) {
    if(visited[i] == [2]) {
        dfs(i, visited, adj, n);
    }
}
Drag options to blanks, or click blank then click option'
A0
B1
C-1
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing visited array with 1 instead of 0.
Checking for visited[i] == 1 instead of 0.
5fill in blank
hard

Fill all three blanks to complete the DFS function that prints nodes in DFS order.

DSA C
void dfs(int node, int visited[], vector<vector<int>> [1], int n) {
    visited[node] = [2];
    printf("%d ", [3]);
    for(int i = 0; i < adj[node].size(); i++) {
        int adjNode = adj[node][i];
        if(!visited[adjNode]) {
            dfs(adjNode, visited, adj, n);
        }
    }
}
Drag options to blanks, or click blank then click option'
Aadj
B1
Cnode
Dvisited
Attempts:
3 left
💡 Hint
Common Mistakes
Printing visited[node] instead of node.
Using wrong variable name for adjacency list.