Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.

Similar presentations


Presentation on theme: "Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi."— Presentation transcript:

1 Graph Traversal BFS & DFS

2 Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi j

3 Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. 2 3 8 10 1 4 5 9 11 6 7

4 Graph Search Methods (traversal) A search method starts at a given vertex v and visits/labels/marks every vertex that is reachable from v. 8 10 5 9 11 6 7 2 3 1 4

5 Graph Search Methods Many graph problems solved using a search method.  Finding Path(shortest) from one vertex to another.  Check if the graph is connected?  Find a (minimum) spanning tree. Commonly used search (traversal) methods:  Breadth-first search.  Depth-first search.

6 Breadth-First Search Visit start vertex (selected by user) and put into a FIFO queue. Repeatedly remove a vertex from the queue, visit its unvisited adjacent vertices, put newly visited vertices into the queue. BFS(G,1) 5 2 3 1 4

7 Breadth-First Search Example Start search at vertex 1. 2 3 8 10 1 4 5 9 11 6 7

8 Breadth-First Search Example Visit/mark/label start vertex and put in a FIFO queue. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 1

9 Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 1 Blue vertex is visited

10 Breadth-First Search Example Remove 1 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 2 4 4

11 Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 2 4 4

12 Breadth-First Search Example Remove 2 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 4 5 5 3 3 6 6

13 Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 4 5 5 3 3 6 6

14 Breadth-First Search Example Remove 4 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 5 6 3 6

15 Breadth-First Search Example Remove 3 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 5 6 3 6

16 Breadth-First Search Example Remove 5 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 6 6 7 9 97

17 Breadth-First Search Example Remove 7 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 697 7 9

18 Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 697 9

19 Breadth-First Search Example Remove 9 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 697 8

20 Breadth-First Search Example Remove 8 from Q; visit adjacent unvisited vertices; put in Q. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 6978 8

21 Breadth-First Search Example Queue is empty. Search terminates. 2 3 8 10 1 4 5 9 11 6 7 1 FIFO Queue 2 4 5 3 6978

22 Example start from 2, 7 and 9 2 3 8 10 1 4 5 9 11 6 7

23 Breadth-First Search Property All vertices reachable from the start vertex (including the start vertex) are visited.

24 Breadth-first-search

25

26 Example start at 1 2 3 8 10 1 4 5 9 11 6 7

27 2 3 8 10 1 4 5 9 11 6 7 Example start at 1

28 Example start at 2 2 3 8 10 1 4 5 9 11 6 7

29 Example start at 2 2 3 8 10 1 4 5 9 11 6 7

30 Finding a Path From Vertex v To Vertex u Start a breadth-first search at vertex v. Terminate when vertex u is visited or when Q becomes empty (whichever occurs first).

31 Is The Graph Connected? Start a breadth-first search at any vertex of the graph. Graph is connected iff all n vertices get visited.

32 Connected Components Start a breadth-first search at any as yet unvisited vertex of the graph. Newly visited vertices (plus edges between them) define a component. Repeat until all vertices are visited.

33 Connected Components 2 3 8 10 1 4 5 9 11 6 7

34 Spanning Tree Breadth-first search from vertex 1. 2 3 8 1 4 5 9 6 7 12 4 3 65978 Breadth-first spanning tree.

35 Spanning Tree Start a breadth-first search at any vertex of the graph. If graph is connected, the n-1 edges used to get to unvisited vertices define a spanning tree (breadth-first spanning tree).

36 Depth-First Search depthFirstSearch(v) { Label vertex v as reached. for (each unreached vertex u adjacenct from v) depthFirstSearch(u); }

37 Depth-First Search Example Start search at vertex 1. 2 3 8 10 1 4 5 9 11 6 7 Label vertex 1 and do a depth first search from either 2 or 4. 12 Suppose that vertex 2 is selected.

38 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 2 and do a depth first search from either 3, 5, or 6. 1225 Suppose that vertex 5 is selected.

39 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 5 and do a depth first search from either 3, 7, or 9. 122559 Suppose that vertex 9 is selected.

40 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 9 and do a depth first search from either 6 or 8. 12255998 Suppose that vertex 8 is selected.

41 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 Label vertex 8 and return to vertex 9. 122559988 From vertex 9 do a dfs(6). 6

42 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 122559988 Label vertex 6 and do a depth first search from either 4 or 7. 66 4 Suppose that vertex 4 is selected.

43 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 122559988 Label vertex 4 and return to 6. 66 44 From vertex 6 do a dfs(7). 7

44 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 122559988 Label vertex 7 and return to 6. 66 44 77 Return to 9.

45 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 12255998866 44 77 Return to 5.

46 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 12255998866 44 77 Do a dfs(3). 3

47 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 12255998866 44 77 Label 3 and return to 5. 33 Return to 2.

48 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 12255998866 44 77 33 Return to 1.

49 Depth-First Search Example 2 3 8 10 1 4 5 9 11 6 7 12255998866 44 77 33 Return to invoking method.

50 Example start from 2, 7 and 9 2 3 8 10 1 4 5 9 11 6 7

51 Depth-First Search Properties Same properties with respect to path finding, connected components, and spanning trees. Edges used to reach unlabeled vertices define a depth-first spanning tree when the graph is connected. There are problems for which bfs is better than dfs and vice versa.

52 Depth-first-search

53

54 54 Graph Algorithms Minimum Spanning Trees

55 55 Problem: Laying Telephone Wire Central office

56 56 Wiring: Naïve Approach Central office Expensive!

57 57 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers

58 58 Minimum Spanning Tree (MST) (see Weiss, Section 24.2.2) it is a tree (i.e., it is acyclic) it covers all the vertices V –contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree is a subgraph of an undirected weighted graph G, such that

59 59 Applications of MST Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning…) Provides a heuristic for traveling salesman problems.

60 60 How Can We Generate a MST? a c e d b 2 45 9 6 4 5 5 a c e d b 2 45 9 6 4 5 5

61 Kruskal’s Algorithm u is a vertex FIND-SET(u) returns a representative element from the set that contains u. We can determine whether two vertices u and v belong to the same tree by testing whether FIND-SET(u) equals FIND-SET(v).

62 Kruskal’s Algorithm a c e d b 2 45 9 6 4 5 5

63

64 Example 1

65

66

67

68 Example 2

69

70 Homework #3  Implement Kruskal’s Algorithm for finding a minimum spanning tree  Test your program using  Using adjacency matrix to represent the graph  Output the edges  Due date: May 4 by 11:59pm


Download ppt "Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi."

Similar presentations


Ads by Google