Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breadth First Search and Depth First Search

Can anybody give a link for a simple explanation on BFS and DFS with its implementation?

like image 276
Jony Avatar asked Mar 24 '10 04:03

Jony


People also ask

What is the difference between depth first and breadth first search?

BFS starts traversal from the root node and visits nodes in a level by level manner (i.e., visiting the ones closest to the root first). DFS starts the traversal from the root node and visits nodes as far as possible from the root node (i.e., depth wise). Usually implemented using a queue data structure.

What is the difference between breadth first search and best first search?

Best-first search is informed whereas Breadth-first search is uninformed, as in one has a metal detector and the other doesn't! Breadth-first search is complete, meaning it'll find a solution if one exists, and given enough resources will find the optimal solution.

What is DFS and BFS used for?

Graph Theory Algorithms We can detect cycles in a graph using DFS. If we get one back-edge during BFS, then there must be one cycle. Using DFS we can find path between two given vertices u and v. We can perform topological sorting is used to scheduling jobs from given dependencies among jobs.

What is breadth first search with example?

Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue to remember to get the next vertex to start a search, when a dead end occurs in any iteration. As in the example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D.


4 Answers

Depth First Search:

alt text

like image 83
claws Avatar answered Sep 17 '22 08:09

claws


Lets say you are given the following structure:

 Format: Node [Children]  A [B C D] B [E F] C [G] D [] E [] F [] G [] 

A breadth first search visits all of a node's children before visiting their children. Here's the pseudocode and the solution for the above structure:

 1. Enqueue root node. 2. Dequeue and output. If the queue is empty, go to step 5. 3. Enqueue the dequeued node's children. 4. Go to Step 2. 5. Done 
 Two queues: (Active Node) [Output] [Working Set] Starting with root: ( ) []              [A] (A) [A]             [B C D]  (B) [A B]           [C D E F]  (C) [A B C]         [D E F G]  (D) [A B C D]       [E F G]  (E) [A B C D E]     [F G]  (F) [A B C D E F]   [G]  (G) [A B C D E F G] []   Done 

A depth first search visits the lowest level (deepest children) of the tree first instead. There are two types of depth first search: pre-order and post-order. This just differentiates between when you add the node to the output (when you visit it vs leave it).

     var rootNode = structure.getRoot();     var preOrder = new Array();     var postOrder = new Array();     function DepthFirst( rootNode ){         // Pre-order         preOrder[ preOrder.length ] = rootNode;          for( var child in rootNode ){             DepthFirst( child );         }          // Post-order         postOrder[ postOrder.length ] = rootNode;     } 
 Pre-order: * A B E F C G D  Post-order: * E F B G C D A 
like image 29
apandit Avatar answered Sep 17 '22 08:09

apandit


Say you have a tree as follows:

alt text

It may be a little confusing because E is both a child of A and F but it helps illustrate the depth in a depth first search. A depth first search searches the tree going as deep (hence the term depth) as it can first. So the traversal left to right would be A, B, D, F, E, C, G.

A breadth first search evaluates all the children first before proceeding to the children of the children. So the same tree would go A, B, C, E, D, F, G.

Hope this helps.

like image 22
Ternary Avatar answered Sep 19 '22 08:09

Ternary


you can find everything on wiki:

BFS and DFS

this link can be useful too. if you want an implementation go to: c++ boost library: DFS

like image 38
sorush-r Avatar answered Sep 20 '22 08:09

sorush-r