SlideShare a Scribd company logo
1 of 49
Graph Search Methods 
• A vertex u is reachable from vertex v iff there is a 
path from v to u. 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7
Graph Search Methods 
• A search method starts at a given vertex v and 
visits/labels/marks every vertex that is reachable 
from v. 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7
Graph Search Methods 
• Many graph problems solved using a search 
method. 
 Path from one vertex to another. 
 Is the graph connected? 
 Find a spanning tree. 
 Etc. 
• Commonly used search methods: 
 Breadth-first search. 
 Depth-first search.
Breadth-First Search 
• Visit start vertex and put into a FIFO queue. 
• Repeatedly remove a vertex from the queue, visit 
its unvisited adjacent vertices, put newly visited 
vertices into the queue.
Breadth-First Search Example 
2 
3 
Start search at vertex 1. 
8 
10 
1 
4 
5 9 
11 
6 
7
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
FIFO Queue 
1 
Visit/mark/label start vertex and put in a FIFO queue.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
FIFO Queue 
1 
Remove 1 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
FIFO Queue 
2 
4 
Remove 1 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
FIFO Queue 
2 
4 
Remove 2 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
4 
5 
3 
6 
Remove 2 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
4 
5 
3 
6 
Remove 4 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
5 
3 
6 
Remove 4 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
5 
3 
6 
Remove 5 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
3 
6 
9 
7 
Remove 5 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
3 
6 
9 
7 
Remove 3 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
6 
9 
7 
Remove 3 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
6 
9 
7 
Remove 6 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
9 
7 
Remove 6 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
9 
7 
Remove 9 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
7 8 
Remove 9 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
7 8 
Remove 7 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
8 
Remove 7 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
FIFO Queue 
8 
Remove 8 from Q; visit adjacent unvisited vertices; 
put in Q.
Breadth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
Queue is empty. Search terminates. 
FIFO Queue
Breadth-First Search Property 
• All vertices reachable from the start vertex 
(including the start vertex) are visited.
Time Complexity 
• Each visited vertex is put on (and so 
removed from) the queue exactly once. 
• When a vertex is removed from the queue, 
we examine its adjacent vertices. 
 O(n) if adjacency matrix used 
 O(vertex degree) if adjacency lists used 
• Total time 
 O(mn), where m is number of vertices in the 
component that is searched (adjacency matrix)
Time Complexity 
 O(n + sum of component vertex degrees) (adj. 
lists) 
= O(n + number of edges in component)
Path From Vertex v To Vertex u 
• Start a breadth-first search at vertex v. 
• Terminate when vertex u is visited or when 
Q becomes empty (whichever occurs first). 
• Time 
 O(n2) when adjacency matrix used 
 O(n+e) when adjacency lists used (e is number 
of edges)
Is The Graph Connected? 
• Start a breadth-first search at any vertex of 
the graph. 
• Graph is connected iff all n vertices get 
visited. 
• Time 
 O(n2) when adjacency matrix used 
 O(n+e) when adjacency lists used (e is number 
of edges)
Connected Components 
• Start a breadth-first search at any as yet 
unvisited vertex of the graph. 
• Newly visited vertices (plus edges between 
them) define a component. 
• Repeat until all vertices are visited.
Connected Components 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7
Time Complexity 
O(n2) when adjacency matrix used 
O(n+e) when adjacency lists used (e 
is number of edges)
Spanning Tree 
2 
3 
Breadth-first search from vertex 1. 
8 
1 
4 
5 9 
6 
7 
Breadth-first spanning tree.
Spanning Tree 
• Start a breadth-first search at any vertex of 
the graph. 
• If graph is connected, the n-1 edges used to 
get to unvisited vertices define a spanning 
tree (breadth-first spanning tree). 
• Time 
 O(n2) when adjacency matrix used 
 O(n+e) when adjacency lists used (e is number 
of edges)
Depth-First Search 
depthFirstSearch(v) 
{ 
Label vertex v as reached. 
for (each unreached vertex u 
adjacenct from v) 
depthFirstSearch(u); 
}
Depth-First Search Example 
1 
2 
Start search at vertex 1. 
3 
8 
10 
4 
5 9 
11 
6 
7 
Label vertex 1 and do a depth first search 
from either 2 or 4. 
Suppose that vertex 2 is selected.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 
9 
11 
6 
7 
Label vertex 2 and do a depth first search 
from either 3, 5, or 6. 
Suppose that vertex 5 is selected.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label vertex 5 and do a depth first search 
from either 3, 7, or 9. 
Suppose that vertex 9 is selected.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label vertex 9 and do a depth first search 
from either 6 or 8. 
Suppose that vertex 8 is selected.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label vertex 8 and return to vertex 9. 
From vertex 9 do a dfs(6).
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label vertex 6 and do a depth first search from 
either 4 or 7. 
Suppose that vertex 4 is selected.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label vertex 4 and return to 6. 
From vertex 6 do a dfs(7).
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label vertex 7 and return to 6. 
Return to 9.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Return to 5.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Do a dfs(3).
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Label 3 and return to 5. 
Return to 2.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Return to 1.
Depth-First Search Example 
2 
3 
8 
10 
1 
4 
5 9 
11 
6 
7 
Return to invoking method.
Depth-First Search Properties 
• Same complexity as BFS. 
• Same properties with respect to path 
finding, connected components, and 
spanning trees. 
• Edges used to reach unlabeled vertices 
define a depth-first spanning tree when the 
graph is connected. 
• There are problems for which bfs is better 
than dfs and vice versa.

