Download presentation
Presentation is loading. Please wait.
1
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm
2
Search vs Traversal Search: Look for a given node Search: Look for a given node –stop when node found, even if not all nodes were visited Traversal: Always visit all nodes Traversal: Always visit all nodes
3
Depth-first Search Similar to Depth-first Traversal of a Binary Tree Similar to Depth-first Traversal of a Binary Tree Choose a starting vertex Choose a starting vertex Do a depth-first search on each adjacent vertex Do a depth-first search on each adjacent vertex
4
Pseudo-Code for Depth-First Search depth-first-search mark vertex as visited for each adjacent vertex if unvisited do a depth-first search on adjacent vertex
5
Depth-First Search A BC DEFG
6
A BC DEFG A A v
7
A BC DEFG A A v
8
A BC DEFG A B v v
9
Depth-First Search A BC DEFG A B v v
10
Depth-First Search A BC DEFG A B v v
11
Depth-First Search A BC DEFG v v A B D v
12
Depth-First Search A BC DEFG v v A B D v
13
Depth-First Search A BC DEFG v v A B D v
14
Depth-First Search A BC DEFG v v v A B D E v
15
Depth-First Search A BC DEFG v v v A B D E v
16
Depth-First Search A BC DEFG v v v A B D E v
17
Depth-First Search A BC DEFG v v v A B D E
18
Depth-First Search A BC DEFG v v v A B D E v
19
Depth-First Search A BC DEFG v v v A B D E v
20
Depth-First Search A BC DEFG v v v A B D E v
21
Depth-First Search A BC DEFG v v v v v A B D E F
22
Depth-First Search A BC DEFG v v v v v A B D E F
23
Depth-First Search A BC DEFG v v v v v A B D E F
24
Depth-First Search A BC DEFG v v v v v A B D E F C v
25
Depth-First Search A BC DEFG v v v v v A B D E F C v
26
Depth-First Search A BC DEFG v v v v v A B D E F C v
27
Depth-First Search A BC DEFG v v v v v A B D E F C v
28
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
29
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
30
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
31
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
32
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
33
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
34
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
35
Depth-First Search A BC DEFG v v v v v v A B D E F C G v
36
Depth-First Search A BC DEFG A B D E F C G
37
Was this a true search? Was this a true search? –How would we make it a true search? Was this a true traversal? Was this a true traversal? –How would we make it a true traversal?
38
Time and Space Complexity for Depth-First Search Time Complexity Time Complexity –Adjacency Lists Each node is marked visited once Each node is marked visited once Each node is checked for each incoming edge Each node is checked for each incoming edge O (v + e) O (v + e) –Adjacency Matrix Have to check all entries in matrix: O(n 2 ) Have to check all entries in matrix: O(n 2 )
39
Space Complexity Space Complexity –Stack to handle nodes as they are explored Worst case: all nodes put on stack (if graph is linear) Worst case: all nodes put on stack (if graph is linear) O(n) O(n) Time and Space Complexity for Depth-First Search
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.