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

BFS DFS

Depth-first search (DFS) and breadth-first search (BFS) are two algorithms for searching graphs. DFS explores each path as far as possible before backtracking, while BFS explores all neighbors of a node before moving to the next level. Both algorithms run in O(V+E) time and O(V+E) space, where V is the number of vertices and E is the number of edges. DFS may not find the shortest path but is easier to retrieve the path. BFS always finds the shortest path but is harder to retrieve the path.

Uploaded by

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

BFS DFS

Depth-first search (DFS) and breadth-first search (BFS) are two algorithms for searching graphs. DFS explores each path as far as possible before backtracking, while BFS explores all neighbors of a node before moving to the next level. Both algorithms run in O(V+E) time and O(V+E) space, where V is the number of vertices and E is the number of edges. DFS may not find the shortest path but is easier to retrieve the path. BFS always finds the shortest path but is harder to retrieve the path.

Uploaded by

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

Depth-first search

• depth-first search (DFS): Finds a path between two vertices by


exploring each possible path as far as possible before backtracking.
 Often implemented recursively.
 Many graph algorithms involve visiting or marking vertices.

• Depth-first paths from a to all vertices (assuming ABC edge order):


 to b: {a, b}
 to c: {a, b, e, f, c} a b c
 to d: {a, d}
 to e: {a, b, e} d e f
 to f: {a, b, e, f}
 to g: {a, d, g} g h
 to h: {a, d, g, h}
1
DFS pseudocode
function dfs(v1, v2): a b c
dfs(v1, v2, { }).
d e f
function dfs(v1, v2, path):
path += v1.
g h
mark v1 as visited.
if v1 is v2:
a path is found!
for each unvisited neighbor n of v1:
if dfs(n, v2, path) finds a path: a path is found!
path -= v1. // path is not found.

• The path param above is used if you want to have the


path available as a list once you are done.
 Trace dfs(a, f) in the above graph. 2
DFS observations
• discovery: DFS is guaranteed to a b c
find a path if one exists.
d e f
• retrieval: It is easy to retrieve exactly
g h
what the path is (the sequence of
edges taken) if we find it

• optimality: not optimal. DFS is guaranteed to find a path, not


necessarily the best/shortest path
 Example: dfs(a, f) returns {a, d, c, f} rather than {a, d, f}.

3
Breadth-first search
• breadth-first search (BFS): Finds a path between two nodes by
taking one step down all paths and then immediately backtracking.
 Often implemented by maintaining a queue of vertices to visit.

• BFS always returns the shortest path (the one with the fewest
edges) between the start and the end vertices.
 to b: {a, b}
 to c: {a, e, f, c} a b c
 to d: {a, d}
 to e: {a, e} d e f
 to f: {a, e, f}
g h
 to g: {a, d, g}
 to h: {a, d, h}

4
BFS pseudocode
function bfs(v1, v2): a b c
queue := {v1}.
mark v1 as visited. d e f

while queue is not empty: g h


v := queue.removeFirst().
if v is v2:
a path is found!
for each unvisited neighbor n of v:
mark n as visited.
queue.addLast(n).
// path is not found.

• Trace bfs(a, f) in the above graph.


5
BFS observations
• optimality: a b c
 always finds the shortest path (fewest edges).
d e f
 in unweighted graphs, finds optimal cost path.
 In weighted graphs, not always optimal cost. g h

• retrieval: harder to reconstruct the actual sequence of vertices or


edges in the path once you find it
 conceptually, BFS is exploring many possible paths in parallel, so it's
not easy to store a path array/list in progress
 solution: We can keep track of the path by storing predecessors for
each vertex (each vertex can store a reference to a previous vertex).

• DFS uses less memory than BFS, easier to reconstruct the path once
found; but DFS does not always find shortest path. BFS does.
6
DFS, BFS runtime
• What is the expected runtime of DFS and BFS, in terms of the
number of vertices V and the number of edges E ?

• Answer: O(|V| + |E|)


 where |V| = number of vertices, |E| = number of edges
 Must potentially visit every node and/or examine every edge once.

 why not O(|V| * |E|) ?

• What is the space complexity of each algorithm?


 (How much memory does each algorithm require?)

7
BFS that finds path
function bfs(v1, v2): a b c
queue := {v1}. prev
mark v1 as visited. d e f

while queue is not empty: g h


v := queue.removeFirst().
if v is v2:
a path is found! (reconstruct it by following .prev back to v1.)
for each unvisited neighbor n of v:
mark n as visited. (set n.prev = v.)
queue.addLast(n).
// path is not found.

 By storing some kind of "previous" reference associated with each


vertex, you can reconstruct your path back once you find v2.
8

You might also like