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]
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.