BFS and DFS
BFS and DFS
ALGORITHMS
Graph Algorithms
Review: Graphs
• A graph G = (V, E)
• V = set of vertices, E = set of edges
• Dense graph: |E| ≈ |V|2; Sparse graph: |E| ≈ |V|
• Undirected graph:
• Edge (u,v) = edge (v,u)
• No self-loops
• Directed graph:
• Edge (u,v) goes from vertex u to vertex v, notated u→v
• A weighted graph associates weights with either the edges or the
vertices
Review: Representing Graphs
• Assume V = {1, 2, …, n}
• An adjacency matrix represents the graph as a n x n
matrix A:
• A[i, j] = 1 if edge (i, j) ∈ E (or weight of edge)
= 0 if edge (i, j) ∉ E
• Storage requirements: O(V2)
• A dense representation
• But, can be very efficient for small graphs
• Especially if store just one bit/edge
• Undirected graph: only need one diagonal of
matrix
Review: Graph Searching
• Given: a graph G = (V, E), directed or undirected
• Goal: methodically explore every vertex and every edge
• Ultimately: build a tree on the graph
• Pick a vertex as the root
• Choose certain edges to produce a tree
• Note: might also build a forest if graph is not connected
Review: Breadth-First Search
• “Explore” a graph, turning it into a tree
• One vertex at a time
• Expand frontier of explored vertices across the breadth of the
frontier
• Builds a tree over the graph
• Pick a source vertex to be the root
• Find (“discover”) its children, then their children, etc.
Review: Breadth-First Search
• Again will associate vertex “colors” to guide the
algorithm
• White vertices have not been discovered
• All vertices start out white
• Grey vertices are discovered but not fully explored
• They may be adjacent to white vertices
• Black vertices are discovered and fully explored
• They are adjacent only to black and gray vertices
∞ ∞ ∞ ∞
∞ ∞ ∞ ∞
v w x y
Breadth-First Search: Example
r s t u
∞ 0 ∞ ∞
∞ ∞ ∞ ∞
v w x y
Q: s
Breadth-First Search: Example
r s t u
1 0 ∞ ∞
∞ 1 ∞ ∞
v w x y
Q: w r
Breadth-First Search: Example
r s t u
1 0 2 ∞
∞ 1 2 ∞
v w x y
Q: r t x
Breadth-First Search: Example
r s t u
1 0 2 ∞
2 1 2 ∞
v w x y
Q: t x v
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 ∞
v w x y
Q: x v u
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
BFS: The Code Again
BFS(G, s) {
initialize vertices; Touch every vertex: O(V)
Q = {s};
while (Q not empty) {
u = RemoveTop(Q);
u = every vertex, but only once
for each v ∈ u->adj {
if (v->color == WHITE)
(Why?)
v->color = GREY;
So v = every vertex
that appears in v->d = u->d + 1;
v->p = u;
some other vert’s Enqueue(Q, v);
adjacency l}ist
u->color = BLACK;
} What will be the running time?
} Total running time: O(V+E)
BFS: The Code Again
BFS(G, s) {
initialize vertices;
Q = {s};
while (Q not empty) {
u = RemoveTop(Q);
for each v ∈ u->adj {
if (v->color == WHITE)
v->color = GREY;
v->d = u->d + 1;
v->p = u;
Enqueue(Q, v);
}
What will be the storage cost
u->color = BLACK;
}
in addition to storing the graph?
} Total space used:
O(max(degree(v))) = O(E)
Breadth-First Search: Properties
• BFS calculates the shortest-path distance to the source
node
• Shortest-path distance δ(s,v) = minimum number of edges from s
to v, or ∞ if v not reachable from s
• Proof given in the book (p. 472-5)
| |
| | |
DFS Example
source
vertex
d f
1 | | |
2 | |
| | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | |
DFS Example
source
vertex
d f
1 | | |
2 | |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 |
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 | |
2 | 7 9 |
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 | 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 |
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 |
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|
DFS Example
source
vertex
d f
1 |12 8 |11 13|
2 | 7 9 |10
3 | 4 5 | 6 14|15
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• The tree edges form a spanning forest
• Can tree edges form cycles?Why or why not?
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
Tree edges
DFS: Kinds of edges
• DFS introduces an important distinction among edges in
the original graph:
• Tree edge: encounter new (white) vertex
• Back edge: from descendent to ancestor
• Encounter a grey vertex (grey to grey)
DFS Example
source
vertex
d f
1 |12 8 |11 13|16
2 | 7 9 |10
3 | 4 5 | 6 14|15
2 | 7 9 |10
3 | 4 5 | 6 14|15
2 | 7 9 |10
3 | 4 5 | 6 14|15
C?
DFS And Graph
Cycles
• Thm: An undirected graph is acyclic iff a DFS yields no
back edges
• If acyclic, no back edges (because a back edge implies a cycle
• If no back edges, acyclic
• No back edges implies only tree edges (Why?)
• Only tree edges implies we have a tree or a forest
• Which by definition is acyclic