1
COMP 250
Lecture 29
graph traversal
- Nov. 15/16, 2017
graph traversal Nov. 15/16, 2017 1 Today Recursive graph - - PowerPoint PPT Presentation
COMP 250 Lecture 29 graph traversal Nov. 15/16, 2017 1 Today Recursive graph traversal depth first Non-recursive graph traversal depth first breadth first 2 Heads up! There were a few mistakes in the slides for Sec. 001
1
2
3
4
5
6
7
8
depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }
9
depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }
10
depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }
11
depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }
12
depthFirst_Graph(v){ v.visited = true for each w such that (v,w) is in E if ! (w.visited) depthFirst_Graph(w) }
13
14
15
16
17
18
19
20
21
22
23
24
25
26
a
b c d i e f h g j k
27
28
29
30
31
32
Both ‘b’, ‘e’ are visited and enqueued before ‘b’ is dequeued.
33
34
35
1
36
1 2 3
37
1 2 4 3 5
38
1 2 4 3 5 6
39
1 2 4 3 5 7 6
40
1 2 4 3 5 7 6 8
41
1 2 4 3 5 7 6 8
42
1 2 4 3 5 7 6 8 9
43
1 2 4 3 5 7 6 8 9
44
class Graph<T> { HashMap< String, Vertex<T> > vertexMap; class Vertex<T> { ArrayList<Edge> adjList; T element; boolean visited; } class Edge { Vertex endVertex; double weight; : } }
46
47
48
[ASIDE: I did something unnecessarily complicated on the Sec.001 slides. What I have above is better. ]