Breadth First Search (BFS) : Input: Graph G (V, E), Either Directed or Undirected, Output
Breadth First Search (BFS) : Input: Graph G (V, E), Either Directed or Undirected, Output
Breadth First Search (BFS) : Input: Graph G (V, E), Either Directed or Undirected, Output
BFS: Algorithm
BFS(G,s)
white: undiscovered
1. for each vertex u in V[G] {s}
gray: discovered
2
do color[u] white
black: finished
3
d[u]
4
[u] nil
Q: a queue of discovered
5 color[s] gray
vertices
6 d[s] 0
color[v]: color of v
d[v]: distance from s to v
7 [s] nil
[u]: predecessor of v
8 Q
9 enqueue(Q,s)
10 while Q
11
do u dequeue(Q)
12
for each v in Adj[u]
13
do if color[v] = white
14
then color[v] gray
15
d[v] d[u] + 1
16
[v] u
17
enqueue(Q,v)
18
color[u] black
BFS: Analysis
Initialization takes O(V).
Traversal Loop
After initialization, each vertex is enqueued and dequeued at most
once, and each operation takes O(1). So, total time for queuing is
O(V).
The adjacency list of each vertex is scanned at most once. The
sum of lengths of all adjacency lists is (E).
DFS: Algorithm
DFS(G)
1. for each vertex u V[G]
2.
do color[u] WHITE
3.
[u] NIL
4. time 0
5. for each vertex u V[G]
6.
do if color[u] = WHITE
7.
then DFS-Visit(u)
DFS-Visit(u)
1.
color[u] GRAY // White vertex
u has been discovered
2.
time time + 1
3.
d[u] time
4.
for each v Adj[u]
5.
do if color[v] = WHITE
6.
then [v] u
7.
DFS-Visit(v)
8.
color[u] BLACK // Blacken u;
it is finished.
9.
f[u] time time + 1
Properties of DFS
Property 1
DFS-VISIT(G, u) visits all the vertices and edges in the connected
component of v.
Property 2
The discovery edges labeled by DFS-VISIT(G, v) form a spanning tree of
the connected component of v.
Property 3
The DFS(G) form a forest of spanning trees of the connected components
of G.
A
Analysis of DFS
Loops on lines 1-2 & 5-7 take (V) time, excluding
time to execute DFS-Visit.
DFS-Visit is called once for each white vertex vV
when its painted gray the first time.
Lines 4-7 of DFS-Visit is executed |Adj[v]| times. The
total cost of executing DFS-Visit is vV|Adj[v]| = (E)
Total running time of DFS is (V+E).
DFS :Applications
Path Finding:
We can specialize the DFS algorithm to find a path
between two given vertices u and z using the
template method pattern
We call DFS(G, u) with u as the start vertex
We use a stack S to keep track of the path between
the start vertex and the current vertex
As soon as destination vertex z is encountered, we
return the path as the contents of the stack
Path Finding:
Algorithm pathDFS(G, v, z)
1. for each vertex u V[G]
2. do color[u] WHITE
3. Done=FALSE
4. pathDFS-VISIT(G,v,z)
Algorithm pathDFS-VISIT(G, v, z)
1. Color[v] GRAY
2. S.push(v)
3. If (v = z)
4. THEN Done=TRUE
5.
return S.elements
6. for each u Adj[v]
7. do if (color[u] = WHITE)
8.
THEN pathDFS(G,u,z)
9.
if (Done) THEN return;
10. S.pop()
11. Color[v] BLACK
Socks
Watch
Shoes
Pants
Shirt
Belt
Tie
Jacket
13
a DAG implies an
ordering on events
In a complex DAG, it
can be hard to find a
schedule that obeys
all the constraints.
Topological Sort
For a directed acyclic graph G = (V,E), a topological
sort is a linear ordering of all vertices of G such that
if G contains an edge (u,v), then u appears before v in
the ordering.
A topological sort of a graph can be viewed as an
ordering of its vertices along a horizontal line so that
all directed edges go from left to right.
Topological Sort:Example
Undershorts
Socks
Shoes
Pants
Watch
Shirt
Belt
Tie
Jacket
Socks
Undershorts
Pants
Shoes
Watch
Shirt
Belt
Tie
Jacket
Topological sort
There are often many possible topological sorts of a
given DAG (Directed Acyclic Graph)
Topological orders for this DAG :
1,2,5,4,3,6,7
2,1,5,4,7,3,6
2,5,1,4,7,3,6
Etc.
1
3
2
4
5
7
Impossible!
3
Topological Sort
11/16
17/18
Undershorts
12/15
Socks
Shoes
Pants
Shirt
6/7
Belt
Tie
Jacket
Socks
17/18
1/8
Watch 9/10
13/14
2/5
3/4
Undershorts
Pants
Shoes
Watch
Shirt
Belt
Tie
Jacket
11/16
12/15
13/14
9/10
1/8
6/7
2/5
3/4
13/14
11/16
1/10
8/9
12/15
3/4
2/7
5/6
g
f
DFS on graph G with 4 SCCs, tree edges are in RED
b
g
f
DFS on Graph GT (transpose of G)
SCC tree
abe
cd
fg
(u,v) =
s 0
5
9
2
3
5
x
11
y
[s] = NIL
[u] = s
[v] = u
[x] = s
[y] = x
(s,s) = 0
(s,u) = 3
(s,v) = 9
(s,x) = 5
(s,y) = 11
Relaxation
Algorithms keep track of d[v], [v]. Initialized as
follows:
INITIALIZE-SINGLE-SOURCE(G, s)
1. for each v V[G] do
2.
d[v] ;
//distance from source
3.
[v] NIL
//parent node
4. d[s] 0
// source to source distance =0
Relaxation
u
5
RELAX(u,v)
u
5
RELAX(u,v)
v
6
Dijkstras Algorithm
DIJKSTRA(G, w, s)
1.
INITIALIZE-SINGLE-SOURCE(G,s)
2.
S
3.
QV
4.
while Q
5.
do u EXTRACT-MIN(Q)
6.
SS {u}
7.
for each vertex v Adj[u]
8.
do RELAX(u, v, w)
INITIALIZE-SINGLE-SOURCE(G, s)
1.
for each v V[G] do
2.
d[v] ;
3.
[v] NIL
4. d[s] 0
RELAX(u, v, w)
1.
if d[v] > d[u] + w(u, v) then
2.
d[v] d[u] + w(u, v);
3.
[v] u
Dijkstras Algorithm
DIJKSTRA(G, w, s)
1. for each v V[G] do
2.
d[v] ;
INITIALIZE3.
[v] NIL
SINGLE-SOURCE
4. d[s] 0
5. S
6. QV
7. while Q
// Q is priority queue using minHeap
8. do u EXTRACT-MIN(Q)
9.
SS {u}
10.
for each vertex v Adj[u]
11.
do if d[v] > d[u] + w(u, v)
12.
then d[v] d[u] + w(u, v);
RELAX
13.
[v] u
MST: Problem
Undirected, connected graph G = (V,E)
Weight function W: E R (assigning cost or length
or other values to edges)
Cost/weigth of MST: sum of weights of all edges in
MST.
Problem is to find a Minimum spanning tree: tree that
connects all the vertices and having minimum weight.
w(T ) =
( u ,v )T
w(u, v)
Kruskals Algorithm
Create a forest of trees from the vertices
Repeatedly merge trees by adding safe edges
until only one tree remains
A safe edge is an edge of minimum weight
which does not create a cycle
Kruskal's Algorithm
Edge based algorithm
Add the edges one at a time, in increasing weight
order
The algorithm maintains A a forest of trees. An
edge is accepted it if connects vertices of distinct
trees
We need a data structure that maintains a partition,
i.e.,a collection of disjoint sets
Make-Set(v): S {v}
Union(Si,Sj): S S {Si,Sj} {Si Sj}
FindSet(S, x): returns unique Si S, where x Si
Kruskal's Algorithm
The algorithm adds the cheapest edge that connects
two trees of the forest
MST-Kruskal(G,w)
1 A // set of edges forming MST
2 for each vertex v V[G] do
3
Make-Set(v)
4 sort the edges of E by non-decreasing weight w
5 for each edge (u,v) E, in order by nondecreasing weight do
6
if Find-Set(u) Find-Set(v) then
7
A A {(u,v)}
8
Union(u,v) // Union of sets containing u and v
9 return A
Prims Algorithm
Vertex based algorithm
It is a greedy algorithm.
Start by selecting an arbitrary vertex, include it
into the current MST.
Grow the current MST by inserting into it the
vertex closest to one of the vertices already in
current MST.
Grows one tree T, one vertex at a time
A cloud covering the portion of T already computed
Label the vertices v outside the cloud with key[v]
the minimum weigth of an edge connecting v to a
vertex in the cloud, key[v] = , if no such edge exists
Prims Algorithm
MST-Prim(G,w,r)
01
02
03
04
05
06
07
08
09
10
11