Docsity

NOTHING MUCH NOTHING MUCH, Study notes of Computer science

Partial preview of the text

Download NOTHING MUCH NOTHING MUCH and more Study notes Computer science in PDF only on Docsity!

Depth-First Search

CS260 Data Structures

Galen Long

Winter 2025-

Table of contents

Depth-first search (DFS) pseudocode

DFS example

DFS use cases

DFS runtime

DFS

▶ (^) Explores entire graph, start node is arbitrary ▶ (^) Discovers nodes via descendant chains using stack, “lightning” ▶ (^) Called depth-first search , but think of it more like traversal ▶ (^) Often used on digraphs

DFS pseudocode

/* G is graph */ function DFS(G) { global time = 0 // global variable for (u in G.vertices) { u.color = WHITE u.p = NIL } for (u in G.vertices) { if (u.color == WHITE) { DFSVisit(G, u) } } }

function DFSVisit(G, u) { time++ u.d = time u.color = GRAY for (v in G.adjList[u]) { if (v.color == WHITE) { v.p = u DFSVisit(G, v) } } u.color = BLACK time++ u.f = time }

DFS pseudocode

▶ (^) DFS starts with arbitrary vertex ▶ (^) DFSVisit visits neighbors in arbitrary order ▶ (^) All nodes will be discovered regardless of whether graph is connected or not because of loop in DFS ▶ (^) No u.d or u.f values will be the same because we increment the global time variable before each time we use it ▶ (^) Uses stack (recursive call stack) to control order of iteration ▶ (^) Visits entire descendant chain before recursing back up to explore other neighbors

Aside: How to do “global” variables in C?

function DFS(G) { int time = 0; // "global" variable // ... for (u in G.vertices) { // ... { DFSVisit(G, u, &time); } } } function DFSVisit(G, u, int time) { (time)++; u.d = time; // ... for (v in G.adjList[u]) { // ... { DFSVisit(G, v, time); } } // ... (time)++; u.f = *time; }

▶ (^) DFS declares 1 int, passes address of int (not int itself) to DFSVisit ▶ (^) Thus, when DFSVisit mutates *time ( not time), changes persist between recursive calls

DFS example

DFS example∗

Node p d f a NIL 1 12 b a 2 11 c d 4 5 d b 3 6 e f 8 9 f b 7 10 g NIL 13 14

∗Assumes we visit nodes in alphabetical order whenever the order is arbitrary.

DFS use cases

▶ (^) Like BFS, DFS on its own just stores information in each node ▶ (^) Can then do/learn different things with that information

DFS intervals

Node p d f a NIL 1 12 b a 2 11 c d 4 5 d b 3 6 e f 8 9 f b 7 10 g NIL 13 14 ▶ (^) Draw [ d, f ] intervals for each node in increasing order of d ▶ (^) Intervals will never overlap, “parenthesis structure” ▶ (^) If u ’s interval contains v ’s interval, v is descendant of u ▶ (^) If u, v intervals disjoint and u discovered before v , u doesn’t have path to v ▶ (^) Different traversal orders give different trees ▶ (^) Not all descendant chains necessarily discovered

DFS forest

Green edges are tree edges. Red edges are back edges. All other edges are forward/cross edges.

Node p d f a NIL 1 12 b a 2 11 c d 4 5 d b 3 6 e f 8 9 f b 7 10 g NIL 13 14

DFS use cases

▶ (^) Can be modified to check for cycles in digraphs ▶ (^) Can also find cycles in undirected graphs, but we can use BFS for that, too ▶ (^) Does not find shortest paths ▶ (^) Used as subroutine in other algorithms∗ ▶ (^) Topological sort ▶ (^) Kosaraju-Sharir’s algorithm ▶ (^) Can check if graph is strongly connected

∗More on these later.

DFS runtime

Analysis is identical to BFS.∗ Runtime: Θ( V + E )

∗Except we always visit every node even if the graph is disconnected.