SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PPTX
Slides Chapter10.1 10.2
showslidedump
 
PDF
introduction to graph theory
Chuckie Balbuena
 
PPT
Graph traversal-BFS & DFS
Rajandeep Gill
 
PPTX
Depth-First Search
Dakshitha Dissanayaka
 
PPT
Graphs in Data Structure
hafsa komal
 
PPT
17. Trees and Graphs
Intro C# Book
 
PPTX
Depth first search and breadth first searching
Kawsar Hamid Sumon
 
PPTX
Discrete Mathematics Presentation
Salman Elahi
 
PPTX
MATCHING GRAPH THEORY
garishma bhatia
 
PPTX
Dijkstra's algorithm presentation
Subid Biswas
 
PPTX
DFS & BFS Graph
Mahfuzul Yamin
 
PPTX
Depth-First Search
Md. Shafiuzzaman Hira
 
PPT
Bellman Ford's Algorithm
Tanmay Baranwal
 
PPTX
bfs and dfs (data structures).pptx
ssuser55cbdb
 
PPT
Application of dfs
Hossain Md Shakhawat
 
PPT
Graphs bfs dfs
Jaya Gautam
 
PPT
Graph colouring
Priyank Jain
 
PPTX
Dijkstra's algorithm
gsp1294
 
PPTX
Networks dijkstra's algorithm- pgsr
Linawati Adiman
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
Slides Chapter10.1 10.2
showslidedump
 
introduction to graph theory
Chuckie Balbuena
 
Graph traversal-BFS & DFS
Rajandeep Gill
 
Depth-First Search
Dakshitha Dissanayaka
 
Graphs in Data Structure
hafsa komal
 
17. Trees and Graphs
Intro C# Book
 
Depth first search and breadth first searching
Kawsar Hamid Sumon
 
Discrete Mathematics Presentation
Salman Elahi
 
MATCHING GRAPH THEORY
garishma bhatia
 
Dijkstra's algorithm presentation
Subid Biswas
 
DFS & BFS Graph
Mahfuzul Yamin
 
Depth-First Search
Md. Shafiuzzaman Hira
 
Bellman Ford's Algorithm
Tanmay Baranwal
 
bfs and dfs (data structures).pptx
ssuser55cbdb
 
Application of dfs
Hossain Md Shakhawat
 
Graphs bfs dfs
Jaya Gautam
 
Graph colouring
Priyank Jain
 
Dijkstra's algorithm
gsp1294
 
Networks dijkstra's algorithm- pgsr
Linawati Adiman
 
Graph traversals in Data Structures
Anandhasilambarasan D
 

Similar to Depth firstsearchalgorithm (16)

PPT
lecture 19
sajinsc
 
PPT
lecture 18
sajinsc
 
PPTX
Lecture 5 - Graph Algorithms BFS and DFS.pptx
mtahanasir65
 
PPTX
DFS algorithm
Amit Kundu
 
PDF
DFS ppt.pdf
Rajkk5
 
PPTX
Depth first search [dfs]
DEEPIKA T
 
PPTX
kumattt).pptx
gurukhade1
 
PDF
Analysis and design of algorithms part 3
Deepak John
 
PDF
Bfs dfs
Praveen Yadav
 
PPT
Chapter 23 aoa
Hanif Durad
 
PPT
lecture24.ppt
PallaviDhade1
 
PPT
B.tech admission in india
Edhole.com
 
PPTX
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
PPT
Elementary Graph Algo.ppt
SazidHossain9
 
DOCX
Data Structures and Algorithm Analysis1. How much memory w.docx
randyburney60861
 
PPT
19-graph1 (1).ppt
Himajanaidu2
 
lecture 19
sajinsc
 
lecture 18
sajinsc
 
Lecture 5 - Graph Algorithms BFS and DFS.pptx
mtahanasir65
 
DFS algorithm
Amit Kundu
 
DFS ppt.pdf
Rajkk5
 
Depth first search [dfs]
DEEPIKA T
 