More Related Content

What's hot

Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 QueenHa Ninh
 
Ram minimum spanning tree
Ram   minimum spanning treeRam   minimum spanning tree
Ram minimum spanning treeRama Prasath A
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of adaSahil Kumar
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructuresKrish_ver2
 
Converting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesConverting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesStrand Life Sciences Pvt Ltd
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
Higher-order organization of complex networks
Higher-order organization of complex networksHigher-order organization of complex networks
Higher-order organization of complex networksDavid Gleich
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
Common fixed point theorems with continuously subcompatible mappings in fuzz...
 Common fixed point theorems with continuously subcompatible mappings in fuzz... Common fixed point theorems with continuously subcompatible mappings in fuzz...
Common fixed point theorems with continuously subcompatible mappings in fuzz...Alexander Decker
 
Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3Amin khalil
 

What's hot (20)

Queue- 8 Queen
Queue- 8 QueenQueue- 8 Queen
Queue- 8 Queen
 
Ram minimum spanning tree
Ram   minimum spanning treeRam   minimum spanning tree
Ram minimum spanning tree
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 
backtracking algorithms of ada
backtracking algorithms of adabacktracking algorithms of ada
backtracking algorithms of ada
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
5.4 randomized datastructures
5.4 randomized datastructures5.4 randomized datastructures
5.4 randomized datastructures
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
Converting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional OnesConverting High Dimensional Problems to Low Dimensional Ones
Converting High Dimensional Problems to Low Dimensional Ones
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
Higher-order organization of complex networks
Higher-order organization of complex networksHigher-order organization of complex networks
Higher-order organization of complex networks
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Backtracking
BacktrackingBacktracking
Backtracking
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Common fixed point theorems with continuously subcompatible mappings in fuzz...
 Common fixed point theorems with continuously subcompatible mappings in fuzz... Common fixed point theorems with continuously subcompatible mappings in fuzz...
Common fixed point theorems with continuously subcompatible mappings in fuzz...
 
Seismic data processing lecture 3
Seismic data processing lecture 3Seismic data processing lecture 3
Seismic data processing lecture 3
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 

Similar to Lec29

Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxRatnakar Mikkili
 
Depth first Search.pptx
Depth first Search.pptxDepth first Search.pptx
Depth first Search.pptxAleezaShakeel3
 
Branch and bound
Branch and boundBranch and bound
Branch and boundAcad
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms masterHossam Hassan
 
9.1 square roots day 1
9.1  square roots day 19.1  square roots day 1
9.1 square roots day 1bweldon
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxPankaj Debbarma
 
Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptxMuhammadShafi89
 
