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

Graph Search:: Breadth-First Search (BFS) Depth-First Search (DFS)

The document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It provides pseudocode for BFS and explains that it uses a queue data structure. It also provides an example of running BFS on a sample graph. For DFS, it explains that it explores deeper paths first and uses a stack. Pseudocode and an example of running DFS on a graph are also provided. Applications of the algorithms include finding connectivity in graphs and tree traversals.

Uploaded by

Han
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Graph Search:: Breadth-First Search (BFS) Depth-First Search (DFS)

The document discusses graph search algorithms breadth-first search (BFS) and depth-first search (DFS). It provides pseudocode for BFS and explains that it uses a queue data structure. It also provides an example of running BFS on a sample graph. For DFS, it explains that it explores deeper paths first and uses a stack. Pseudocode and an example of running DFS on a graph are also provided. Applications of the algorithms include finding connectivity in graphs and tree traversals.

Uploaded by

Han
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

GRAPH SEARCH:

BREADTH-FIRST SEARCH (BFS)


DEPTH-FIRST SEARCH (DFS)
BREADTH-FIRST AND DEPTH-FIRST
SEARCH
 BFS Basic Algorithm
 BFS Complexity
 DFS Algorithm
 DFS Implementation
 Relation between BFS and DFS
GRAPH REPRESENTATION

graph Adjacency list Adjacency matrix


GRAPH TRAVERSAL
 Problem: Search for a certain node or traverse all nodes
in the graph
 Depth First Search
 Once a possible path is found, continue the search until the
end of the path
 Breadth First Search
 Startseveral paths at a time, and advance in each one step at
a time
GRAPH TRAVERSAL (CONTD.)

 In both DFS and BFS, the nodes of the undirected graph are
visited in a systematic manner so that every node is visited
exactly one.

 Both BFS and DFS give rise to a tree:


 When a node x is visited, it is labeled as visited, and it is
added to the tree.
 If the traversal got to node x from node y, y is viewed as
the parent of x, and x a child of y.
GRAPH TRAVERSALS
BREADTH-FIRST SEARCH
 BFS follows the following rules:
 Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
 From each node z in the current level, in the order in
which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes
from this level form a new level that becomes the next
current level.
 Repeat step 2 until no more nodes can be visited.
 If there are still unvisited nodes, repeat from Step 1.
IMPLEMENTATION OF BFS
 Observations:
 the first node visited in each level is the first node from
which to proceed to visit new nodes.

 This suggests that a queue is the proper data structure to


remember the order of the steps.
BREADTH-FIRST SEARCH: EXAMPLE

r s t u

   

   
v w x y
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

 0  

   
v w x y

Q: s
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0  

 1  
v w x y

Q: w r
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 

 1 2 
v w x y

Q: r t x
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 

2 1 2 
v w x y

Q: t x v
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 3

2 1 2 
v w x y

Q: x v u
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: v u y
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: u y
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: y
BREADTH-FIRST SEARCH: EXAMPLE
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: Ø
BFS (ALGORITHM)

BFS(V,E,s)
For all nodes except s
d[v]=∞
d[s]=0
Q= s
While Q ≠ Ø
u= Dequeue(Q)
for all adjacent nodes v of u
if d[v]= ∞
then d[v]=d[u]+1
Enqueue(Q,v)
BFS (TIME COMPLEXITY)

BFS(V,E,s)
For all nodes except s O(|V|)
d[v]=∞
d[s]=0
Q= Ø
While Q ≠ Ø
u= Dequeue(Q)
O(V2) = |E|).
for all adjacent nodes v of u
if d[v]= ∞
then d[v]=d[u]+1
Enqueue(Q,v)

Overall complexity = O(|V|)+ |E|)


DEPTH-FIRST SEARCH

 Depth-first search is another strategy for exploring a


graph
Explore “deeper” in the graph whenever possible.
Edges are explored out of the most recently
discovered vertex v that still has unexplored edges.
When all of v’s edges have been explored,
backtrack to the vertex from which v was
discovered.
DEPTH-FIRST SEARCH
 Vertices initially colored white
 Then colored gray when discovered

 Then black when finished


DFS EXAMPLE
source
vertex
DFS EXAMPLE
source
vertex
d f
1 | | |

| |

| | |
DFS EXAMPLE
source
vertex
d f
1 | | |

2 | |

| | |
DFS EXAMPLE
source
vertex
d f
1 | | |

2 | |

3 | | |
DFS EXAMPLE
source
vertex
d f
1 | | |

2 | |

3 | 4 | |
DFS EXAMPLE
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | |
DFS EXAMPLE
source
vertex
d f
1 | | |

2 | |

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 | 8 | |

2 | 7 |

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 | 8 | |

2 | 7 9 |

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 | 8 | |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 | 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 |12 8 |11 |

2 | 7 9 |10

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 |
DFS EXAMPLE
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|
DFS EXAMPLE
source
vertex
d f
1 |12 8 |11 13|

2 | 7 9 |10

3 | 4 5 | 6 14|15
DFS EXAMPLE
source
vertex
d f
1 |12 8 |11 13|16

2 | 7 9 |10

3 | 4 5 | 6 14|15
DEPTH-FIRST SEARCH WITH
STACK
Initialization:
mark all vertices as unvisited,
visit(s)

while the stack is not empty:


pop (v,w)
if w is not visited
add (v,w) to tree T
visit(w)

Procedure visit(v)
mark v as visited
for each edge (v,w)
push (v,w) in the stack
RECURSIVE DFS
DepthFirst(Vertex v)
visit(v);
for each neighbor w of v
if (w is not visited)
add edge (v,w) to tree T
DepthFirst(w)

Visit(v)
mark v as visited
APPLICATIONS OF DFS
 Trees: preorder traversal
 Graphs:
 Connectivity
 Biconnectivity – articulation points
 Euler graphs

You might also like