Graph Traversals - PowerPoint PPT Presentation

About This Presentation
Title:

Graph Traversals

Description:

I will be out of town until Thursday, 15th. Read Chapter 14. Project 5 Due 16th. Traversing graphs. Depth-First Search. like a traversal of a tree ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 57
Provided by: thaddeusf
Category:

less

Transcript and Presenter's Notes

Title: Graph Traversals


1
Graph Traversals
  • CSC 172
  • SPRING 2004
  • LECTURE 21

2
Announcements
  • Project 3 is graded
  • handed back Tuesday
  • Grad spam, tonight if you are really anxious
    see me after class
  • I will be out of town until Thursday, 15th
  • Read Chapter 14
  • Project 5 Due 16th

3
Traversing graphs
  • Depth-First Search
  • like a traversal of a tree
  • Breath-First Search
  • Less like tree traversal
  • Use a stack

4
Exploring a Maze
  • A depth-first search (DFS) in an undirected graph
    G is like wandering in a maze with a string and a
    can of paint you can prevent yourself from
    getting lost.

5
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
6
DFS
  • Start at vertex s
  • Tie the end of the string to s and mark visited
    on s
  • Make s the current vertex u
  • Travel along an arbitrary edge (u,v)
  • unrolling string
  • If edge(u,v) leads to an already visited vertex v
  • then return to u
  • else mark v as visited, set v as current
    u, repeat _at_ step 2
  • When all edges lead to visited verticies,
    backtrack to previous vertex (roll up string) and
    repeat _at_ step 2
  • When we backtrack to s and explore all its edges
    we are done

7
DFS Pseudocode (labels edges)
  • DFS( Vertex v)
  • for each edge incident on v do
  • if edge e is unexplored then
  • let w be the other endpoint of e
  • if vertex w is unexplored then
  • label e as a discovery edge
  • recursively call DFS(w)
  • else
  • label e as a backedge

8
EXAMPLE
9
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
10
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
11
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
12
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
13
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
14
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
15
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
16
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
17
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
18
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
19
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
20
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
21
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
22
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
23
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
24
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
25
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
26
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
27
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
28
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
29
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
30
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
31
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
32
DFS Tree
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
33
DFS Properties
  • Starting at s
  • The traversal visits al the vertices in the
    connected component of s
  • The discovery edges form a spanning tree of the
    connected component of s

34
DFS Runtime
  • DFS is called on each vertex exactly once
  • Every edge is examined exactly twice (once from
    each of its vertices)
  • So, for ns vertices and ms edges in the connected
    component of the vertex s, the DFS runs in
    O(nsms) if
  • The graph data structure methods take constant
    time
  • Marking takes constant time
  • There is a systematic way to examine edges
    (avoiding redundancy)

35
Marking Verticies
  • Extend vertex structure to support variable for
    marking
  • Use a hash table mechanism to log marked vertices

36
Breadth-First Search
  • Starting vertex has level 0 (anchor vertex)
  • Visit (mark) all vertices that are only one edge
    away
  • mark each vertex with its level
  • One edge away from level 0 is level 1
  • One edge away from level 1 is level 2
  • Etc. . . .

37
EXAMPLE
38
Example
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
39
Example
0
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
40
Example
0
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
41
Example
0
1
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
42
Example
0
1
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
43
Example
2
0
1
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
44
Example
2
0
1
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
45
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
46
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
N
O
P
M
47
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
N
O
P
M
48
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
N
O
P
M
49
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
N
O
P
M
50
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
N
O
P
M
51
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
5
N
O
P
M
52
Example
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
5
N
O
P
M
53
BFS Tree
2
3
0
1
B
C
D
A
F
G
H
E
J
K
L
I
4
5
N
O
P
M
54
BFS Pseudocode
  • BSF(Vertex s)
  • initialize container L0 to contain vertex s
  • i ? 0
  • while Li is not empty do
  • create container Li1 to initially be empty
  • for each vertex v in Li do
  • if edge e incident on v do
  • let w be the other endpoint of e
  • if w is unexplored then
  • label e as a discovery edge
  • insert w into Li1
  • else
  • label e as a cross edge
  • i ? i1

55
BSF Properties
  • The traversal visits all vertices in the
    connected component of s
  • The discover edges form a spanning tree of the cc
  • For each vertex v at level I, the path of the BSF
    tree T between s and v has I edges and any other
    path of G between s and v has at least I edges
  • If (u,v) is an edge that is not in the BSF tree,
    then the level number of u and v differ by at
    most one

56
Run Time
  • A BSF traversal takes O(nm) time
  • Also, there exist O(nm) time algorithms base on
    BFS which test for
  • Connectivity of graph
  • Spanning tree of G
  • Connected component
  • Minimum number of edges path between s and v
Write a Comment
User Comments (0)
About PowerShow.com