SlideShare uma empresa 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]

Mais conteúdo relacionado

Mais procurados (20)

Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Floyd Warshall Algorithm
Floyd Warshall AlgorithmFloyd Warshall Algorithm
Floyd Warshall Algorithm
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Red black tree
Red black treeRed black tree
Red black tree
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Deadlock dbms
Deadlock dbmsDeadlock dbms
Deadlock dbms
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptx
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 

Semelhante a Depth first search [dfs]

DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdfRajkk5
 
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 3Deepak John
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
ada-191015145338.pdf
ada-191015145338.pdfada-191015145338.pdf
ada-191015145338.pdfAshwin180668
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)Dhrumil Panchal
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
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 TraversalAmrinder Arora
 
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.docxwhittemorelucilla
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs applicationUmme habiba
 
lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
lecture 19
lecture 19lecture 19
lecture 19sajinsc
 

Semelhante 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
 
BFS
BFSBFS
BFS
 
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)
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
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
 

Mais de DEEPIKA T

71619109 configuration-management.pdf (1) (1)
71619109 configuration-management.pdf (1) (1)71619109 configuration-management.pdf (1) (1)
71619109 configuration-management.pdf (1) (1)DEEPIKA T
 
Parallelizing matrix multiplication
Parallelizing  matrix multiplicationParallelizing  matrix multiplication
Parallelizing matrix multiplicationDEEPIKA T
 
Health care in big data analytics
Health care in big data analyticsHealth care in big data analytics
Health care in big data analyticsDEEPIKA T
 
Role of human interaction
Role of human interactionRole of human interaction
Role of human interactionDEEPIKA T
 
Basic analtyics & advanced analtyics
Basic analtyics & advanced analtyicsBasic analtyics & advanced analtyics
Basic analtyics & advanced analtyicsDEEPIKA T
 
Soap,Rest&Json
Soap,Rest&JsonSoap,Rest&Json
Soap,Rest&JsonDEEPIKA T
 
Remote method invocation
Remote  method invocationRemote  method invocation
Remote method invocationDEEPIKA T
 
Graph representation
Graph representationGraph representation
Graph representationDEEPIKA T
 
Presentation2
Presentation2Presentation2
Presentation2DEEPIKA T
 
Topological sort
Topological sortTopological sort
Topological sortDEEPIKA T
 
Path compression
Path compressionPath compression
Path compressionDEEPIKA T
 

Mais 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

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 

Último (20)

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 

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.