SlideShare une entreprise Scribd logo
1  sur  29
DFS
Depth First Search (DFS)Depth First Search (DFS)
Topics
Group Memberz
Hansa Khalique (28)Hansa Khalique (28)
Tayyaba Anwer (26)Tayyaba Anwer (26)
Saira Mahboob (48)Saira Mahboob (48)
Shanza Anwer (29)Shanza Anwer (29)
Shantul Khan (18)Shantul Khan (18)
Asma urooj (08)Asma urooj (08)
Saira Moheed (39)Saira Moheed (39)
Sundus jalil (11)Sundus jalil (11)
Khanzadi Nayar Saba (42)Khanzadi Nayar Saba (42)
Graph Search (traversal)
How do we search a graph?How do we search a graph?
– At a particular vertices, where shall we go next?At a particular vertices, where shall we go next?
Two common framework:Two common framework:
 the depth-first search (DFS)the depth-first search (DFS)
 the breadth-first search (BFS)the breadth-first search (BFS)
 We are use DFSWe are use DFS
In DFS:In DFS:
– In DFS, go as far as possible along a single pathIn DFS, go as far as possible along a single path
until reach a dead end (a vertex with no edge outuntil reach a dead end (a vertex with no edge out
or no neighbor unexplored) then backtrackor no neighbor unexplored) then backtrack
Depth-First Search (DFS)
The basic idea behind this algorithm is that it traversesThe basic idea behind this algorithm is that it traverses
the graph using recursionthe graph using recursion
– Go as far as possible until you reach a deadendGo as far as possible until you reach a deadend
– Backtrack to the previous path and try the next branchBacktrack to the previous path and try the next branch
– The graph below, started at node a, would be visitedThe graph below, started at node a, would be visited
in the following order: a, b, c, g, h, i, e, d, f, jin the following order: a, b, c, g, h, i, e, d, f, j
da b
h i
c
g
e
j
f
DFS: Color Scheme
Vertices initially colored whiteVertices initially colored white
Then colored gray when discoveredThen colored gray when discovered
Then black when finishedThen black when finished
DFS: Time Stamps
Discover time d[u]: when u is firstDiscover time d[u]: when u is first
discovereddiscovered
Finish time f[u]: when backtrack fromFinish time f[u]: when backtrack from
uu
d[u] < f[u]d[u] < f[u]
DFS Example
source
vertex
DFS Example
1 | | |
|||
| |
source
vertex
d f
DFS Example
1 | | |
|||
2 | |
source
vertex
d f
DFS Example
1 | | |
||3 |
2 | |
source
vertex
d f
DFS Example
1 | | |
||3 | 4
2 | |
source
vertex
d f
DFS Example
1 | | |
|5 |3 | 4
2 | |
source
vertex
d f
DFS Example
1 | | |
|5 | 63 | 4
2 | |
source
vertex
d f
DFS Example
1 | | |
|5 | 63 | 4
2 | 7 |
source
vertex
d f
DFS Example
1 | 8 | |
|5 | 63 | 4
2 | 7 |
source
vertex
d f
DFS Example
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |
source
vertex
d f
DFS Example
1 | 8 | |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS Example
1 | 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS Example
1 |12 8 |11 |
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS Example
1 |12 8 |11 13|
|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS Example
1 |12 8 |11 13|
14|5 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS Example
1 |12 8 |11 13|
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex
d f
DFS: Algorithm
DFS(G)
 for each vertex u in V,
 color[u]=white; π[u]=NIL
 time=0;
 for each vertex u in V
 if (color[u]=white)
 DFS-VISIT(u)
DFS-VISIT(u)
 color[u]=gray;
 time = time + 1;
 d[u] = time;
 for each v in Adj(u) do
 if (color[v] = white)
 π[v] = u;
 DFS-VISIT(v);
 color[u] = black;
 time = time + 1; f[u]= time;
DFS: Algorithm (Cont.)
source
vertex
DFS: Kinds of edges
DFS introduces an important distinctionDFS introduces an important distinction
among edges in the original graph:among edges in the original graph:
– Tree edgeTree edge: encounter new (white) vertex: encounter new (white) vertex
– Back edgeBack edge: from descendent to ancestor: from descendent to ancestor
– Forward edgeForward edge: from ancestor to descendent: from ancestor to descendent
– Cross edgeCross edge: between a tree or subtrees: between a tree or subtrees
Note: tree & back edges are important;Note: tree & back edges are important;
most algorithms don’t distinguish forwardmost algorithms don’t distinguish forward
& cross& cross
DFS Example
1 |12 8 |11 13|16
14|155 | 63 | 4
2 | 7 9 |10
source
vertex d f
Tree edges Back edges Forward edges Cross edges
DFS: Complexity Analysis
DFS_VISIT is called exactly once for each vertex
And DFS_VISIT scans all the edges which causes cost of
O(E)
Initialization complexity is O(V)
Overall complexity is O(V + E)
DFS: Application
Topological SortTopological Sort
Strongly Connected ComponentStrongly Connected Component

Contenu connexe

Tendances

Tendances (20)

Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Depth first search and breadth first searching
Depth first search and breadth first searchingDepth first search and breadth first searching
Depth first search and breadth first searching
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
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)
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Graph representation
Graph representationGraph representation
Graph representation
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
Red black tree
Red black treeRed black tree
Red black tree
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Bfs new
Bfs newBfs new
Bfs new
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 

Similaire à Depth firstsearchalgorithm

lecture 19
lecture 19lecture 19
lecture 19sajinsc
 
lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
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
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Traian Rebedea
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxrandyburney60861
 

Similaire à Depth firstsearchalgorithm (15)

lecture 19
lecture 19lecture 19
lecture 19
 
lecture 18
lecture 18lecture 18
lecture 18
 
DFS algorithm
DFS algorithmDFS algorithm
DFS algorithm
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
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
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
lecture24.ppt
lecture24.pptlecture24.ppt
lecture24.ppt
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 

Dernier

Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...amitlee9823
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAroojKhan71
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Researchmichael115558
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxolyaivanovalion
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...shambhavirathore45
 

Dernier (20)

Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 

Depth firstsearchalgorithm

  • 1. DFS Depth First Search (DFS)Depth First Search (DFS) Topics
  • 2. Group Memberz Hansa Khalique (28)Hansa Khalique (28) Tayyaba Anwer (26)Tayyaba Anwer (26) Saira Mahboob (48)Saira Mahboob (48) Shanza Anwer (29)Shanza Anwer (29) Shantul Khan (18)Shantul Khan (18) Asma urooj (08)Asma urooj (08) Saira Moheed (39)Saira Moheed (39) Sundus jalil (11)Sundus jalil (11) Khanzadi Nayar Saba (42)Khanzadi Nayar Saba (42)
  • 3. Graph Search (traversal) How do we search a graph?How do we search a graph? – At a particular vertices, where shall we go next?At a particular vertices, where shall we go next? Two common framework:Two common framework:  the depth-first search (DFS)the depth-first search (DFS)  the breadth-first search (BFS)the breadth-first search (BFS)  We are use DFSWe are use DFS In DFS:In DFS: – In DFS, go as far as possible along a single pathIn DFS, go as far as possible along a single path until reach a dead end (a vertex with no edge outuntil reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrackor no neighbor unexplored) then backtrack
  • 4. Depth-First Search (DFS) The basic idea behind this algorithm is that it traversesThe basic idea behind this algorithm is that it traverses the graph using recursionthe graph using recursion – Go as far as possible until you reach a deadendGo as far as possible until you reach a deadend – Backtrack to the previous path and try the next branchBacktrack to the previous path and try the next branch – The graph below, started at node a, would be visitedThe graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, jin the following order: a, b, c, g, h, i, e, d, f, j da b h i c g e j f
  • 5. DFS: Color Scheme Vertices initially colored whiteVertices initially colored white Then colored gray when discoveredThen colored gray when discovered Then black when finishedThen black when finished
  • 6. DFS: Time Stamps Discover time d[u]: when u is firstDiscover time d[u]: when u is first discovereddiscovered Finish time f[u]: when backtrack fromFinish time f[u]: when backtrack from uu d[u] < f[u]d[u] < f[u]
  • 8. DFS Example 1 | | | ||| | | source vertex d f
  • 9. DFS Example 1 | | | ||| 2 | | source vertex d f
  • 10. DFS Example 1 | | | ||3 | 2 | | source vertex d f
  • 11. DFS Example 1 | | | ||3 | 4 2 | | source vertex d f
  • 12. DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f
  • 13. DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f
  • 14. DFS Example 1 | | | |5 | 63 | 4 2 | 7 | source vertex d f
  • 15. DFS Example 1 | 8 | | |5 | 63 | 4 2 | 7 | source vertex d f
  • 16. DFS Example 1 | 8 | | |5 | 63 | 4 2 | 7 9 | source vertex d f
  • 17. DFS Example 1 | 8 | | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 18. DFS Example 1 | 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 19. DFS Example 1 |12 8 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 20. DFS Example 1 |12 8 |11 13| |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 21. DFS Example 1 |12 8 |11 13| 14|5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 22. DFS Example 1 |12 8 |11 13| 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 23. DFS Example 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 24. DFS: Algorithm DFS(G)  for each vertex u in V,  color[u]=white; π[u]=NIL  time=0;  for each vertex u in V  if (color[u]=white)  DFS-VISIT(u)
  • 25. DFS-VISIT(u)  color[u]=gray;  time = time + 1;  d[u] = time;  for each v in Adj(u) do  if (color[v] = white)  π[v] = u;  DFS-VISIT(v);  color[u] = black;  time = time + 1; f[u]= time; DFS: Algorithm (Cont.) source vertex
  • 26. DFS: Kinds of edges DFS introduces an important distinctionDFS introduces an important distinction among edges in the original graph:among edges in the original graph: – Tree edgeTree edge: encounter new (white) vertex: encounter new (white) vertex – Back edgeBack edge: from descendent to ancestor: from descendent to ancestor – Forward edgeForward edge: from ancestor to descendent: from ancestor to descendent – Cross edgeCross edge: between a tree or subtrees: between a tree or subtrees Note: tree & back edges are important;Note: tree & back edges are important; most algorithms don’t distinguish forwardmost algorithms don’t distinguish forward & cross& cross
  • 27. DFS Example 1 |12 8 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f Tree edges Back edges Forward edges Cross edges
  • 28. DFS: Complexity Analysis DFS_VISIT is called exactly once for each vertex And DFS_VISIT scans all the edges which causes cost of O(E) Initialization complexity is O(V) Overall complexity is O(V + E)
  • 29. DFS: Application Topological SortTopological Sort Strongly Connected ComponentStrongly Connected Component