Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs.

Similar presentations


Presentation on theme: "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs."— Presentation transcript:

1 1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs

2 2 Graph Traversal

3 3 Slide adapted from Goodrich & Tamassia

4 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 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 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 7 Slide adapted from Goodrich & Tamassia

8 8

9 9

10 10 Slide adapted from Goodrich & Tamassia

11 11 Slide adapted from Goodrich & Tamassia

12 12 Slide adapted from Goodrich & Tamassia

13 13 Slide adapted from Goodrich & Tamassia

14 14 Digraphs (Directed Graphs)

15 15 Slide adapted from Goodrich & Tamassia (Directed Graphs)

16 16 Slide adapted from Goodrich & Tamassia

17 17 Slide adapted from Goodrich & Tamassia

18 18 DAGs Directed Acyclic Graphs

19 19 Slide adapted from Goodrich & Tamassia

20 20 Slide adapted from Goodrich & Tamassia

21 21 Slide adapted from Goodrich & Tamassia

22 22 Slide adapted from Goodrich & Tamassia

23 23 Slide adapted from Goodrich & Tamassia

24 24 Weighted Graphs

25 25 Slide adapted from Goodrich & Tamassia

26 26 Slide adapted from Goodrich & Tamassia

27 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 28 Slide adapted from Goodrich & Tamassia An animation http://www.cs.uwa.edu.au/undergraduate/courses/230.300/readings/graphapplet/graph.html

29 29 Slide adapted from Goodrich & Tamassia

30 30 Slide adapted from Goodrich & Tamassia

31 31 Slide adapted from Goodrich & Tamassia

32 32 Slide adapted from Goodrich & Tamassia

33 33 Slide adapted from Goodrich & Tamassia

34 34 Slide adapted from Goodrich & Tamassia

35 35 Slide adapted from Goodrich & Tamassia

36 36 Slide adapted from Goodrich & Tamassia

37 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 38 Kruskal’s Minimum Spanning Tree http://swww.ee.uwa.edu.au/~plsd210/ds/krusk.html

39 39 Kruskal’s Minimum Spanning Tree (cont) http://swww.ee.uwa.edu.au/~plsd210/ds/krusk.html

40 40 Kruskal’s Minimum Spanning Tree (cont) http://swww.ee.uwa.edu.au/~plsd210/ds/krusk.html An animation of Kruskal’s http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/kruskal/Kruskal.shtml


Download ppt "1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs."

Similar presentations


Ads by Google