Study guide for area polygons 2011
Study guide for area polygons 2011Study guide for area polygons 2011
Study guide for area polygons 2011Christopher Polizzi
 
Lesson 1.10 square roots
Lesson 1.10   square rootsLesson 1.10   square roots
Lesson 1.10 square rootsJohnnyBallecer
 
Nov 20 Determining Equations Of Lines
Nov 20 Determining Equations Of LinesNov 20 Determining Equations Of Lines
Nov 20 Determining Equations Of LinesDMCI
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Determining Equations Of Lines Nov 20
Determining Equations Of Lines Nov 20Determining Equations Of Lines Nov 20
Determining Equations Of Lines Nov 20DMCI
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graphgetacew
 

Similar to Lec29 (16)

Artificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptxArtificial Intelligence_Searching.pptx
Artificial Intelligence_Searching.pptx
 
Depth first Search.pptx
Depth first Search.pptxDepth first Search.pptx
Depth first Search.pptx
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Search algorithms master
Search algorithms masterSearch algorithms master
Search algorithms master
 
9.1 square roots day 1
9.1  square roots day 19.1  square roots day 1
9.1 square roots day 1
 
AI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptxAI-04 Production System - Search Problem.pptx
AI-04 Production System - Search Problem.pptx
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
 
Graph bfs
Graph bfsGraph bfs
Graph bfs
 
Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptx
 
Study guide for area polygons 2011
Study guide for area polygons 2011Study guide for area polygons 2011
Study guide for area polygons 2011
 
Lesson 1.10 square roots
Lesson 1.10   square rootsLesson 1.10   square roots
Lesson 1.10 square roots
 
Nov 20 Determining Equations Of Lines
Nov 20 Determining Equations Of LinesNov 20 Determining Equations Of Lines
Nov 20 Determining Equations Of Lines
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Determining Equations Of Lines Nov 20
Determining Equations Of Lines Nov 20Determining Equations Of Lines Nov 20
Determining Equations Of Lines Nov 20
 
Algorithms of graph
Algorithms of graphAlgorithms of graph
Algorithms of graph
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec30
Lec30Lec30
Lec30
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 
Lec18
Lec18Lec18
Lec18
 
Lec17
Lec17Lec17
Lec17
 
Lec16
Lec16Lec16
Lec16
 
Lec15
Lec15Lec15
Lec15
 
Lec14
Lec14Lec14
Lec14
 

