Here is the DFS algorithm step by step:
function dfs(u):
visited[u] = true
process(u)
for each neighbor v of u:
if not visited[v]:
dfs(v)
Start at vertex . Mark as visited so you do not come back. Process (print it, add to result, whatever your problem needs). Loop through all neighbors of . For each neighbor , if is not visited, recursively call DFS on .
The recursion handles backtracking automatically. When you finish exploring all paths from , the recursive call returns and you continue with the next neighbor.