Graph Algorithms - BFS: Outlines: Graphs Part-2
Graph Algorithms - BFS: Outlines: Graphs Part-2
PART-2
1
Graph Search Methods
1
10
4 9
5
11
6 7
2
Graph Search Methods
2 3 4
5 6 7 8
3
Breadth First Search
Breadth-First Search
4
Breadth-First Search
Breadth-First Search
BFS(G, s) {
// initialize vertices;
1 for each u V(G) – {s}{
2 do color[u] = WHITE
3 d[u] = // distance from s to u
4 p[u] = NIL // predecessor or parent of u
}
5 color[s] = GRAY
6 d[s] = 0
7 p[s] = NIL
8 Q = Empty;
9 Enqueue (Q,s); // Q is a queue; initialize to s
10 while (Q not empty) {
11 u = Dequeue(Q);
12 for each v adj[u] {
13 if (color[v] == WHITE)
14 color[v] = GRAY;
15 d[v] = d[u] + 1;
16 p[v] = u; What does d[v] represent?
17 Enqueue(Q, v);
} What does p[v] represent?
18 color[u] = BLACK;
}
}
10
5
Breadth-First Search
Lines 1-4 paint every vertex white, set d[u] to be
infinity for each vertex (u), and set p[u] the parent
of every vertex to be NIL.
Line 5 paints the source vertex (s) gray.
Line 6 initializes d[s] to 0.
Line 7 sets the parent of the source to be NIL.
Lines 8-9 initialize Q to the queue containing just
the vertex (s).
The while loop of lines 10-18 iterates as long as
there remain gray vertices, which are discovered
vertices that have not yet had their adjacency lists
fully examined.
This while loop maintains the test in line 10, the
queue Q consists of the set of the gray vertices.
11
Breadth-First Search
Prior to the first iteration in line 10, the only gray vertex, and
the only vertex in Q, is the source vertex (s).
Line 11 determines the gray vertex (u) at the head of the queue
Q and removes it from Q.
The for loop of lines 12-17 considers each vertex (v) in the
adjacency list of (u).
If (v) is white, then it has not yet been discovered, and the
algorithm discovers it by executing lines 14-17.
It is first grayed, and its distance d[v] is set to d[u]+1.
Then, u is recorded as its parent.
Finally, it is placed at the tail of the queue Q.
When all the vertices on (u’s) adjacency list have been
examined, u is blackened in line 18.
12
6
Breadth-First Search: Example
r s t u
v w x y
13
r s t u
0
v w x y
Q: s
14
7
Breadth-First Search: Example
r s t u
1 0
1
v w x y
Q: w r
15
r s t u
1 0 2
1 2
v w x y
Q: r t x
16
8
Breadth-First Search: Example
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
17
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
18
9
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
19
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
20
10
Breadth-First Search: Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
21
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
22
11
BFS: The Code Again
BFS(G, s) {
// initialize vertices
for each u V(G) – {s}{ Touch every vertex: O(V)
do color[u] = WHITE
d[u] =
p[u] = NIL
}
color[s] = GRAY;
d[s] = 0;
p[s] = NIL;
Q = Empty;
Enqueue (Q,s);
while (Q not empty) {
u = Dequeue(Q);
for each v adj[u] {
u = every vertex, but only once
if (color[v] == WHITE)
color[v] = GRAY;
So v = every d[v] = d[u] + 1;
p[v] = u;
vertex that Enqueue(Q, v);
appears in some
} What will be the running time?
color[u] = BLACK;
other vert’s
} Total running time: O(V+E)
}
adjacency list
23
24
12
BFS: The Code Again
BFS(G, s) {
// initialize vertices
for each u V(G) – {s}{ Each vertex will be enqueue once.
do color[u] = WHITE
d[u] =
p[u] = NIL
}
color[s] = GRAY
d[s] = 0
p[s] = NIL
Q = Empty;
Enqueue (Q,s);
while (Q not empty) {
u = Dequeue(Q);
for each v adj[u] {
if (color[v] == WHITE) What will be the storage cost
color[v] = GREY; in addition to storing the tree?
d[v] = d[u] + 1;
p[v] = u; Queue Size
Enqueue(Q, v); Total space used:
} at least = O(max(degree(v)))
color[u] = BLACK;
} at most = O(V)
}
25
Shortest Paths
Given a graph G = (V, E), BFS discovers all vertices
reachable from a source vertex s
It computes the shortest-path distance to all reachable
vertices
It computes a breadth-first tree that contains all such
reachable vertices
For any vertex v reachable from s, the path in the breadth first
tree from s to v, corresponds to a shortest path in G.
26
13
Breadth-First Tree
BFS builds breadth-first tree, in which paths to root
represent shortest paths in G
Gp is a breadth-first tree
Vp consists of the vertices reachable from s, and
for all v Vp, there is a unique simple path from s to v
in Gp that is also a shortest path from s to v in G.
27
Time O(V+E)
28
14
Is The Graph Connected?
Time O(V+E)
29
15