Breadth-First Search and Depth-First
Search
Md. Muktar Hossain
Lecturer(Provisional), CSE, VU
Breadth First Search(BFS)
The breadth-first search (BFS) algorithm is used to search a tree or graph data
structure for a node that meets a set of criteria. It starts at the tree’s root or
graph and searches/visits all nodes at the current depth level before moving on
to the nodes at the next depth level.
Applications of BFS algorithm
I. BFS can be used to find the neighboring locations from a given source location.
II. In a peer-to-peer network, BFS algorithm can be used as a traversal method to find
all the neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc.
employ this process to find "seeds" and "peers" in the network.
III. BFS is one of the main algorithms that can be used to index web pages. It starts
traversing from the source page and follows the links associated with the page.
Here, every web page is considered as a node in the graph.
IV. BFS is used to determine the shortest path and minimum spanning tree.
V. It can be used in ford-Fulkerson method to compute the maximum flow in a flow
network.
Breadth First Search(BFS) Cont’d
Algorithm:
BFS(start):
create a queue Q
mark start as visited and enqueue it onto Q
while Q is not empty:
dequeue a vertex v from Q
process v (e.g., print its value)
for each neighbor u of v:
if u has not been visited:
mark u as visited and enqueue it onto Q
Complexity: O(V+E)
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={}
V={}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={A,B,C}
V={S}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={B,C,D,E}
V={S,A}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={C,D,E}
V={S,A,B}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={D,E,F,G}
V={S,A,B,C}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={E,F,G}
V={S,A,B,C,D}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={F,G}
V={S,A,B,C,D,E}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={G}
V={S,A,B,C,D,E,F}
Breadth First Search(BFS) Cont’d
S
A B C
D F
E G
Q={}
V={S,A,B,C,D,E,F,G}
Depth First Search(DFS)
Depth-first search is an algorithm for searching tree or graph data structures.
The algorithm starts at the root node (selecting some arbitrary node as the root
node in the case of a graph) and explores as far as possible along each branch
before backtracking.
Applications of DFS algorithm
I. DFS algorithm can be used to implement the topological sorting.
II. It can be used to find the paths between two vertices.
III. It can also be used to detect cycles in the graph.
IV. DFS is used to determine if a graph is bipartite or not.
Depth First Search(DFS)
Algorithm:
DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false
DFS(G,v)
init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)
}
Complexity: O(V+E)
Depth First Search(DFS)
S
A B C
D F
E G
S={}
V={}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={A,B,C}
V={S}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={D,E,B,C}
V={S,A}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={E,B,C}
V={S,A,D}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={B,C}
V={S,A,D,E}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={C}
V={S,A,D,E,B}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={F,G}
V={S,A,D,E,B,C}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={G}
V={S,A,D,E,B,C,F}
Depth First Search(DFS) Cont’d
S
A B C
D F
E G
S={}
V={S,A,D,E,B,C,F,G}
Home Task
● BFS vs DFS
For help:
● https://www.tutorialspoint.com/difference-between-bfs-and-dfs
● https://www.geeksforgeeks.org/difference-between-bfs-and-dfs/
References
● https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/
● https://www.programiz.com/dsa/graph-dfs
● https://www.javatpoint.com/depth-first-search-algorithm
● https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/
● https://www.javatpoint.com/breadth-first-search-algorithm
● https://www.programiz.com/dsa/graph-bfs
● https://www.tutorialspoint.com/difference-between-bfs-and-dfs
● https://www.tutorialspoint.com/data_structures_algorithms/breadth_first_trave
rsal.htm
Thank You

