Depth-first search (DFS) is an algorithm for traversing tree and graph data structures. It starts at the root node and explores as far as possible along each branch before backtracking. It continues visiting neighbors in a recursive pattern, recursively visiting all unvisited neighbors of each visited vertex before backtracking. The DFS algorithm works by starting with any vertex on a stack, visiting and removing it from the stack while adding its unvisited neighbors to the top of the stack, repeating until the stack is empty.