Lecture
Coding Quiz - Visit Order (DFS)
In this coding quiz, you will write a program that uses the Depth-First Search (DFS) algorithm to visit all nodes in a graph starting from a specific node.
You will be given an adjacency list representing the nodes of the graph and their connections. Your task is to perform DFS starting from a given node and print the order in which all nodes are visited.
Write Your Code
def solution(graph): # Write your code here return
Constraints
-
The graph is undirected.
-
All nodes are represented by numbers, starting from 1.
-
The graph contains at least one node and a maximum of 100 nodes.
-
There is at most one edge between any two nodes.
-
The starting node is always node 1.
Input/Output Example
Input
-
Adjacency list:
{1: [2, 3], 2: [4], 3: [4], 4: []} -
Starting node:
1
Output
- Visit order:
[1, 2, 4, 3]
Previous lessonDepth First Search (DFS) ImplementationNext lessonWhat is Breadth-First Search (BFS)?
Lessons in this chapter · Graph Data Structures and Greedy Algorithms
- 1. What is a Graph?
- 2. Graph Implementation Techniques
- 3. Multiple-choice quiz
- 4. What is Depth First Search (DFS)?
- 5. Depth First Search (DFS) Implementation
- 6. Coding Quiz - Visit Order (DFS)
- 7. What is Breadth-First Search (BFS)?
- 8. Breadth-First Search (BFS) Implementation Guide
- 9. Coding Quiz - Order of Visits (BFS)
- 10. What are Greedy Algorithms?
- 11. Greedy Algorithm Implementation Guide
- 12. Multiple-choice quiz
- 13. Coding Quiz - ATM Problem
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help