BFS DFS
-
Upload
madhurimapatra1987 -
Category
Documents
-
view
377 -
download
3
description
Transcript of BFS DFS
![Page 1: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/1.jpg)
Breadth-First-Search
And
Depth-First-Search
Prepared by Madhurima Patra
Roll No: 14401062011
![Page 2: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/2.jpg)
BFS:- Breadth first search
BFS is a graph search algorithm that begins at the root node and explores all the neighbouring nodes. Then for each of those nearest nodes, it explores their unexplored neighbour nodes, and so on, until it finds the goal. It is implemented by using queue.
A
B C D
E FStatus=1(ready)
Status=2(waiting)
Status=3(processed)
![Page 3: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/3.jpg)
A
B C D
E F
A
Status=1
Status=2
Status=3
L0
L1
L2
QUEUE
OUTPUT
![Page 4: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/4.jpg)
A
B C D
E F
A
B C D
Status=1
Status=3
Status=2
L0
L1
L2
QUEUE
OUTPUT
![Page 5: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/5.jpg)
A
B C D
E F
A B
C D E
L0
L1
L2
Status=1 Status=2
Status=3QUEUE
OUTPUT
![Page 6: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/6.jpg)
A
B C D
E F
A B C
D E F
L0
L1
L2Status=1 Status=2
Status=3QUEUE
OUTPUT
![Page 7: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/7.jpg)
A
B C D
E F
A B C D
E F
L0
L1
L2
Status=1 Status=2
Status=3QUEUE
OUTPUT
![Page 8: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/8.jpg)
A
B C D
E F
A B C D E
F
L0
L1
L2Status=1 Status=2
Status=3QUEUE
OUTPUT
![Page 9: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/9.jpg)
A
B C D
E F
A B C D E F
L0
L1
L2
Status=1 Status=2
Status=3QUEUE
OUTPUT
![Page 10: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/10.jpg)
A
B C D
E F
BFS of the graph
![Page 11: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/11.jpg)
Algorithm of BFSSTEP 1:- Initialize the status of all nodes to ready status (status =1).STEP 2:- Insert the starting node into the queue and set its status to waiting (status=2).STEP 3:- Continue step 4 and 5 until the queue is empty.STEP 4:- Delete the front node from the queue and set its status to processed (status=3).STEP 5:- Insert all the neighboring nodes (whose status is ready) of the deleted node into the queue and set their status to waiting (status=2).STEP 6:- Exit.
![Page 12: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/12.jpg)
Features of BFSSpace complexitySpace complexity is proportional to the number of nodes at the deepest level. Given a branching factor b and graph depth d the space complexity is the number of nodes at the deepest level, O(b ).Time complexityTime complexity of breadth-first search is O(b ). CompletenessBreadth-first search is complete. Proof of CompletenessIf the shallowest goal node is at some finite depth say d, breadth-first search will eventually find it after expanding all shallower nodes.OptimalityFor unit-step cost, breadth-first search is optimal.
d
d
![Page 13: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/13.jpg)
Applications of BFS
• Finding all connected components in a graph.
• Finding all nodes within one connected component.
• Finding the shortest path between two nodes u and v in an unweighted graph.
• Finding the shortest path between two nodes u and v in a weighted graph..
• Compute a spanning forest of a graph.
![Page 14: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/14.jpg)
DFS:- Depth first search
DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it has not finished exploring. It is implemented using a stack.
A
B C D
F GE
I J
![Page 15: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/15.jpg)
A
B C D
F GE
I J
A
Stack
Output
![Page 16: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/16.jpg)
A
B C D
F GE
I JB
C
D
A
Stack
Output
![Page 17: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/17.jpg)
A
B C D
F GE
I J
E
F
C
D
A B
Stack
Output
![Page 18: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/18.jpg)
A
B C D
F GE
I J
A B E
I
F
C
D
Stack
Output
![Page 19: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/19.jpg)
A
B C D
F GE
I J
A B E I
F
C
D
Stack
Output
![Page 20: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/20.jpg)
A
B C D
F GE
I J
A B E I F
J
C
D
Stack
Output
![Page 21: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/21.jpg)
A
B C D
F GE
I J
A B E I F J
C
D
Stack
Output
![Page 22: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/22.jpg)
A
B C D
F GE
I J
A B E I F J C
D
Stack
Output
![Page 23: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/23.jpg)
A
B C D
F GE
I J
A B E I F J C D
G
Stack
Output
![Page 24: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/24.jpg)
A
B C D
F GE
I J
A B E I F J C D G
Stack
Output
![Page 25: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/25.jpg)
A
B C D
F GE
I J
DFS of the graph
![Page 26: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/26.jpg)
Algorithm of DFSSTEP 1:- Initialize the status of all nodes to ready status (status=1).STEP 2:- Push the starting node into the stack (status=waiting=2).STEP 3:- Continue step 4 and 5 until the stack is empty.STEP 4:- Pop the top node from the stack and set its status to processed state (status=3).STEP 5:- Push all the successors whose status is ready; of the popped node into the stack and set their status to waiting (status=2).STEP 6:- Exit.
![Page 27: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/27.jpg)
Time complexitySince in the worst case depth-first search has to consider all paths to all possible nodes the time complexity of depth-first search is O(b ).
Space complexityThe space complexity is O(d) where d is the length of the longest simple path in the graph.
Features of DFS
CompletenessDepth-first search is not complete.
OptimalityDepth-first search is not optimal.
d
![Page 28: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/28.jpg)
Applications of DFS• Finding whether there exists a path between the given vertices.
• Find the connected components of a graph.
• Topological sorting.
• We can specialize the DFS algorithm to find a simple cycle.
• Solving puzzles with only one solution, such as mazes. (DFS can be adapted to find all solutions to a maze by only including nodes on the current path in the visited set.)
![Page 29: BFS DFS](https://reader033.fdocuments.us/reader033/viewer/2022051513/546b0f02b4af9fc6128b49d2/html5/thumbnails/29.jpg)