SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 

Was ist angesagt? (20)

Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Graphs
GraphsGraphs
Graphs
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptx
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
PRIM’S AND KRUSKAL’S ALGORITHM
PRIM’S AND KRUSKAL’S  ALGORITHMPRIM’S AND KRUSKAL’S  ALGORITHM
PRIM’S AND KRUSKAL’S ALGORITHM
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 

Ähnlich wie Depth firstsearchalgorithm

lecture 19
lecture 19lecture 19
lecture 19
sajinsc
 
lecture 18
lecture 18lecture 18
lecture 18
sajinsc
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
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
randyburney60861
 

Ähnlich wie Depth firstsearchalgorithm (14)

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
 
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
 

Kürzlich hochgeladen

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
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
gajnagarg
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
amitlee9823
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
gajnagarg
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
amitlee9823
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
gajnagarg
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
gajnagarg
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
gajnagarg
 

Kürzlich hochgeladen (20)

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...
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Aspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraAspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - Almora
 
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
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
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
 

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