290 likes | 592 Views
Searching Algorithms. Breadth First Search and Depth First Search. Why Search?. Greatest problem in Computer Science Has lead to a lot of new ideas and data structures Search engines before G oogle dawned were very slow. Search engines even took 10 minutes to search.
E N D
Searching Algorithms Breadth First Search and Depth First Search
Why Search? • Greatest problem in Computer Science • Has lead to a lot of new ideas and data structures • Search engines before Google dawned were very slow. Search engines even took 10 minutes to search.
Trees were created to store data • Finding the data requires searching • Graph searching and Tree Searching algorithms that we are going to learn today are BFS and DFS.
Breadth First Search • b- branching • d - depth 1 4 2 3 10 9 5 6 5 6 7 7 8
Breadth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Breadth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Breadth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Breadth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Breadth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Properties of BFS • Worst case performance – O(bd) • Worst case space complexity - O(bd) • Implemented using a queue. But you are free to experiment with other data structures
Depth First Search • b – branching • d - depth 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Depth First Search 1 4 2 3 10 9 5 6 5 6 7 7 8
Can you guess the Time and Space complexity of the DFS? • Yes, Time complexity of DFS is O(bd)
Properties of DFS • Worst case performance – O(bd) • Worst case space complexity - O(d) • Implemented using a queue. But you are free to experiment with other data structures
Are you sure you know BFS and DFS? • What will you do if there is a loop in DFS? What are the data structures to use? • In order to reduce space complexity in BFS, why don’t we just delete all the nodes that are visited until the previous depth?