You are on page 1of 46

Advance Analysis Of Algorithm

Depth First Search & Breadth First Search

Supervised by: Dr Noman Hasany

Presentation by:
Jyoti Shina (2073112)
Sarooj Danish (2073130)
Ritu Talreja (2073129)
Maaz Ahmed (2073113)
UNINFORMED SEARCH
• Uninformed search methods use only information available in the
problem definition.
– Breadth First Search (BFS)
– Depth First Search (DFS)
– Iterative DFS (IDA)
– Bi-directional search
– Uniform Cost Search (a.k.a. Dijkstra alg.)
DEPTH FIRST SEARCH
Depth First Search
• Depth first search was first investigated by French Mathematician
Charles Pierre Tremaux
• It is an Algorithm for traversing tree or graph data structures
• One starts at the root and explores as deep as possible along each
branch before backtracking.
• It can be implemented using Stack
What is Stack ?
• A stack is a data structure in which items can be inserted only from
one end and get items back from the same end
• The last item inserted into stack, is the first item to be taken out from
the stack
• LIFO
Stack Examples
Concept of DFS
• Start at “root” node
• Set of possible paths is just root node
• If not at “goal” node, then
• Extend current path by adding each “child” of current node to path,
unless child already in path
• Add these new paths to potential set of paths, at front of set
• Select next path and recursively repeat
• Current node has no “children”, then just go to next option
• Stop when reach “goal” node, or when no more paths to explore
ALGORITHM OF DFS
DFS(graph, start, end, path = []):
# Assumes graph is a Digraph
# Assumes start and end are nodes in graph
path = path + [start]
print 'Current dfs path:', printPath(path)
if start == end:
return path
for node in graph.childrenOf(start):
if node not in path: # Avoid cycles
newPath = DFS(graph,node,end,path)
if newPath != None:
return newPath
return None
EXAMPLE
• Consider Graph G(V, E) containing 10 vertices, consider A as source
node initially Stack is empty.
• Consider Graph G(V, E) containing 9 vertices, consider A as source node initially Stack is empty.

STACK
A

B C D

E F G H
• Start visit source node A in Graph G(V, E), inserted into the stack and mark as Red i.e. mark as
visited.

STACK
A

B C D

E F G H A

Display: A
• Start visiting the unvisited adjacent nodes to source node A in Graph G(V, E), i.e. B inserted into
the stack and mark as Red.

STACK
A

B C D

B
E F G H A

Display: A B
• Start visiting the unvisited adjacent nodes to B in Graph G(V, E), i.e. E inserted into the stack and
mark as Red.

STACK
A

B C D

E
B
E F G H A

Display: A B E
• As no adjacent vertex to E and is already visited so pop the TOP element from stack i.e E is
removed from stack and pointer shifts to B.

STACK
A

B C D

B
E F G H A

Display: A B E
• Again Start with visiting the unvisited adjacent nodes to B in Graph G(V, E), i.e. F inserted into the
stack and mark as Red and pointer shifts to F.

STACK
A

B C D

F
B
E F G H A

Display: A B E F
• As no adjacent vertex to E and is already visited so pop the TOP element from stack i.e. F is
removed from stack and pointer shifts to B.

STACK
A

B C D

B
E F G H A

Display: A B E F
• As adjacent vertex to B are already visited i.e. E and F so pop the TOP element from stack B is
removed from stack and pointer shifts to A.

STACK
A

B C D

E F G H A

Display: A B E F
• Start visiting the unvisited adjacent nodes to node A in Graph G(V, E), i.e. C inserted into the stack
and mark as Red and pointer shifts to C.

STACK
A

B C D

C
E F G H A

Display: A B E F C
• Start with visiting the unvisited adjacent nodes to C in Graph G(V, E), i.e. G inserted into the stack
and mark as Red and pointer shifts to G.

STACK
A

B C D

G
C
E F G H A

Display: A B E F C G
• As no adjacent vertex to G and is already visited so pop the TOP element from stack i.e. G is
removed from stack and pointer shifts to C.

STACK
A

B C D

C
E F G H A

Display: A B E F C G
• Again visiting C As adjacent vertex to C is already visited i.e. G so pop the TOP element from stack
C is removed from stack and pointer shifts to A.

STACK
A

B C D

E F G H A

Display: A B E F C G
• Start visiting the unvisited adjacent nodes to node A in Graph G(V, E), i.e. D inserted into the stack
and mark as Red and pointer shifts to D.

STACK
A

B C D

D
E F G H A

Display: A B E F C G D
• Start visiting the unvisited adjacent nodes to source node D in Graph G(V, E), i.e. H inserted into
the stack and mark as Red and pointer shifts to H.

STACK
A

B C D

H
D
E F G H A

Display: A B E F C G D H
• As no unvisited adjacent vertex to H found so remove the TOP element from stack and shifts
pointer to D.

STACK
A

B C D

D
E F G H A

Display: A B E F C G D H
• As no unvisited adjacent to node D found so remove the TOP element D from the stack and shifts
pointer to A.

STACK
A

B C D

E F G H A

Display: A B E F C G D H
• As again start visiting the node A no unvisited adjacent vertex found so remove the TOP element
from stack A and Stack is “EMPTY”.

STACK
A

B C D

E F G H

Display: A B E F C G D H
Time Complexity
• Time complexity: O(V + E), where V is the number of vertices and E is
the number of edges in the graph.
Real Life Example
• Detecting cycles
• A topological sort
• Strongly Connected Component
Limitation of DFS

• Go down to the left most path


• Not a guaranteed result
• No guarantee to find a minimal solution, if more than one solution
exists
Graphs

• A graph is a pictorial representation of a set of objects


