0% found this document useful (0 votes)
17 views23 pages

Lecture-03 DFS (Class Date 9 Feb 2019)

The document discusses depth-first search (DFS), a graph traversal algorithm. DFS explores "deeper" in the graph whenever possible by exploring edges out of the most recently discovered vertex that still has unexplored edges. When all edges of a vertex have been explored, it backtracks to the previously discovered vertex. Vertices are initially colored white, then gray when discovered, and black when finished. DFS runs in O(V+E) time, where V is the number of vertices and E is the number of edges. Pseudocode and an example are provided.

Uploaded by

asif01cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views23 pages

Lecture-03 DFS (Class Date 9 Feb 2019)

The document discusses depth-first search (DFS), a graph traversal algorithm. DFS explores "deeper" in the graph whenever possible by exploring edges out of the most recently discovered vertex that still has unexplored edges. When all edges of a vertex have been explored, it backtracks to the previously discovered vertex. Vertices are initially colored white, then gray when discovered, and black when finished. DFS runs in O(V+E) time, where V is the number of vertices and E is the number of edges. Pseudocode and an example are provided.

Uploaded by

asif01cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 3

Graph Theory
Graph Searching
Given: a graph G = (V, E), directed or undirected
Goal: methodically explore every vertex and every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree
Note: might also build a forest if graph is not connected
Graph Traversals
Ways to traverse/search a graph
Visit every vertex exactly once

Breadth-First Search
Depth-First Search
Depth-First Search
Depth-first search is another strategy for exploring a graph
Explore “deeper” in the graph whenever possible

Edges are explored out of the most recently discovered vertex v


that still has unexplored edges
When all of v’s edges have been explored, backtrack to the
vertex from which v was discovered
Depth-First Search
Vertices initially colored white

Then colored gray when discovered

Then black when finished


DFS Tree
Actually might be a DFS forest (collection of trees)
Keep track of parents
Depth-First Search
DFS (G)
1. for each vertex u ϵ G.V
DFS-VISIT(G, u)
2. u.color = WHITE
3. u.π = NIL 1. time = time + 1
4. time = 0 2. u.d = time
5. for each vertex u ϵ G.V 3. u.color = GRAY
6. if u.color == WHITE 4. for each v ϵ G.Adj[u]
7. DFS-VISIT(G, u) 5. if v.color == WHITE
6. v.π = u
7. DFS-VISIT(G, v)
8. u.color = BLACK
9. time = time + 1
10. u.f = time
DFS Example
source
vertex
E G
S
F

B C D
DFS Example
source
vertex
d f E G
S 1 | | |

F
| |
A
| | |
B C D
S
DFS Example
source
vertex
d f E G
S 1 | | |

F
2 | |
A
| | |
B C D A
S
DFS Example
source
vertex
d f E G
S 1 | | |

F
2 | |
A
3 | | | B
B C D A
S
DFS Example
source
vertex
d f E G
S 1 | | |

F
2 | |
A
3 | 4 | |
B C D A
S
DFS Example
source
vertex
d f E G
S 1 | | |

F
2 | |
A
3 | 4 5 | |
C
B C D A
S
DFS Example
source
vertex
d f E G
S 1 | | |

F
2 | |
A
3 | 4 5 | 6 |
B C D A
S
DFS Example
source
vertex
d f E G
S 1 | | |

F
2 | 7 |
A
3 | 4 5 | 6 |
B C D
S
DFS Example
source
vertex
d f E G
S 1 | 8 | |

F
2 | 7 |
A
3 | 4 5 | 6 |
B C D E
S
DFS Example
source
vertex
d f E G
S 1 | 8 | |

F
2 | 7 9 |
A
3 | 4 5 | 6 | F
B C D E
S
DFS Example
source
vertex
d f E G
S 1 | 8 | |

F
2 | 7 9 |10
A
3 | 4 5 | 6 |
B C D E
S
DFS Example
source
vertex
d f E G
S 1 | 8 |11 |

F
2 | 7 9 |10
A
3 | 4 5 | 6 |
B C D
S
DFS Example
source
vertex
d f E G
S 1 |12 8 |11 |

F
2 | 7 9 |10
A
3 | 4 5 | 6 |
B C D
Running Time of DFS
initialization takes O(V) time
second for loop in non-recursive wrapper considers each
vertex, so O(V) iterations
one recursive call is made for each vertex
in recursive call for vertex u, all its neighbors are checked;
total time in all recursive calls is O(E)
Total time is O(V+E)
DFS Exercise
F C
A
source
vertex
B D
H

G E
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy