Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Algorithms

Breadth First Search (BFS) and Depth First Search (DFS) Algorithms



In this article, we learn about the concept of Breadth first search (BFS) and depth first search (DFS) and the algorithms of breadth first search and the depth first search.
Submitted by Shivangi Jain, on July 27, 2018

1) Breadth first search (BFS)

Breadth first search explores the space level by level only when there are no more states to be explored at a given level does the algorithm move on to the next level.

We implement BFS using lists open and closed to keep track of progress through the state space. In the order list, the elements will be those who have been generated but whose children have not been examined. The closed list records the states that have been examined and whose children have been generated. The order of removing the states from the open list will be the order of searching. The open is maintained as a queue on the first in first out data structure. States are added to the right of the list and removed from the left.

After initialization, each vertex is enqueued at most once and hence dequeued at most once. The operations of enqueuing and dequeuing take O (1) time, so the total time devoted to queue operations is O (v). Because the adjacency list of each vertex is scanned only when the vertex is dequeued, the adjacency list of each vertex is scanned at most once. Since the sum of the lengths of all the adjacency lists is the ta(E), at most O(E) time is spent in total scanning adjacency lists. The overhead for the initialization is O(v), and thus the total running time of BFS is O(v+E). Thus, breadth first search runs in time linear in the size of the adjacency list representation.

Algorithm - Breadth First Search (BFS)

    1.	Begin
    2.	Open = [start];
    3.	Closed =[];
    4.	While open != [] do
    5.	Begin
    6.	Remove leftmost state from open call it x;
    7.	If x is a goal then return success
    8.	Else
    9.	Begin
    10.	Generate children of x;
    11.	Put x on closed;
    12.	Put children on right end of open;
    13.	End
    14.	End
    15.	Return(failure)
    16.	End

2) Depth First Search (DFS)

In depth first search, when a state is examined all of its children and their descendants are examined before any of its siblings. Depth first search goes deeper into the search space whenever this is possible, only when no further descendants of a state can be found, are its siblings considered.

A depth first traversal takes O(N*E) time for adjacency list representation and O(N2) for matrix representation.

Algorithm - Depth First Search (DFS)

    1.	Begin
    2.	Open = [start];
    3.	Closed = [];
    4.	While open != [] do
    5.	Begin
    6.	Remove left most state from open call it x;
    7.	If x is a goal then return success
    8.	Else
    9.	Begin
    10.	Generate children of x;
    11.	Put x on closed;
    12.	Put children on left end of open;
    13.	End
    14.	End
    15.	Return (failure)
    16.	End

References:






Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.