Depth First Search
Depth First Search
function pathTo(t):
path := []
while parent[t] != t:
path.push(t)
t = parent[t]
path.push(t)
reverse(path)
return path parent = [0,3,1,0,1,2,-1,-1,4]
Depth-first search (DFS)
● We define the dfs(cur) function that traverses all not-yet-visited vertices
reachable from cur without going through visited, and then returns.
● To do that, we check all unvisited neighbors to and call dfs(to) recursively.
● We have a global array to check is a vertex is visited.
● Running time is O(|V|+|E|).
To traverse the whole graph
function DFS(cur):
regardless of connectivity from s,
visited[cur] := true use a series of DFS calls:
for to in neighbors(cur):
visited[] ← false
if not visited[to]:
for s in V:
DFS(to) if not visited[s]:
DFS(s)
DFS: parents, three colors and in-out times
● Define three colors of vertices function DFS(cur, p):
○ WHITE: the vertex is not visited yet parent[cur] := p
○ GRAY: we entered the vertex, but not yet exited color[cur] := GRAY
(the call dfs(v) is in the stack) tin[cur] := T++
○ BLACK: we exited the vertex for to in neighbors(cur):
● visited[v] == (color[v] != WHITE) if color[to] == WHITE:
DFS(to, cur)
● The starting node s and the colors of all vertices
color[cur] := BLACK
are enough to continue the DFS tout[cur] := T++
● Define two values for each vertex
○ tin[v]: time when we entered v main:
(its color changes from WHITE to GRAY) parent[] ← -1
○ tout[v]: time when we exited v color[] ← WHITE
(its color changes from GRAY to BLACK) DFS(s, s)
DFS example
Example (s=0):
DFS example
Example (s=0):
⇒
Time Segments
● For any two vertices u and v, time segments [tin[u], tout[u]],
[tin[v], tout[v]] give the relation of u and v in the DFS-tree:
○ [tin[u], tout[u]] nested in [tin[v], tout[v]] denotes u being a descendant of v,
○ [tin[v], tout[v]] not intersecting [tin[u], tout[u]] denotes u and v being not
comparable.
for s in V:
if not visited[s]:
topsort(s)
reverse(order)
Bipartite checking
● An undirected graph is bipartite, if we can color the vertices in two colors, so
that no edge connects two vertices of the same color
● A graph is bipartite if and only if it does not have a cycle of odd length
● Choose the color of any vertex and color the others with a DFS
● On any contradiction, break
● dfs(cur, cur_color):
if visited[cur]:
if color[cur] != cur_color: not bipartite
return
visited[cur] = True, color[cur] = cur_color
for to in neighbors(cur):
dfs(to, cur_color ^ 1)
Bridges
● For an undirected graph, a bridge is an edge of the graph whose deletion
increases the number of connected components.
● Criterion. An edge is a bridge if and only if it does not belong to any simple
cycle.
Bridges
● Let’s run a DFS.
● Only tree edges could be bridges.
● How to check if a tree edge is a bridge?
● Definition. Forward-up value, fup[u] is the smallest
depth we can reach from u by traversing zero or more
tree edges, at the end, zero or one backward edge.
● Depth d[v] is the distance from the root in tree edges.
Bridges
● Example