kumattt).pptx
gurukhade1
 
Analysis and design of algorithms part 3
Deepak John
 
Bfs dfs
Praveen Yadav
 
Chapter 23 aoa
Hanif Durad
 
lecture24.ppt
PallaviDhade1
 
B.tech admission in india
Edhole.com
 
Algorithm Design and Complexity - Course 7
Traian Rebedea
 
Elementary Graph Algo.ppt
SazidHossain9
 
Data Structures and Algorithm Analysis1. How much memory w.docx
randyburney60861
 
19-graph1 (1).ppt
Himajanaidu2
 
Ad

Recently uploaded (20)

PDF
624753984-Annex-A3-RPMS-Tool-for-Proficient-Teachers-SY-2024-2025.pdf
CristineGraceAcuyan
 
PPTX
Data Analytics using sparkabcdefghi.pptx
KarkuzhaliS3
 
PPTX
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
PDF
Exploiting the Low Volatility Anomaly: A Low Beta Model Portfolio for Risk-Ad...
Bradley Norbom, CFA
 
PPT
Reliability Monitoring of Aircrfat commerce
Rizk2
 
PPTX
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
Taqyea
 
DOCX
Artigo - Playing to Win.planejamento docx
KellyXavier15
 
PDF
SaleServicereport and SaleServicereport
2251330007
 
PPTX
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
PDF
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...
Mahmoud Shoush
 
PPTX
Mynd company all details what they are doing a
AniketKadam40952
 
PPTX
Presentation by Tariq & Mohammed (1).pptx
AbooddSandoqaa
 
PDF
Kafka Use Cases Real-World Applications
Accentfuture
 
PPTX
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
PDF
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
PDF
Microsoft Power BI - Advanced Certificate for Business Intelligence using Pow...
Prasenjit Debnath
 
DOCX
COT Feb 19, 2025 DLLgvbbnnjjjjjj_Digestive System and its Functions_PISA_CBA....
kayemorales1105
 
PDF
CT-2-Ancient ancient accept-Criticism.pdf
DepartmentofEnglishC1
 
PDF
Digital-Transformation-for-Federal-Agencies.pdf.pdf
One Federal Solution
 
PPTX
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
624753984-Annex-A3-RPMS-Tool-for-Proficient-Teachers-SY-2024-2025.pdf
CristineGraceAcuyan
 
Data Analytics using sparkabcdefghi.pptx
KarkuzhaliS3
 
RESEARCH-FINAL-GROUP-3, about the final .pptx
gwapokoha1
 
Exploiting the Low Volatility Anomaly: A Low Beta Model Portfolio for Risk-Ad...
Bradley Norbom, CFA
 
Reliability Monitoring of Aircrfat commerce
Rizk2
 
美国毕业证范本中华盛顿大学学位证书CWU学生卡购买
Taqyea
 
Artigo - Playing to Win.planejamento docx
KellyXavier15
 
SaleServicereport and SaleServicereport
2251330007
 
25 items quiz for practical research 1 in grade 11
leamaydayaganon81
 
Prescriptive Process Monitoring Under Uncertainty and Resource Constraints: A...
Mahmoud Shoush
 
Mynd company all details what they are doing a
AniketKadam40952
 
Presentation by Tariq & Mohammed (1).pptx
AbooddSandoqaa
 
Kafka Use Cases Real-World Applications
Accentfuture
 
Communication_Skills_Class10_Visual.pptx
namanrastogi70555
 
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
Microsoft Power BI - Advanced Certificate for Business Intelligence using Pow...
Prasenjit Debnath
 
COT Feb 19, 2025 DLLgvbbnnjjjjjj_Digestive System and its Functions_PISA_CBA....
kayemorales1105
 
CT-2-Ancient ancient accept-Criticism.pdf
DepartmentofEnglishC1
 
Digital-Transformation-for-Federal-Agencies.pdf.pdf
One Federal Solution
 
@Reset-Password.pptx presentakh;kenvtion
MarkLariosa1
 
Ad

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