• where some pairs of objects are connected by links.
• The interconnected objects are represented by points termed
as vertices, and the links that connect the vertices are called edges.
• Formally, a graph is a pair of sets (V, E), where V is the set of vertices
and E is the set of edges, connecting the pairs vertices
Example

In the above graph,


V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
Queue

• Queue is also an abstract data type or a linear data


structure

• The first element is inserted from one end called


the REAR(also called tail), and the removal of existing
element takes place from the other end called
as FRONT(also called head).

• This makes queue as FIFO(First in First Out) data structure,


which means that element inserted first will be removed
first.
Example
BREADTH FIRST SEARCH
• Breadth-first search (BFS) is an algorithm that is used to graph data or
searching tree or traversing structures. The full form of BFS is the
Breadth-first search.
• The algorithm efficiently visits and marks all the key nodes in a graph
in an accurate breadthwise fashion.
• This algorithm selects a single node (initial or source point) in a graph
and then visits all the nodes adjacent to the selected node
Concept Diagram
Real-life Examples
GPS Navigation System:
• BFS algorithm is use in GPS system as it detects all the neighboring locations. And also identifies
the shortest path to the location you searched for
• The Breadth First Search algorithm looks at the map as we do; it just can’t perceive it completely.
When you have to travel from one destination to another, you draw a line from point A to point B,
and then chose the road closest to that line
• Algorithms repeat the same method choosing the node nearest to the intersection points, eventually
selecting the route with the shortest length.
P2P Networks:
• BFS algorithm is also use in p2p networks it’s a distributed system in which peer employ distribute
resources to perform a task that is critical in nature every node in a p2p network
• plays equal role as each system is connected to one another in a decentralized manner therefor
these nodes are called peers
Limitations

1. One disadvantage of BFS is that it is a 'blind' search, when the search space is large the search
performance will be poor compared to other heuristic searches. BFS will perform well if the search
space is small. It performs best if the goal state lies in upper left-hand side of the tree.
2. Breadth first search is its memory requirement. Since each level of the tree must be saved in order
to generate the next level.
ALGORITHM OF BFS
Breadth First Search technique involves FIFO (Frist In First Out) Structure.
Therefore, the neighbors of the vertex will be visited in the order in which they were inserted in the graph i.e., the vertex that was inserted first
will be visited first, and so on.

BFS (G, s) //Where G is the graph and s is the source node


let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbor vertices are marked.
mark s as visited.
while ( Q is not empty)
//Removing that vertex from queue, whose neighbor will be visited now
v = Q.dequeue( )
// processing all the neighbors of v
for all neighbors w of v in Graph
if w is not visited
Q.enqueue( w ) //Stores w in Q to further visit its neighbour
mark w as visited
TRAVERSAL METHOD OF BFS

1. (G, s) is input, here G is the graph and “s” is the root node
2. Create a list of vertices name A queue ‘Q’ and initialized with the source vertex ‘s’
3. All child vertices of ‘s’ are marked
4. Extract ‘s from queue and visit all adjacent the child vertices.
5. Process all the child vertexes of v
6. Stores w (child nodes) in Q to further visit its child vertexes
7. Continue till ‘Q’ is empty
The while loop iterates vertices, which are discovered vertices that have not yet had their adjacency lists fully
examined.
RULES FOR ALGORITHM
• Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
• Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
• Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
VISUALIZATION FOR BFS

Breadth-first search algorithm visits each vertex in blue, green and red color. All vertices start out with Blue in color and may
later become Green and then Red.
 
• ADJACENT VERTEX: Current vertex which are to be discovered not visited colored in Blue.
• VISITED VERTEX: Vertices already visited and delete from queue Q colored in Red
• UNVISITED VERTEX: Initially all vertices are un visited and Green in color.

QUEUE SIZE
Given in the graph below the queue Q has total queue size is: 13 vertices. Starting vertex, A in the head of the queue Q.
Given the graph consider G(V, E), consider the source node “A” at initial Queue Q is empty.

A STEPS:

B C D

E F G H I
J
J K L

N
FIFO QUEUE:
                         
Given the graph consider G(V, E), consider the source node “A” at initial Queue Q is empty.

A STEPS:
dequeue
dequeue
No vertex
dequeue
No
Visiting
visit KF
vertexnext
next
finished
finished
Enqueue
Queue vertex
vertex
discovered
N
isDiscovered
Enqueue
next
neighborsEmpty
L vertex
vertex
Found
neighbors ofof
No HEnqueue
Vertex
FICdiscovered
dequeue
dequeueCEBfinished
finished
visit
Enqueue
visit
G
Enqueue E
D
Dequeue
B next
neighbors
finished
Dneighbors J,K
next
discovered
discovered I
E,F
with
finished
discovered
discovered
source
discovered
Enqueue vertex ofAF
vertex
discovered
G,H
with BAAK
E
of
node
B,C,D C
D B
A

B C D

E F G H I
J
JJ K LL

N
FIFO QUEUE:
A B C     
D E   F  G  
H   I   J  
K   L   N  
AFTER BFS: Given the graph G(V,E) successfully find the element while traversing the graph.
A STEPS:
Queue is Empty

B C D

E F G H I

J K L

N
FIFO QUEUE:
                         
CONCLUSION
• optimality:
• always finds the shortest path (fewest edges).
• in unweighted graphs, finds optimal cost path.
• In weighted graphs, not always optimal cost.

• retrieval: harder to reconstruct the actual sequence of vertices or edges in the path once you find it
• conceptually, BFS is exploring many possible paths in parallel, so it's not easy to store a path array/list in progress
• solution: We can keep track of the path by storing predecessors for each vertex (each vertex can store a reference to
a previous vertex).

• DFS uses less memory than BFS, easier to reconstruct the path once found; but DFS does not always find shortest path.
BFS does.
Any questions
Feedback

You might also like