-1

I have searched on Google and here too about any starting level question which has the implementation of BFS! Actually I know the algorithm of BFS and I have created a program of BFS which is working fine but I am not getting the use of BFS, I mean I want a simple starting level question to try BFS! I don't want source code just a question so that I can know about how and where to use BFS and it would be better if you provide info about DFS too! and yes I am new here and if I made any mistake forgive me!

Dominique Fortin
  • 2,212
  • 15
  • 20
Deepanshu
  • 488
  • 6
  • 18
  • This: is the most intuitive way of understanding what BFS and DFS do, as well has the differences between them. That is, however, just an opinion and as such really doesn't belong on SO. For extra credit, try bi-directional BFS on the above problem. – beaker Dec 07 '14 at 20:12

2 Answers2

1

It has all the info about BFS and DFS and why they used? And also relation between them.

http://www.ics.uci.edu/~eppstein/161/960215.html

BFS Animation

https://www.cs.usfca.edu/~galles/visualization/BFS.html

DFS Animation

https://www.cs.usfca.edu/~galles/visualization/DFS.html

I hope it helps you!

Muhammad Noman
  • 1,344
  • 1
  • 14
  • 28
0

When it says multiple sources it is referring to the start node of the search. You'll notice that the parameters of the algorithms are BFS(G, s) and DFS(G). That should already be a hint that BFS is single-source and DFS isn't, since DFS doesn't take any initial node as an argument.

The major difference between these two, as the authors point out, is that the result of BFS is always a tree, whereas DFS can be a forest (collection of trees). Meaning, that if BFS is run from a node s, then it will construct the tree only of those nodes reachable from s, but if there are other nodes in the graph, will not touch them. DFS however will continue its search through the entire graph, and construct the forest of all of these connected components. This is, as they explain, the desired result of each algorithm in most use-cases.

As the authors mentioned there is nothing stopping slight modifications to make DFS single source. In fact this change is easy. We simply accept another parameter s, and in the routine DFS (not DFS_VISIT) instead of lines 5-7 iterating through all nodes in the graph, we simply execute DFS_VISIT(s).

Similarly, changing BFS is possible to make it run with multiple sources. I found an implementation online: http://algs4.cs.princeton.edu/41undirected/BreadthFirstPaths.java.html although that is slightly different to another possible implementation, which creates separate trees automatically. Meaning, that algorithm looks like this BFS(G, S) (where S is a collection of nodes) whereas you can implement BFS(G) and make separate trees automatically. It's a slight modification to the queueing and I'll leave it as an exercise.

As the authors point out, the reason these aren't done is that the main use of each algorithm lends to them being useful as they are. Although well done for thinking about this, it is an important point that should be understood.

mfs
  • 3,984
  • 6
  • 32
  • 51