Breadth-First Search and Depth-First Search.pptx

  • 1.
    Breadth-First Search andDepth-First Search Md. Muktar Hossain Lecturer(Provisional), CSE, VU
  • 2.
    Breadth First Search(BFS) Thebreadth-first search (BFS) algorithm is used to search a tree or graph data structure for a node that meets a set of criteria. It starts at the tree’s root or graph and searches/visits all nodes at the current depth level before moving on to the nodes at the next depth level. Applications of BFS algorithm I. BFS can be used to find the neighboring locations from a given source location. II. In a peer-to-peer network, BFS algorithm can be used as a traversal method to find all the neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc. employ this process to find "seeds" and "peers" in the network. III. BFS is one of the main algorithms that can be used to index web pages. It starts traversing from the source page and follows the links associated with the page. Here, every web page is considered as a node in the graph. IV. BFS is used to determine the shortest path and minimum spanning tree. V. It can be used in ford-Fulkerson method to compute the maximum flow in a flow network.
  • 3.
    Breadth First Search(BFS)Cont’d Algorithm: BFS(start): create a queue Q mark start as visited and enqueue it onto Q while Q is not empty: dequeue a vertex v from Q process v (e.g., print its value) for each neighbor u of v: if u has not been visited: mark u as visited and enqueue it onto Q Complexity: O(V+E)
  • 4.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={} V={}
  • 5.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={A,B,C} V={S}
  • 6.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={B,C,D,E} V={S,A}
  • 7.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={C,D,E} V={S,A,B}
  • 8.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={D,E,F,G} V={S,A,B,C}
  • 9.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={E,F,G} V={S,A,B,C,D}
  • 10.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={F,G} V={S,A,B,C,D,E}
  • 11.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={G} V={S,A,B,C,D,E,F}
  • 12.
    Breadth First Search(BFS)Cont’d S A B C D F E G Q={} V={S,A,B,C,D,E,F,G}
  • 13.
    Depth First Search(DFS) Depth-firstsearch is an algorithm for searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. Applications of DFS algorithm I. DFS algorithm can be used to implement the topological sorting. II. It can be used to find the paths between two vertices. III. It can also be used to detect cycles in the graph. IV. DFS is used to determine if a graph is bipartite or not.
  • 14.
    Depth First Search(DFS) Algorithm: DFS(G,u) u.visited = true for each v ∈ G.Adj[u] if v.visited == false DFS(G,v) init() { For each u ∈ G u.visited = false For each u ∈ G DFS(G, u) } Complexity: O(V+E)
  • 15.
    Depth First Search(DFS) S AB C D F E G S={} V={}
  • 16.
    Depth First Search(DFS)Cont’d S A B C D F E G S={A,B,C} V={S}
  • 17.
    Depth First Search(DFS)Cont’d S A B C D F E G S={D,E,B,C} V={S,A}
  • 18.
    Depth First Search(DFS)Cont’d S A B C D F E G S={E,B,C} V={S,A,D}
  • 19.
    Depth First Search(DFS)Cont’d S A B C D F E G S={B,C} V={S,A,D,E}
  • 20.
    Depth First Search(DFS)Cont’d S A B C D F E G S={C} V={S,A,D,E,B}
  • 21.
    Depth First Search(DFS)Cont’d S A B C D F E G S={F,G} V={S,A,D,E,B,C}
  • 22.
    Depth First Search(DFS)Cont’d S A B C D F E G S={G} V={S,A,D,E,B,C,F}
  • 23.
    Depth First Search(DFS)Cont’d S A B C D F E G S={} V={S,A,D,E,B,C,F,G}
  • 24.
    Home Task ● BFSvs DFS For help: ● https://www.tutorialspoint.com/difference-between-bfs-and-dfs ● https://www.geeksforgeeks.org/difference-between-bfs-and-dfs/
  • 25.
    References ● https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/ ● https://www.programiz.com/dsa/graph-dfs ●https://www.javatpoint.com/depth-first-search-algorithm ● https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/ ● https://www.javatpoint.com/breadth-first-search-algorithm ● https://www.programiz.com/dsa/graph-bfs ● https://www.tutorialspoint.com/difference-between-bfs-and-dfs ● https://www.tutorialspoint.com/data_structures_algorithms/breadth_first_trave rsal.htm
  • 26.