Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of BFS and DFS?

I've learned how these algorithms work, but what are they used for?

Do we use them to:

  • find a certain node in a graph or
  • to find a shortest path or
  • to find a cycle in a graph

?

Both of them just visit all the nodes and mark them visited, and I don't see the point of doing that.

I am sort of lost here what I am learning.

like image 517
Nayana Avatar asked Feb 06 '13 02:02

Nayana


1 Answers

BFS and DFS are graph search algorithms that can be used for a variety of different purposes.

One common application of the two search techniques is to identify all nodes that are reachable from a given starting node. For example, suppose that you have a collection of computers that each are networked to a handful of other computers. By running a BFS or DFS from a given node, you will discover all other computers in the network that the original computer is capable of directly or indirectly talking to. These are the computers that come back marked.

BFS specifically can be used to find the shortest path between two nodes in an unweighted graph. Suppose, for example, that you want to send a packet from one computer in a network to another, and that the computers aren't directly connected to one another. Along what route should you send the packet to get it to arrive at the destination as quickly as possible? If you run a BFS and at each iteration have each node store a pointer to its "parent" node, you will end up finding route from the start node to each other node in the graph that minimizes the number of links that have to be traversed to reach the destination computer.

DFS is often used as a subroutine in more complex algorithms. For example, Tarjan's algorithm for computing strongly-connected components is based on depth-first search. Many optimizing compiler techniques run a DFS over an appropriately-constructed graph in order to determine in which order to apply a specific series of operations. Depth-first search can also be used in maze generation: by taking a grid of nodes and linking each node to its neighbors, you can construct a graph representing a grid. Running a random depth-first search over this graph then produces a maze that has exactly one solution.

This is by no means an exhaustive list. These algorithms have all sorts of applications, and as you start to explore more advanced algorithms you will often find yourself relying on DFS and BFS as building blocks. It's similar to sorting - sorting by itself isn't all that interesting, but being able to sort a list of values is enormously useful as a subroutine in more complex algorithms.

Hope this helps!

like image 70
templatetypedef Avatar answered Oct 14 '22 08:10

templatetypedef