Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Traversing a graph: BFS and DFS, Schemes and Mind Maps of Data Structures and Algorithms

There are two standard (and simple) ways of traversing all vertices/edges in a graph in a systematic way: BFS and DFS. • Most fundamental algorithms on ...

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/12/2022

anoushka
anoushka 🇺🇸

4.1

(15)

241 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Traversing a graph: BFS and DFS
(CLRS 22.2, 22.3)
The most fundamental graph problem is traversing the graph.
There are two standard (and simple) ways of traversing all vertices/edges in a graph in a
systematic way: BFS and DFS.
Most fundamental algorithms on graphs (e.g finding cycles, connected components) are ap-
plications of graph traversal.
Like finding the way out of a maze (maze = graph). Need to be careful to not get stuck in
the graph, so we need to mark vertices that we’ve encountered; and we need to make sure we
don’t skip anything.
Basic idea: over the course of the traversal a vertex progresses from undiscovered, to discov-
ered, to completely-discovered:
undiscovered: initially (WHITE)
discovered: after it’s encountered, but before it’s completely explored (GRAY)
completely explored: the vertex after we visited all its incident edges (BLACK)
We start with a single vertex and evaluate its outgoing edges:
If an edge goes to an undiscoverd vertex, we mark it as discovered and add it to the list
of discovered vertices.
If an edge goes to a completely explored vertex, we ignore it (we’ve already been there)
If an edge goes to an already discovered vertex, we ignore it (it’s on the list).
Analysis: Each edge is visited once (for directed graphs), or twice (undirected graphs once
when exploring each endpoint) O(|V|+|E|)
Depending on how we store the list of discovered vertices we get BFS or DFS:
queue: explore oldest vertex first. The exploration propagates in layers form the starting
vertex.
stack: explore newest vertex first. The exploration goes along a path, and backs up only
when new unexplored vertices are not available.
1
pf3
pf4

Partial preview of the text

Download Traversing a graph: BFS and DFS and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

Traversing a graph: BFS and DFS

(CLRS 22.2, 22.3)

The most fundamental graph problem is traversing the graph.

  • There are two standard (and simple) ways of traversing all vertices/edges in a graph in a systematic way: BFS and DFS.
  • Most fundamental algorithms on graphs (e.g finding cycles, connected components) are ap- plications of graph traversal.
  • Like finding the way out of a maze (maze = graph). Need to be careful to not get stuck in the graph, so we need to mark vertices that we’ve encountered; and we need to make sure we don’t skip anything.
  • Basic idea: over the course of the traversal a vertex progresses from undiscovered, to discov- ered, to completely-discovered: - undiscovered: initially (WHITE) - discovered: after it’s encountered, but before it’s completely explored (GRAY) - completely explored: the vertex after we visited all its incident edges (BLACK)
  • We start with a single vertex and evaluate its outgoing edges:
    • If an edge goes to an undiscoverd vertex, we mark it as discovered and add it to the list of discovered vertices.
    • If an edge goes to a completely explored vertex, we ignore it (we’ve already been there)
    • If an edge goes to an already discovered vertex, we ignore it (it’s on the list).
  • Analysis: Each edge is visited once (for directed graphs), or twice (undirected graphs — once when exploring each endpoint) ⇒ O(|V | + |E|)
  • Depending on how we store the list of discovered vertices we get BFS or DFS:
    • queue: explore oldest vertex first. The exploration propagates in layers form the starting vertex.
    • stack: explore newest vertex first. The exploration goes along a path, and backs up only when new unexplored vertices are not available.

Breadth-first search (BFS)

  • We use a queue Q to hold all gray vertices—vertices we have seen but are still not done with.
  • We remember from which vertex a given vertex v is colored gray – i.e. the node that discovered v first; this is called parent[v].
  • We also maintain d[v], the length of the path from s to v. Initially d[s] = 0.

BFS(s) color[s] = gray d[s] = 0 ENQUEUE(Q, s) WHILE Q not empty DO DEQUEUE(Q, u) FOR each v ∈ adj[u] DO IF color[v] = white THEN color[v] = gray d[v] = d[u] + 1 parent[v] = u //(u,v) is a tree-edge ENQUEUE(Q, v) //ELSE v is not white, (u,v) is non-tree edge color[u] = black

  • Example (for directed graph):
  • If graph is not connected we start the traversal at all nodes until the entire graph is explored.

BFS(G)

FOR each vertex u ∈ V DO IF color[u] = white THEN BFS(u)

Depth-first search (DFS)

  • Use stack instead of queue to hold discovered vertices:
    • We go “as deep as possible”, go back until we find first unexplored adjacent vertex
  • Useful to compute “start time” and “finish time” of vertex u
    • Start time d[u]: time when a vertex is first visited.
    • Finish time f[u]: time when all adjacent vertices of u have been visited.
  • We can write DFS iteratively using the same algorithm as for BFS but with a STACK instead of a QUEUE, or, we can write a recursive DFS procedure DFS(u) color[u] = gray d[u] = time time = time + 1 FOR each v ∈ adj[u] DO IF color[v] = white THEN parent[v] = u DFS(v) color[u] = black f [u] = time time = time + 1
  • Example:

DFS Properties:

  • DFS(u) reaches all vertices reachable from u. On undirected graphs, DFS(u) visits all vertices in CC(u), and the DFS-tree obtained is a spanning tree of G.
  • Analysis: DFS(s) runs in O(|Vc| + |Ec|), where Vc, Ec are the number of vertices and edges in CC(s) (reachable from s, for directed graphs). When run on the entire graph, DFS(G) runs in O(|V | + |E|) time. Put differently, DFS runs in linear time in the size of the graph.
  • As with BFS (v, parent[v]) forms a tree, the DFS-tree
  • Nesting of descendants: If u is descendent of v in DFS-tree then d[v] < d[u] < f [u] < f [v].