Lec29

  • 1. Graph Search Methods • A vertex u is reachable from vertex v iff there is a path from v to u. 2 3 8 10 1 4 5 9 11 6 7
  • 2. Graph Search Methods • A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v. 2 3 8 10 1 4 5 9 11 6 7
  • 3. Graph Search Methods • Many graph problems solved using a search method.  Path from one vertex to another.  Is the graph connected?  Find a spanning tree.  Etc. • Commonly used search methods:  Breadth-first search.  Depth-first search.
  • 4. Breadth-First Search • Visit start vertex and put into a FIFO queue. • Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue.
  • 5. Breadth-First Search Example 2 3 Start search at vertex 1. 8 10 1 4 5 9 11 6 7
  • 6. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 1 Visit/mark/label start vertex and put in a FIFO queue.
  • 7. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 1 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.
  • 8. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 2 4 Remove 1 from Q; visit adjacent unvisited vertices; put in Q.
  • 9. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 2 4 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.
  • 10. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 4 5 3 6 Remove 2 from Q; visit adjacent unvisited vertices; put in Q.
  • 11. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 4 5 3 6 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.
  • 12. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 5 3 6 Remove 4 from Q; visit adjacent unvisited vertices; put in Q.
  • 13. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 5 3 6 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.
  • 14. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 3 6 9 7 Remove 5 from Q; visit adjacent unvisited vertices; put in Q.
  • 15. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 3 6 9 7 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.
  • 16. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 6 9 7 Remove 3 from Q; visit adjacent unvisited vertices; put in Q.
  • 17. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 6 9 7 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.
  • 18. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 9 7 Remove 6 from Q; visit adjacent unvisited vertices; put in Q.
  • 19. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 9 7 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.
  • 20. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 7 8 Remove 9 from Q; visit adjacent unvisited vertices; put in Q.
  • 21. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 7 8 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.
  • 22. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 8 Remove 7 from Q; visit adjacent unvisited vertices; put in Q.
  • 23. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 FIFO Queue 8 Remove 8 from Q; visit adjacent unvisited vertices; put in Q.
  • 24. Breadth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Queue is empty. Search terminates. FIFO Queue
  • 25. Breadth-First Search Property • All vertices reachable from the start vertex (including the start vertex) are visited.
  • 26. Time Complexity • Each visited vertex is put on (and so removed from) the queue exactly once. • When a vertex is removed from the queue, we examine its adjacent vertices.  O(n) if adjacency matrix used  O(vertex degree) if adjacency lists used • Total time  O(mn), where m is number of vertices in the component that is searched (adjacency matrix)
  • 27. Time Complexity  O(n + sum of component vertex degrees) (adj. lists) = O(n + number of edges in component)
  • 28. Path From Vertex v To Vertex u • Start a breadth-first search at vertex v. • Terminate when vertex u is visited or when Q becomes empty (whichever occurs first). • Time  O(n2) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)
  • 29. Is The Graph Connected? • Start a breadth-first search at any vertex of the graph. • Graph is connected iff all n vertices get visited. • Time  O(n2) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)
  • 30. Connected Components • Start a breadth-first search at any as yet unvisited vertex of the graph. • Newly visited vertices (plus edges between them) define a component. • Repeat until all vertices are visited.
  • 31. Connected Components 2 3 8 10 1 4 5 9 11 6 7
  • 32. Time Complexity O(n2) when adjacency matrix used O(n+e) when adjacency lists used (e is number of edges)
  • 33. Spanning Tree 2 3 Breadth-first search from vertex 1. 8 1 4 5 9 6 7 Breadth-first spanning tree.
  • 34. Spanning Tree • Start a breadth-first search at any vertex of the graph. • If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (breadth-first spanning tree). • Time  O(n2) when adjacency matrix used  O(n+e) when adjacency lists used (e is number of edges)
  • 35. Depth-First Search depthFirstSearch(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) depthFirstSearch(u); }
  • 36. Depth-First Search Example 1 2 Start search at vertex 1. 3 8 10 4 5 9 11 6 7 Label vertex 1 and do a depth first search from either 2 or 4. Suppose that vertex 2 is selected.
  • 37. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 2 and do a depth first search from either 3, 5, or 6. Suppose that vertex 5 is selected.
  • 38. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 5 and do a depth first search from either 3, 7, or 9. Suppose that vertex 9 is selected.
  • 39. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 9 and do a depth first search from either 6 or 8. Suppose that vertex 8 is selected.
  • 40. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 8 and return to vertex 9. From vertex 9 do a dfs(6).
  • 41. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 6 and do a depth first search from either 4 or 7. Suppose that vertex 4 is selected.
  • 42. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 4 and return to 6. From vertex 6 do a dfs(7).
  • 43. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 7 and return to 6. Return to 9.
  • 44. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Return to 5.
  • 45. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Do a dfs(3).
  • 46. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label 3 and return to 5. Return to 2.
  • 47. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Return to 1.
  • 48. Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Return to invoking method.
  • 49. Depth-First Search Properties • Same complexity as BFS. • Same properties with respect to path finding, connected components, and spanning trees. • Edges used to reach unlabeled vertices define a depth-first spanning tree when the graph is connected. • There are problems for which bfs is better than dfs and vice versa.

Editor's Notes

  1. Visit, mark, and label used as synonyms.
  2. Keep track of edges used to rreach new vertices. These edges form a spanning tree if the graph is connected.
  3. Note that vertices adjacent from v are examined one at a time. As soon as an unreached adjacent vertex u is found, a depthFirstSearch(u) is done. Remaining vertices adjacent from v are examined after depthFirstSearch(u) completes.