1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs
2 Graph Traversal
3 Slide adapted from Goodrich & Tamassia
4 Exploring a Labyrinth Without Getting Lost A depth-first search in an undirected graph G is like wandering in a labyrinth with a string and a can of red paint without getting lost. –Start at vertex s –Tie the string to s –Paint s red (visited) –Set u to be s (the current vertex) –Travel on an arbitrary edge (u,v), trailing string behind –If v is already visited, follow the string back to u –Else if v is unvisited, paint v red –Set u (current vertex) to v and repeat –Keep visiting and unrolling (backtracking) until we finally arrive back at s
5 Slide adapted from Goodrich & Tamassia DFS Starting at vertex A Discovery edges solid Back edges dashed Unvisited nodes black a)Initial graph b)Path of edges from A until node linking back to A found c)Start from B, reach F, a dead end (because E and I have already been visited) d)After backtracking to C, going on to G and hitting J, another dead end.
6 Slide adapted from Goodrich & Tamassia e)After backtracking to G f)After backtracking to N (which had been sitting on the stack waiting)
7 Slide adapted from Goodrich & Tamassia
8
9
10 Slide adapted from Goodrich & Tamassia
11 Slide adapted from Goodrich & Tamassia
12 Slide adapted from Goodrich & Tamassia
13 Slide adapted from Goodrich & Tamassia
14 Digraphs (Directed Graphs)
15 Slide adapted from Goodrich & Tamassia (Directed Graphs)
16 Slide adapted from Goodrich & Tamassia
17 Slide adapted from Goodrich & Tamassia
18 DAGs Directed Acyclic Graphs
19 Slide adapted from Goodrich & Tamassia
20 Slide adapted from Goodrich & Tamassia
21 Slide adapted from Goodrich & Tamassia
22 Slide adapted from Goodrich & Tamassia
23 Slide adapted from Goodrich & Tamassia
24 Weighted Graphs
25 Slide adapted from Goodrich & Tamassia
26 Slide adapted from Goodrich & Tamassia
27 Greedy Algorithms An algorithm which always takes the best immediate, or local, solution in looking for an answer. Greedy algorithms sometimes find less-than- optimal solutions Some greedy algorithms always find the optimal solution –Dijkstra’s shortest paths algorithm –Prim’s algorithm for minimum spanning trees
28 Slide adapted from Goodrich & Tamassia An animation
29 Slide adapted from Goodrich & Tamassia
30 Slide adapted from Goodrich & Tamassia
31 Slide adapted from Goodrich & Tamassia
32 Slide adapted from Goodrich & Tamassia
33 Slide adapted from Goodrich & Tamassia
34 Slide adapted from Goodrich & Tamassia
35 Slide adapted from Goodrich & Tamassia
36 Slide adapted from Goodrich & Tamassia
37 This algorithm uses a forest of trees. – Initially the forest consists of n single node trees (and no edges). –At each step, we add one (the cheapest one) edge so that it joins two trees together. –If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so we don’t use the edge in this case. Kruskal’s Minimum Spanning Tree
38 Kruskal’s Minimum Spanning Tree
39 Kruskal’s Minimum Spanning Tree (cont)
40 Kruskal’s Minimum Spanning Tree (cont) An animation of Kruskal’s