SlideShare una empresa de Scribd logo
1 de 11
DEPTH FIRST
SEARCH [DFS]
By,
K.B.Snega,M.sc(cs).,
INTRODUCTION
 A depth-first search (DFS) explores a path all the
way to a leaf before backtracking and exploring
another path.
 For example, after searching A, then B, then D, the
search backtracks and tries another path from B.
 Node are explored in the order A B D E H L M N I O
P C F G J K Q. L M N O P N will be found before
J.
INTRO….
 To keep track of progress DFS colors each vertex
white, gray or black. Initially all the vertices are
colored white. Then they are colored gray when
discovered. Finally colored black when finished.
 Besides creating depth first forest DFS also
timestamps each vertex. Each vertex goes through
two time stamps:
 Discover time d[u]: when u is first discovered
Finish time f[u]: when backtrack from u or finished
u
f[u] > d[u]
DFS: ALGORITHM
DFS: Algorithm
DFS(G)
1. for each vertex u in G
2. color[u]=white
3. ᴨ[u]=NIL
4. time=0
5. for each vertex u in G
6. if (color[u]==white)
7. DFS-VISIT(G,u)
ALGO(CONT…)
DFS-VISIT(u)
1. time = time + 1
2. d[u] = time
3. color[u]=gray
4. for each v € Adj(u) in G do
5. if (color[v] = =white)
6. ᴨ [v] = u;
7. DFS-VISIT(G,v);
8. color[u] = black
9. time = time + 1;
10. f[u]= time;
DFS: COMPLEXITY ANALYSIS
 Initialization complexity is O(V)
 DFS_VISIT is called exactly once for each vertex
 And DFS_VISIT scans all the edges which causes
cost of O(E)
 Thus overall complexity is O(V + E)
DFS: APPLICATION
 Topological Sort
 Strongly Connected Component
CLASSIFICATION OF EDGES
 Tree edge: Edge (u,v) is a tree edge if v was first
discovered by exploring edge (u,v). White color
indicates tree edge.
 Back edge: Edge (u,v) is a back edge if it
connects a vertex u to a ancestor v in a depth
first tree. Gray color indicates back edge.
 Forward edge: Edge (u,v) is a forward edge if it
is non-tree edge and it connects a vertex u to a
descendant v in a depth first tree. Black color
indicates forward edge.
 Cross edge: rest all other edges are called the
cross edge. Black color indicates forward edge.
EXAMPLE OF EDGES
THEOREM DERIVED FROM DFS
 Theorem 1: In a depth first search of an undirected
graph G, every edge of G is either a tree edge or
back edge.
 Theorem 2: A directed graph G is acyclic if and
only if a depth-first search of G yields no back
edges.
Depth first search [dfs]

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
BFS
BFSBFS
BFS
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Arrays
ArraysArrays
Arrays
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
Linked list
Linked listLinked list
Linked list
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
single linked list
single linked listsingle linked list
single linked list
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 

Similar a Depth first search [dfs]

graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19
sajinsc
 

Similar a Depth first search [dfs] (20)

DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
ada-191015145338.pdf
ada-191015145338.pdfada-191015145338.pdf
ada-191015145338.pdf
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Graph 02
Graph 02Graph 02
Graph 02
 
lecture 18
lecture 18lecture 18
lecture 18
 
lecture 19
lecture 19lecture 19
lecture 19
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 

Más de DEEPIKA T (20)

See
SeeSee
See
 
71619109 configuration-management.pdf (1) (1)
71619109 configuration-management.pdf (1) (1)71619109 configuration-management.pdf (1) (1)
71619109 configuration-management.pdf (1) (1)
 
80068
8006880068
80068
 
242296
242296242296
242296
 
Data mining
Data miningData mining
Data mining
 
Parallelizing matrix multiplication
Parallelizing  matrix multiplicationParallelizing  matrix multiplication
Parallelizing matrix multiplication
 
Health care in big data analytics
Health care in big data analyticsHealth care in big data analytics
Health care in big data analytics
 
Ajax
AjaxAjax
Ajax
 
Role of human interaction
Role of human interactionRole of human interaction
Role of human interaction
 
Basic analtyics & advanced analtyics
Basic analtyics & advanced analtyicsBasic analtyics & advanced analtyics
Basic analtyics & advanced analtyics
 
Soap,Rest&Json
Soap,Rest&JsonSoap,Rest&Json
Soap,Rest&Json
 
Applet (1)
Applet (1)Applet (1)
Applet (1)
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
 
Appletjava
AppletjavaAppletjava
Appletjava
 
Remote method invocation
Remote  method invocationRemote  method invocation
Remote method invocation
 
Graph representation
Graph representationGraph representation
Graph representation
 
Al
AlAl
Al
 
Presentation2
Presentation2Presentation2
Presentation2
 
Topological sort
Topological sortTopological sort
Topological sort
 
Path compression
Path compressionPath compression
Path compression
 

Último

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Último (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

Depth first search [dfs]

  • 2. INTRODUCTION  A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path.  For example, after searching A, then B, then D, the search backtracks and tries another path from B.  Node are explored in the order A B D E H L M N I O P C F G J K Q. L M N O P N will be found before J.
  • 3. INTRO….  To keep track of progress DFS colors each vertex white, gray or black. Initially all the vertices are colored white. Then they are colored gray when discovered. Finally colored black when finished.  Besides creating depth first forest DFS also timestamps each vertex. Each vertex goes through two time stamps:  Discover time d[u]: when u is first discovered Finish time f[u]: when backtrack from u or finished u f[u] > d[u]
  • 4. DFS: ALGORITHM DFS: Algorithm DFS(G) 1. for each vertex u in G 2. color[u]=white 3. ᴨ[u]=NIL 4. time=0 5. for each vertex u in G 6. if (color[u]==white) 7. DFS-VISIT(G,u)
  • 5. ALGO(CONT…) DFS-VISIT(u) 1. time = time + 1 2. d[u] = time 3. color[u]=gray 4. for each v € Adj(u) in G do 5. if (color[v] = =white) 6. ᴨ [v] = u; 7. DFS-VISIT(G,v); 8. color[u] = black 9. time = time + 1; 10. f[u]= time;
  • 6. DFS: COMPLEXITY ANALYSIS  Initialization complexity is O(V)  DFS_VISIT is called exactly once for each vertex  And DFS_VISIT scans all the edges which causes cost of O(E)  Thus overall complexity is O(V + E)
  • 7. DFS: APPLICATION  Topological Sort  Strongly Connected Component
  • 8. CLASSIFICATION OF EDGES  Tree edge: Edge (u,v) is a tree edge if v was first discovered by exploring edge (u,v). White color indicates tree edge.  Back edge: Edge (u,v) is a back edge if it connects a vertex u to a ancestor v in a depth first tree. Gray color indicates back edge.  Forward edge: Edge (u,v) is a forward edge if it is non-tree edge and it connects a vertex u to a descendant v in a depth first tree. Black color indicates forward edge.  Cross edge: rest all other edges are called the cross edge. Black color indicates forward edge.
  • 10. THEOREM DERIVED FROM DFS  Theorem 1: In a depth first search of an undirected graph G, every edge of G is either a tree edge or back edge.  Theorem 2: A directed graph G is acyclic if and only if a depth-first search of G yields no back edges.