Depth-first search algorithm
Depth-first search (DFS) is a search algorithm that explores the search tree by going as deep as possible along each branch before backtracking. This means that it explores each branch of the search tree as far as possible before moving…