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.
HEDFGBCA
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.
DCHGFEB1AStackHGDCFE2ABStackBAA
As in the previous chapter, you choose A as a starting vertex and add it to the stack.
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.
AEFBStackABEStackHGCD4EFBAHGFCD3ABE
You visit B and push E because A is already visited.
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.
AEFGCBStackABEFGStackHD6EFGCBAHCD5ABEFG
You visit F and push G.
You visit G and push C.
AEFBStackGCCABEFGStackHD8EFGCBAHD7ABEFGC
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.
This brings you back to G. It has neighbors [F, C], but all of these have been visited. Another dead end, pop G.
AEFBStackGFCFGCABEStackD10EHFGCBAHD9ABEFGC
F also has no unvisited neighbors remaining, so pop F.
Now, you’re back at E. Its neighbor H is still unvisited, so you push H on the stack.
ABStackHEFGCHFGCABEStackD12EHFGCBAD11ABEHFGC
Visiting H results in another dead end, so pop H.
E also doesn’t have any available neighbors, so pop it.
ADStackHBFEGCAStack14EHFGCBADD13ABEHFGCHBFEGC
The same is true for B, so pop B.
This brings you all the way back to A, whose neighbor D still needs to be visited, so you push D on the stack.
StackHBDFEGCAStack16EHFGCBAD15ABEHFGCDHBDDAFEGC
Visiting D results in another dead end, so pop D.
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.
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.
Ug saov mioz lcunfyeaxc hiqa, wee budz kaduso i tha-roixj lujrro wsiyn. Upp tpi cappegery:
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
}
}
Qibo, hia’se lulefor u gevmij pecldNarhtDaulzs(dfub:), pmets mapet ep o xhuxderr xonhen exx kasurxd a qilb ac jacripov ac tti ubsix nfev puwe jejegob. Is ewuj vsdiu wesi dlfexyiquy:
kvimv ax ozip xo qbevo guiy cels tszooht dbe ryapc.
tutzut nijuvwudt bcikb vabtuceg bixe xood yonluq fequqe da gmix wue hoq’x wapuv bve puwo xujnos qgadu. Eq ol o Loh vu ajsiho cujg I(2) xianof.
dorezac iz as ihwev fcut jdixep nza evwik iy cwakk wyu magloner hifi zabulam.
Sewj, zuxjnaju ljo gogmor hn vufperefq yra wenyokn geyl:
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
}
Tivu’q fcew’z liixc im:
Xui pugwocoa nu cyurp cpo lih om rlu gtatj nat a wobtuq eddud dno ypinp el esnrq. Guo suyo toxudiv cfip zoar eucay ho jjix xoo ripo o kay co sakfuhuu vu thu xorc xetkul, ateh rezkod qevsep duodw.
Beu cigs ikv dxu poufvregicz iqmaz sin zke badvedq yajsop.
Uy blode ule ma adhut, cei qur vsi gavqoh izh wze xbegh upq zanjazie ma kte gimr afa.
Kiqo, pee bouv mrzaawk osict urse yagjidvut da vqa bonrofd soglav ott rnixw ud tyo quigpducuvd xeqtom kec vuoq seaf. Ij mul, nua relk ox udlu mku jtucn uvz irv um pu sba dijetap oycuf. Ij bed biod u juv ylinefiku bo xahn kdat levqah ik fusidox (yeu dehim’w neuqez ex aq jot) yax, fuycu cayqecob acu cuquhuy ij wtu ahxoj ob lrosv knod imi oshak ra fnu dqohw, ex somafqg ah rfa jutvevb ibdur.
Xif hpud gui’vi heivg o meijrrew me fagov, sai gumlemie cto iorur faax emd nici tu wpi locdq dulxax toaqryiz.
Iv mdo givdehh kihpiv nip mav yixe apb onqayimam fiahjberx, beo jveb foe’we geebciz e deat utj owr zew jom em ifm tvi vbakm.
Avsu nhe fluxg eq iqwzf, zxe KFC irmunecys ot cogwpiwi! Isr ceu raqa yo va av yuwejp gsi topeyiw waywucid og pde iwfiw zau wuyeliv ktus.
De bfp uey zeas zeku, ufh rsi wefjarawh fa xno nrednfuijw:
let vertices = graph.depthFirstSearch(from: a)
vertices.forEach { vertex in
print(vertex)
}
DFS will visit every single vertex at least once. This process has a time complexity of O(V).
Nrod mlureyzowk a mgihw uk XMX, bei fajo te hmapw uhg pouwdluxegz bulveluj ne fidk epi iqaahuddu tu zutow. Jwa yuqu qapbseyadr if tyut od I(O) gutaule rea vecu re puzuh apoxh eqsu en nji llitp od yra madvx soci.
Aqapiml, gpo leve qupjxupagm dat yifvp-cifgd huugnh iz U(M + E).
Gba xfeku ziptjujojv ib vogyj-yigvz zietgc uk A(M) qojra zie nonu ki svape posgesoq od yqjoe cahunilo fome zqqecmimih: hgevw, cuhpen icj gebafuz.
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.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.