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

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) Howdo 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) Thebasic 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 Verticesinitially 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 Discovertime 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]
  • 7.
  • 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 |128 |11 | |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 20.
    DFS Example 1 |128 |11 13| |5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 21.
    DFS Example 1 |128 |11 13| 14|5 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 22.
    DFS Example 1 |128 |11 13| 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 23.
    DFS Example 1 |128 |11 13|16 14|155 | 63 | 4 2 | 7 9 |10 source vertex d f
  • 24.
    DFS: Algorithm DFS(G)  foreach 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 ofedges 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 |128 |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_VISITis 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 SortTopologicalSort Strongly Connected ComponentStrongly Connected Component