Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.

Similar presentations


Presentation on theme: "Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue."— Presentation transcript:

1 Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue

2 Overview Goal –To systematically visit the nodes of a graph A tree is a directed, acyclic, graph (DAG) If the graph is a tree, –DFS is exhibited by preorder, postorder, and (for binary trees) inorder traversals –BFS is exhibited by level-order traversal

3 Depth-First Search // recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; if (v not yet visited) visit&mark(v); // visit node before adjacent nodes for (each w adjacent to v) if (w has not yet been visited) dfs(w); } // dfs

4 Depth-First Search // recursive, postorder, depth-first search void dfs (Node v) { if (v == null) return; tag(v); // mark v as having been considered for (each w adjacent to v) if (w has not yet been tagged) dfs(w); visit(v); // postorder traversal: visit node after // adjacent nodes } // dfs

5 Depth-First Search // non-recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited) push(w); } // while } // dfs

6 Depth-First Search // non-recursive, postorder, depth-first search void dfs (Node v) { // Lex 20 (Do not need to submit) } // dfs

7 Example 0 7 1 5 4 3 2 6 Policy: Visit adjacent nodes in increasing index order

8 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4

9 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Push 5

10 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 Pop/Visit/Mark 5

11 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Push 2, Push 1

12 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 2 Pop/Visit/Mark 1

13 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 2 4 2 Push 4, Push 2, Push 0

14 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 2 4 2 Pop/Visit/Mark 0

15 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 2 4 2 Push 7, Push 3

16 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 2 4 2 Pop/Visit/Mark 3

17 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Push 2

18 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Pop/Mark/Visit 2

19 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 2 4 2 Pop/Mark/Visit 7

20 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 2 4 2 Push 6

21 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 2 4 2 Pop/Mark/Visit 6

22 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 2 Pop (don’t visit) 2

23 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 2 Pop/Mark/Visit 4

24 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 Pop (don’t visit) 2

25 Preorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 2 7 6 4 Done

26 Preorder DFS: Start with Node 5 Note: edge (0,3) removed 0 7 1 5 4 3 2 6 5 1 0 7 6 2 4 3

27 Depth-First Search Policy: Don’t push nodes twice // non-recursive, preorder, depth-first search void dfs (Node v) { if (v == null) return; push(v); while (stack is not empty) { pop(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && not yet stacked) push(w); } // while } // dfs

28 Preorder DFS (Don’t push nodes twice). Start with Node 5 0 7 1 5 4 3 2 6 5 1 0 3 7 6 4 2

29 Postorder DFS: Start with Node 5 0 7 1 5 4 3 2 6 2 3 6 7 0 4 1 5

30 Breadth-first Search Ripples in a pond Visit designated node Then visited unvisited nodes a distance i away, where i = 1, 2, 3, etc. For nodes the same distance away, visit nodes in systematic manner (eg. increasing index order)

31 Breadth-First Search // non-recursive, preorder, breadth-first search void bfs (Node v) { if (v == null) return; enqueue(v); while (queue is not empty) { dequeue(v); if (v has not yet been visited) mark&visit(v); for (each w adjacent to v) if (w has not yet been visited && has not been queued) enqueue(w); } // while } // bfs

32 BFS: Start with Node 5 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0

33 BFS: Start with Node 5 7 1 5 4 3 2 6 5 0

34 BFS: Node one-away 7 1 5 4 3 2 6 5 0

35 BFS: Visit 1 and 2 7 1 5 4 3 2 6 5 1 2 0

36 BFS: Nodes two-away 7 1 5 4 3 2 6 5 1 2 0

37 BFS: Visit 0 and 4 7 1 5 4 3 2 6 5 1 2 0 4 0

38 BFS: Nodes three-away 7 1 5 4 3 2 6 5 1 2 0 4 0

39 BFS: Visit nodes 3 and 7 7 1 5 4 3 2 6 5 1 2 0 4 3 7 0

40 BFS: Node four-away 7 1 5 4 3 2 6 5 1 2 0 4 3 7 0

41 BFS: Visit 6 7 1 5 4 3 2 6 5 1 2 0 4 3 7 6 0


Download ppt "Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue."

Similar presentations


Ads by Google