0% found this document useful (0 votes)
39 views

Graph Algorithms - BFS: Outlines: Graphs Part-2

Breadth-first search (BFS) is a graph search algorithm that begins at a starting node and explores all neighboring nodes. It explores neighboring nodes level-by-level, exploring all nodes at the present depth prior to moving to the next depth level. The key steps of BFS are to: (1) enqueue the starting node, (2) dequeue nodes and enqueue their neighbors, (3) repeat step 2 until the queue is empty. BFS runs in O(V+E) time where V is the number of vertices and E is the number of edges.

Uploaded by

Mohammed Hajjaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Graph Algorithms - BFS: Outlines: Graphs Part-2

Breadth-first search (BFS) is a graph search algorithm that begins at a starting node and explores all neighboring nodes. It explores neighboring nodes level-by-level, exploring all nodes at the present depth prior to moving to the next depth level. The key steps of BFS are to: (1) enqueue the starting node, (2) dequeue nodes and enqueue their neighbors, (3) repeat step 2 until the queue is empty. BFS runs in O(V+E) time where V is the number of vertices and E is the number of edges.

Uploaded by

Mohammed Hajjaj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 22

Graph Algorithms - BFS

PART-2

Outlines: Graphs Part-2

 Graph Search Methods


 Breadth-First Search (BFS):
 BFS Algorithm
 BFS Example
 BFS Time Complexity
 Output of BFS:
 Shortest Path
 Breath-First Tree
 Is the Graph Connected?

1
Graph Search Methods

 Many graph problems solved using a search method


 Path from one vertex to another
 Is the graph connected?
 etc.

 Commonly used search methods:


 Breadth-First Search (BFS)
 Depth-First Search (DFS)

Graph Search Methods

 A vertex u is reachable from vertex v iff there is a


path from v to u.
 A search method starts at a given vertex v and
visits every vertex that is reachable from v.
2
3 8

1
10
4 9
5

11
6 7

2
Graph Search Methods

 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

Breadth First Search

 Example: Binary Tree (This is a special case of a graph).


 The order of search is across levels.
 The root is examined first; then children of the root;
then the children of those nodes, and so on.

2 3 4

5 6 7 8

3
Breadth First Search

 Example: Directed Graph


 Pick a source vertex S to start.
 Find (or discover) the vertices that are adjacent to S.
 Pick each child of S in turn and discover their vertices
adjacent to that child.
 Done when all children have been discovered and examined.
 This results in a tree that is rooted at the source vertex S.
 The idea is to find the distance from the selected Source
vertex to all reachable vertices (Destinations).

Breadth-First Search

 Visit start vertex (s) and put into a FIFO queue.

 Repeatedly remove a vertex from the queue, visit


its unvisited adjacent vertices, put newly visited
vertices into the queue.

 All vertices reachable from the start vertex (s)


(including the start vertex) are visited.

4
Breadth-First Search

 Again will associate vertex “colors” to guide the


algorithm
 White vertices have not been discovered
All vertices start out white
 Gray 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
 Explore vertices by scanning adjacency list of gray
vertices

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

Breadth-First Search: Example

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

Breadth-First Search: Example

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

Breadth-First Search: Example

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

Breadth-First Search: Example

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

Breadth-First Search: Example

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

BFS: Time Complexity

 Given a graph G = (V, E)


 Vertices are enqueued if there color is white
 Assuming that en- and dequeuing takes O(1) time the
total cost of this operation is O(V)
 Adjacency list of a vertex is scanned when the vertex is
dequeued (and at most once)
 The sum of the lengths of all lists is O(E). Consequently,
O(E) time is spent on scanning them
 Initializing the algorithm takes O(V)
 Total running time O(V+E)
 linear in the size of the adjacency list representation of G

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.

 BFS calculates the shortest-path distance to the source node


 shortest-path distance = minimum number of edges from s
to v = (s,v) = d[v]
 or (s,v) =  if v not reachable from s

26

13
Breadth-First Tree
 BFS builds breadth-first tree, in which paths to root
represent shortest paths in G

 Given a graph G = (V, E) with source s  V, we define the


predecessor subgraph of G as Gp = (Vp, Ep), where
 Vp = {v  V : p[v]  NIL}  {s}
 Ep = {(p[v], v) : v  Vp - {s}}

 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.

 The edges in Gp are called tree edges

27

Path From Vertex v To Vertex u

 Start a breadth-first search at vertex v.

 Terminate when vertex u is visited or when Q


becomes empty (whichever occurs first).

 Time O(V+E)

28

14
Is The Graph Connected?

 Start a breadth-first search at any vertex of the graph.

 Graph is connected iff all n vertices get visited.

 Time O(V+E)

29

15

You might also like