You are on page 1of 18

Fundamental of Management

• Presented BY • Submitted To
Tammana Yeasmin MD. Tahzibul Islam
Department: C.S.E assistance Professore
Batch: E_60 Dhaka International University
Roll:
INDEX
SL No. Remarks Slide No.
1 Depth-First Search (DFS) Definition 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
Depth-First Search (DFS)
Definition :
The aim of the DFS algorithm is traverse the graph in such a way that is
try to go for from the root node. Stack is use in the implementation of
the depth first search.

A standard DFS implementation puts each vertex of the graph into one of
two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as visited while
avoiding cycles.
1
Algorithm steps:
Step:1 Push the root node in stack.
Step:2 Loop until stack is empty.
Step:3 Peek the node of the stack.
Step:4 If the node has unvisited child nodes get the unvisited child
node mark it has traverse and push it on stack.

2
We'll traverse the graph following these rules:
We'll start from the source.
No node will be visited twice.
The nodes we didn't visit yet, will be colored white.
The node we visited, but didn't visit all of its child nodes, will be colored grey.
Completely traversed nodes will be colored black.
DFS Example:
source
vertex

3
Kinds of Edges:
DFS introduces an important distinction among edges in the original graph:

Tree edge: encounter new (white) vertex


 Back edge: from descendent to ancestor
Forward edge: from ancestor to descendent
Cross edge: between a tree or sub-trees
From a grey node to a black node

4
How do you solve a maze?
Depth-first search is a common way that many people naturally
approach solving problems like mazes. First, we select a path in the
maze (for the sake of the example, let's choose a path according to
some a rule we lay out ahead of time) and we follow it until we hit a
dead end or reach the finishing point of the maze. If a given path
doesn’t work, we backtrack and take an alternative path from a past
junction, and try that path. Below is a DFS approach to solving this
maze.

5
Example:
Pseudocode :
Data: color[V], time, DFS_Visit(u)
prev[V],d[V], f[V] {
DFS(G) // where prog starts color[u] = GREY;
{ time = time+1;
for each vertex u  V d[u] = time;
{ for each v  Adj[u]
color[u] = WHITE; {
prev[u]=NIL; if(color[v] == WHITE){
f[u]=inf; d[u]=inf; prev[v]=u;
} DFS_Visit(v);}
time = 0; }
for each vertex u  V color[u] = BLACK;
if (color[u] == WHITE) time = time+1;
DFS_Visit(u); f[u] = time;
} }
6
Program :
#include<stdio.h>  for(i=0;i<n;i++)
         for(j=0;j<n;j++)
void DFS(int);             scanf("%d",&G[i][j]);
int G[10][10],visited[10],n;    //n is no of     //visited is initialized to zero
vertices and graph is sorted in array G[10]    for(i=0;i<n;i++)
[10]         visited[i]=0;
void main()     DFS(0);
{ }
    int i,j; void DFS(int i)
    printf("Enter number of vertices:"); {
       int j;
    scanf("%d",&n);     printf("\n%d",i);
    //read the adjecency matrix     visited[i]=1;
    printf("\nEnter adjecency matrix of the     
graph:");     for(j=0;j<n;j++)
          if(!visited[j]&&G[i][j]==1)
            DFS(j);
} 7
Output:

8
Time Complexity:
Assume that graph is connected. Depth-first search visits every
vertex in the graph and checks every edge its edge. Therefore, DFS
complexity is O(V + E). As it was mentioned before, if an adjacency
matrix is used for a graph representation, then all edges, adjacent to
a vertex can't be found efficiently, that results in O(V2) complexity.

9
Applications of Depth First Search:
Finding all pair shortest path in an undirected graph.
Detecting cycle in a graph. Path finding.
Topological Sort.
Testing if a graph is bipartite.
Finding Strongly Connected Component.
Solving puzzles with one solution.
Advantage:
•The advantage of depth-first Search is that memory requirement is only
linear with respect to the search graph. This is in contrast with breadth-
first search which requires more space. The reason is that the algorithm
only needs to store a stack of nodes on the path from the root to the
current node.
• The time complexity of a depth-first Search to depth d is O(b^d) since
it generates the same set of nodes as breadth-first search, but simply in
a different order. Thus practically depth-first search is time-limited rather
than space-limited.
• If depth-first search finds solution without exploring much in a path
then the time and space it takes will be very less.
10
Disadvantage:
•The disadvantage of Depth-First Search is that there is a possibility that it
may go down the left-most path forever. Even a finite graph can generate
an infinite tree. One solution to this problem is to impose a cutoff depth
on the search. Although the ideal cutoff is the solution depth d and this
value is rarely known in advance of actually solving the problem. If the
chosen cutoff depth is less than d, the algorithm will fail to find a solution,
whereas if the cutoff depth is greater than d, a large price is paid in
execution time, and the first solution found may not be an optimal one.
• Depth-First Search is not guaranteed to find the solution.
• And there is no guarantee to find a minimal solution, if more than one
solution exists.
11
HAVE YOU ANY
QUESTION’S ?

You might also like