What is the difference between DFS and BFS? Where can I apply each?

DFS, or Depth First Search traverses a graph - which is any set of interconnected nodes 'depth first'. It tries to go all the way to the end, and if it fails returns back to the last intersection that it didn't try. BFS, or Breadth First Search, traverses a graph by only going to the next set of nodes from it's current set. In effect it goes to all the nodes of distance one from the starting node, and in the next step goes to all nodes 2 from the starting node. The applications of each depend heavily on the use case. If the nodes we were looking for are deep in the graph, using BFS could be impractical as it could potentially take a long time. If solutions are rare, and can be anywhere in the graph, BFS would be significantly faster than DFS. DFS also has the added advantage of giving metrics such as shortest distance from the starting point, meanwhile the same metric in DFS would be harder to implement.

TD
Answered by Tutor86199 D. Python tutor

4059 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Firstly, my question is not Python related, but maths A-level related. I don't know how to change that in my profile but I would like to only tutor maths GCSE and potentially A-level. My question: Integrate sin(x)^3 over x=0 and x=pi/2.


Print "Hello World!" ten times without typing (or pasting) the print function more than once


Create a program that generates prime numbers between two integer boundareis


Create a python code to sum the number from 1 to 10.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences