Breadth-First Search (BFS) : Unmarked Start V Visit Visited Visit Unmarked U V U Mark Recurse Visited
Breadth-First Search (BFS) : Unmarked Start V Visit Visited Visit Unmarked U V U Mark Recurse Visited
Intuition: BFS(vertex v)
To start, all vertices are unmarked.
• Mark v finished.
u1 v v
2 1
1
u2
4
3
u3
v v
2 1 2
7 1
5 3 4
8 5 3 4
6
6
49
Q: What information about the graph can a BFS be used to find?
• the shortest path from v to any other vertex u and this distance d(v)
• ENQUEUE(Q,v)
• DEQUEUE(Q)
• ISEMPTY(Q)
• the predecessor
50
The BFS Algorithm
We will use p[v] to represent the predecessor of v and d[v] to
represent the number of edges from v (i.e., the distance from v).
BFS(G=(V,E),v)
for all vertices u in V
color[u] := black
d[u] := infinity; \\ we use infinity to denote
p[u] := NIL; \\ "not connected"
end for
initialize an empty queue Q;
color[v] := green;
d[v] := 0;
p[v] := NIL;
ENQUEUE(Q,v);
while not ISEMPTY(Q) do
u := DEQUEUE(Q);
for each edge (u,w) in E do
if (color[w] == black) then
color[w] := green;
d[w] := d[u] + 1;
p[w] := u;
ENQUEUE(Q,w);
end if
end for
color[u] := red;
end while
END BFS
51
Complexity of BFS(G,v)
Q: How many times is each node ENQUEUEed?
NOTES:
• BFS will visit only those vertices that are reachable from v.
• If not, then we may have to call BFS on more than one start
vertex in order to see the whole graph.
52
Depth-First Search
Intuition: DFS(G,v)
• Continue.
53
Example.
v v
Back−edge
DFS−edge
v v
v v
Q: Does it find the shortest path between v and all other ver-
tices?
no
54
Implementing a DFS
Q: Which ADT would be the most helpful for implementing DFS
given an adjacency list representation of G?
• PUSH(S, (u,v))
• POP(S)
• ISEMPTY(S)
55
Algorithm DFS(G,s)
DFS(G=(V,E),s)
for all vertices v in V
color[v] := black;
d[v] := infinity;
f[v] := infinity;
p[v] := NIL;
end for
initialize an empty stack S;
color[s] := green; d[s] := 0; p[s] := NIL;
time := 0;
PUSH(S,(s,NIL));
for each edge (s,v) in E do
PUSH(S,(s,v));
end for
while not ISEMPTY(S) do
(u,v) := POP(S);
if (v == NIL) then // Done with u
time := time + 1;
f[u] := time;
color[u] := white;
end if
else if (color[v] == black) then
color[v] := green;
time := time + 1;
d[v] := time;
p[v] := u;
PUSH(S,(v,NIL)); // Marks the end of v’s neighbors
for each edge (v,w) in E do
PUSH(S,(v,w));
end for
(*) end if
end while
END DFS
56
Complexity of DFS(G,s)
Q: How many times does DFS visit the neighbours of a node?
• So the total running time is just like for BFS, Θ(n + m) i.e.,
linear in the size of the adjacency list.
Note that the gold edges, or the DFS edges form a tree called
the DFS-tree.
57
We can specify edges on DFS-tree according to how they are
traversed during the search.
• Cross-Edges∗ are all the other edges that are not part of
the DFS tree (from a vertex u to another vertex v that is
neither an ancestor nor a descendent of u in the DFS tree).
p[v]
It is not hard to see that there is a cycle in G if and only if there are any c
Add a test after the line marked by (*) in DFS. If the color of v is green ins
58
Minimum Cost Spanning Trees (MCSTs)
Thick edges are in A, the thin ones are not. The first one is a
tree and the other two are not.
59
Q: How many edges must any spanning tree contain?
n − 1 edges where |V | = n
!
w(A) = w(e) ≤ w(B)
e∈A
5 5
8 8
2 4 6 2 4 6
Minimum Cost Spanning Tree Spanning tree, but not minimum cost
3 3
3 3 3 3
60
Q: What is an example of an application in which you would we
want to be able to find a MCST?
A: Imagine you have a network of computers that are connected by various links. Some
Prim’s Algorithm
Prim’s algorithm uses a Priority Queue ADT.
A priority queue is just like a queue except that every item in the
queue has a priority (usually just a number).
• a set of elements
• The operations:
61