Graphs
Graphs
r s t u r s t u
Q r t x Q t x v
2 2 2
1 2 2
v w x y v w x y
BFS Example
r s t u r s t u
Q x v u Q v u y
2 2 3 2 3 3
v w x y v w x y
r s t u r s t u
Q u y Q y
3 3 3
v w x y v w x y
BFS Example: Result
r s t u
Q -
v w x y
BFS Algorithm
BFS(G,s)
01 for each vertex u V[G]-{s}
Init all
02 color[u] white
03 d[u] vertices
04 [u] NIL
05 color[s] gray Init BFS with s
06 d[s] 0
07 [u] NIL
08 Q {s}
09 while Q do Handle all u’s
10 u head[Q]
children before
11 for each v Adj[u] do
12 if color[v] = white then handling any
13 color[v] gray children of
14 d[v] d[u] + 1 children
15 [v] u
16 Enqueue(Q,v)
17 Dequeue(Q)
18 color[u] black
BFS Running Time
• 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 only then…)
– The sum of the lengths of all lists is (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)
BFS Properties
• Given a graph G = (V,E), BFS discovers all vertices
reachable from a source vertex s
• It computes the shortest 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
Breadth First Tree
• Predecessor subgraph of G
G = (V , E )
V = v V : [v] NIL s
E = ([v], v) E : v V − {s}
• G is a breadth-first tree
– V consists of the vertices reachable from s, and
– for all v V, there is a unique simple path from s to v in
G that is also a shortest path from s to v in G
• The edges in G are called tree edges
Depth-First Search
• A depth-first search (DFS) in an undirected graph G
is like wandering in a labyrinth with a string and a
can of paint
– We start at vertex s, tying the end of our string to the point
and painting s “visited (discovered)”. Next we label s as our
current vertex called u
– Now, we travel along an arbitrary edge (u,v).
– If edge (u,v) leads us to an already visited vertex v we
return to u
– If vertex v is unvisited, we unroll our string, move to v,
paint v “visited”, set v as our current vertex, and repeat the
previous steps
Depth-First Search (2)
• Eventually, we will get to a point where all incident
edges on u lead to visited vertices
• We then backtrack by unrolling our string to a
previously visited vertex v. Then v becomes our
current vertex and we repeat the previous steps
• Then, if all incident edges on v lead to visited
vertices, we backtrack as we did before. We continue
to backtrack along the path we have traveled,
finding and exploring unexplored edges, and
repeating the procedure
DFS Algorithm
• Initialize – color all vertices white
• Visit each and every white vertex using DFS-Visit
• Each call to DFS-Visit(u) roots a new tree of the
depth-first forest at vertex u
• A vertex is white if it is undiscovered
• A vertex is gray if it has been discovered but not all
of its edges have been discovered
• A vertex is black after all of its adjacent vertices have
been discovered (the adj. list was examined
completely)
DFS Algorithm (2)
Init all
vertices
3/
x y z x y z x y z
u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/
B B
4/ 3/ 4/ 3/ 4/5 3/
x y z x y z x y z
DFS Example (2)
u v w u v w u v w
1/ 2/ 1/ 2/7 1/ 2/7
B B F B
4/5 3/6 4/5 3/6 4/5 3/6
x y z x y z x y z
u v w u v w u v w
1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/
B B B C
F F F
4/5 3/6 4/5 3/6 4/5 3/6
x y z x y z x y z
DFS Example (3)
u v w u v w u v w
1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/
B C B C B C
F F F
4/5 3/6 10/ 4/5 3/6 10/ B 4/5 3/6 10/11 B
x y z x y z x y z
u v w
1/8 2/7 9/12
B C
F
4/5 3/6 10/11 B
x y z
DFS Algorithm (3)
• When DFS returns, every vertex u is assigned
– a discovery time d[u], and a finishing time f[u]
• Running time
– the loops in DFS take time (V) each, excluding the
time to execute DFS-Visit
– DFS-Visit is called once for every vertex
• its only invoked on white vertices, and
• paints the vertex gray immediately
– for each DFS-visit a loop interates over all Adj[v]
– the total cost for DFS-Visit is (E)
Adj[v] = ( E )
vV
G = (V , E )
E = ([v], v) E : v V and [v] NIL