0% found this document useful (0 votes)
187 views41 pages

Graph Traversal: Text Depth-First Search Breadth-First Search

Graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS) are used to systematically visit the nodes of a graph. DFS prioritizes exploring nodes by depth, visiting nodes and their children before siblings. BFS prioritizes exploring nodes by breadth, visiting all nodes at the current distance from the starting node before moving to nodes farther away. The document provides pseudocode and examples of DFS and BFS applied to a sample graph.

Uploaded by

Sathish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
187 views41 pages

Graph Traversal: Text Depth-First Search Breadth-First Search

Graph traversal algorithms like depth-first search (DFS) and breadth-first search (BFS) are used to systematically visit the nodes of a graph. DFS prioritizes exploring nodes by depth, visiting nodes and their children before siblings. BFS prioritizes exploring nodes by breadth, visiting all nodes at the current distance from the starting node before moving to nodes farther away. The document provides pseudocode and examples of DFS and BFS applied to a sample graph.

Uploaded by

Sathish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 41

Graph Traversal

Text
Weiss, 9.6 Think Stack Think Queue

Depth-First Search Breadth-First Search

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

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

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

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

Depth-First Search
// non-recursive, postorder, depth-first search void dfs (Node v) {

// Lex 20 (Do not need to submit)

} // dfs

Example
5 0 1 7 3 4 2

Policy: Visit adjacent nodes in increasing index order

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2

6
5 1 0 3 2 7 6 4

Preorder DFS: Start with Node 5


5 0 1 7 3 4 5
Push 5

Preorder DFS: Start with Node 5


5 0 1 7 3 4
Pop/Visit/Mark 5 5

Preorder DFS: Start with Node 5


5 0 1 7 3 1 4 2
Push 2, Push 1 5

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2
Pop/Visit/Mark 1 5 1

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 0 2 4 2
Push 4, Push 2, Push 0

6
5 1

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 4 2
Pop/Visit/Mark 0 5 1 0

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 3 7 2 4 2
Push 7, Push 3 5 1 0

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 7 2 4 2
Pop/Visit/Mark 3 5 1 0 3

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 2 7 2 4 2
Push 2 5 1 0 3

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 7 2 4 2
Pop/Mark/Visit 2 5 1 0 3 2

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 4 2
Pop/Mark/Visit 7 5 1 0 3 2 7

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 6 2 4 2
Push 6 5 1 0 3 2 7

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2 4 2
Pop/Mark/Visit 6 5 1 0 3 2 7 6

Preorder DFS: Start with Node 5


5 0 1 7 3 4 4 2
Pop (dont visit) 2 5 1 0 3 2 7 6

Preorder DFS: Start with Node 5


5 0 1 7 3 4 2
Pop/Mark/Visit 4 5 1 0 3 2 7 6 4

Preorder DFS: Start with Node 5


5 0 1 7 3 4
Pop (dont visit) 2 5 1 0 3 2 7 6 4

Preorder DFS: Start with Node 5


5 0 1 7 3 4
Done 5 1 0 3 2 7 6 4

Preorder DFS: Start with Node 5 Note: edge (0,3) removed


5 0 1 7 3 4 2

6
5 1 0 7 6 2 4 3

// 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);

Depth-First Search Policy: Dont push nodes twice

for (each w adjacent to v) if (w has not yet been visited && not yet stacked) push(w); } // while } // dfs

Preorder DFS (Dont push nodes twice). Start with Node 5


5 0 1 7 3 4 2

6
5 1 0 37 6 4 2

Postorder DFS: Start with Node 5


5 0 1 7 3 4 2

6
2 3 6 7 0 4 1 5

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)

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

BFS: Start with Node 5


5 0 1 7 3 4 2

6
5 1 2 0 4 3 7 6

BFS: Start with Node 5


5 0 1 7 3 4
5

BFS: Node one-away


5 0 1 7 3 4
5

BFS: Visit 1 and 2


5 0 1 7 3 4
5 1 2

BFS: Nodes two-away


5 0 1 7 3 4
5 1 2

BFS: Visit 0 and 4


5 0 1 7 3 4 2

6
5 1 2 0 4

BFS: Nodes three-away


5 0 1 7 3 4 2

6
5 1 2 0 4

BFS: Visit nodes 3 and 7


5 0 1 7 3 4 2

6
5 1 2 0 4 3 7

BFS: Node four-away


5 0 1 7 3 4 2

6
5 1 2 0 4 3 7

BFS: Visit 6
5 0 1 7 3 4 2

6
5 1 2 0 4 3 7 6

You might also like