Chapters

Hide chapters

Data Structures & Algorithms in Swift

Fourth Edition · iOS 15 · Swift 5.5 · Xcode 13

40. Depth-First Search
Written by Vincent Ngo

In the previous chapter, you looked at breadth-first search (BFS), in which you had to explore every neighbor of a vertex before going to the next level. In this chapter, you will look at depth-first search (DFS), another algorithm for traversing or searching a graph.

There are a lot of applications for DFS:

  • Topological sorting.
  • Detecting a cycle.
  • Pathfinding, such as in maze puzzles.
  • Finding connected components in a sparse graph.

To perform a DFS, you start with a given source vertex and attempt to explore a branch as far as possible until you reach the end. At this point, you would backtrack (move a step back) and explore the next available branch until you find what you are looking for or until you’ve visited all the vertices.

Example

Let’s go through a DFS example. The example graph below is the same as the previous chapter. This is so you can see the difference between BFS and DFS.

H E D F G B C A

You will use a stack to keep track of the levels you move through. The stack’s last-in-first-out approach helps with backtracking. Every push on the stack means that you move one level deeper. You can pop to return to a previous level if you reach a dead end.

D C H G F E B 1 A Stack H G D C F E 2 A B Stack B A A

  1. As in the previous chapter, you choose A as a starting vertex and add it to the stack.
  2. As long as the stack is not empty, you visit the top vertex on the stack and push the first neighboring vertex that has yet to be visited. In this case, you visit A and push B.

Recall from the previous chapter that the order in which you add edges influences the result of a search. In this case, the first edge added to A was an edge to B, so B is pushed first.

A E F B Stack A B E Stack H G C D 4 E F B A H G F C D 3 A B E

  1. You visit B and push E because A is already visited.
  2. You visit E and push F.

Note that every time you push on the stack, you advance farther down a branch. Instead of visiting every adjacent vertex, you continue down a path until you reach the end and then backtrack.

A E F G C B Stack A B E F G Stack H D 6 E F G C B A H C D 5 A B E F G

  1. You visit F and push G.

  2. You visit G and push C.

A E F B Stack G C C A B E F G Stack H D 8 E F G C B A H D 7 A B E F G C

  1. The next vertex to visit is C. It has neighbors [A, F, G], but all of these have been visited. You have reached a dead end, so it’s time to backtrack by popping C off the stack.

  2. This brings you back to G. It has neighbors [F, C], but all of these have been visited. Another dead end, pop G.

A E F B Stack G F C F G C A B E Stack D 10 E H F G C B A H D 9 A B E F G C

  1. F also has no unvisited neighbors remaining, so pop F.

  2. Now, you’re back at E. Its neighbor H is still unvisited, so you push H on the stack.

A B Stack H E F G C H F G C A B E Stack D 12 E H F G C B A D 11 A B E H F G C

  1. Visiting H results in another dead end, so pop H.
  2. E also doesn’t have any available neighbors, so pop it.

A D Stack H B F E G C A Stack 14 E H F G C B A D D 13 A B E H F G C H B F E G C

  1. The same is true for B, so pop B.

  2. This brings you all the way back to A, whose neighbor D still needs to be visited, so you push D on the stack.

Stack H B D F E G C A Stack 16 E H F G C B A D 15 A B E H F G C D H B D D A F E G C

  1. Visiting D results in another dead end, so pop D.
  2. You’re back at A, but this time, there are no available neighbors to push, so you pop A. The stack is now empty and the DFS is complete.

When exploring the vertices, you can construct a tree-like structure, showing the branches you’ve visited. You can see how deep DFS went compared to BFS.

Breadth-First Search A H B D C E F G H A B E D F G C Depth-First Search

Implementation

Open up the starter playground for this chapter. This playground contains an implementation of a graph, as well as a stack, which you’ll use to implement DFS.

In your main playground file, you will notice a pre-built sample graph. Add the following:

extension Graph where Element: Hashable {

  func depthFirstSearch(from source: Vertex<Element>)
      -> [Vertex<Element>] {
    var stack: Stack<Vertex<Element>> = []
    var pushed: Set<Vertex<Element>> = []
    var visited: [Vertex<Element>] = []

    stack.push(source)
    pushed.insert(source)
    visited.append(source)

    // more to come ...

    return visited
  }
}

Here, you’ve defined a method depthFirstSearch(from:), which takes in a starting vertex and returns a list of vertices in the order they were visited. It uses three data structures:

  1. stack is used to store your path through the graph.
  2. pushed remembers which vertices have been pushed before so that you don’t visit the same vertex twice. It is a Set to ensure fast O(1) lookup.
  3. visited is an array that stores the order in which the vertices were visited.

To start the algorithm, you add the source vertex to all three.

Next, complete the method by replacing the comment with:

outer: while let vertex = stack.peek() { // 1
  let neighbors = edges(from: vertex) // 2
  guard !neighbors.isEmpty else { // 3
    stack.pop()
    continue
  }
  for edge in neighbors { // 4
    if !pushed.contains(edge.destination) {
      stack.push(edge.destination)
      pushed.insert(edge.destination)
      visited.append(edge.destination)
      continue outer // 5
    }
  }
  stack.pop() // 6
}

Here’s what’s going on:

  1. You continue to check the top of the stack for a vertex until the stack is empty. You have labeled this loop outer so that you have a way to continue to the next vertex, even within nested loops.
  2. You find all the neighboring edges for the current vertex.
  3. If there are no edges, you pop the vertex off the stack and continue to the next one.
  4. Here, you loop through every edge connected to the current vertex and check if the neighboring vertex has been seen. If not, you push it onto the stack and add it to the visited array. It may seem a bit premature to mark this vertex as visited (you haven’t peeked at it yet) but, since vertices are visited in the order in which they are added to the stack, it results in the correct order.
  5. Now that you’ve found a neighbor to visit, you continue the outer loop and move to the newly pushed neighbor.
  6. If the current vertex did not have any unvisited neighbors, you know you’ve reached a dead end and can pop it off the stack.

Once the stack is empty, the DFS algorithm is complete! All you have to do is return the visited vertices in the order you visited them.

To try out your code, add the following to the playground:

let vertices = graph.depthFirstSearch(from: a)
vertices.forEach { vertex in
  print(vertex)
}

Notice that the order of the visited nodes using a DFS:

0: A
1: B
4: E
5: F
6: G
2: C
7: H
3: D

Performance

DFS will visit every single vertex at least once. This process has a time complexity of O(V).

When traversing a graph in DFS, you have to check all neighboring vertices to find one available to visit. The time complexity of this is O(E) because you have to visit every edge in the graph in the worst case.

Overall, the time complexity for depth-first search is O(V + E).

The space complexity of depth-first search is O(V) since you have to store vertices in three separate data structures: stack, pushed and visited.

Key points

  • Depth-first search (DFS) is another algorithm to traverse or search a graph.
  • DFS explores a branch as far as possible until it reaches the end.
  • Leverage a stack data structure to keep track of how deep you are in the graph. Only pop off the stack when you reach a dead end.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.