Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.

Similar presentations


Presentation on theme: "Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application."— Presentation transcript:

1 Graph Traversals Reading Material: Chapter 9

2 Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some special order based on graph topology. Examples: – Minimum Spanning Trees: Image Segmentation – Shortest paths problems: Route finding (traveling salesman problem)

3 Depth First Search Algorithm Input: Graph G=(V,E) and vertex v Output: DFS traversal of G starting from v. Procedure DFS(G,v) { PreVisit(G,v); // Take action Mark v as visited; for each edge (v,w)  E do if w is marked unvisited then DFS(G, w); PostVisit(G,v); // Take action }

4 Depth First Search DFS Traversal Cost: A B C D F E A B C D F E

5 Edge Classification Undirected Graphs –Tree edges –Back edges Directed Graphs –Tree edges –Back edges –Forward edges –Cross edges

6 Breadth First Search Algorithm Input: Graph G=(V,E) and vertex v Output: BFS traversal of G starting from v Procedure BFS(G,v) { queue Q=  // Queue initialized to empty Q.enqueue(v); // Initialize Q Mark v as visited; while (!Q.empty()) { v = Q.dequeue(); PreVisit(G,v); // Take action for each edge (v,w)  E do if w is marked unvisited then { mark w as visited; Q.enqueue(w); } PostVisit(G,v); // Take action }

7 Breadth First Search BFS Traversal Cost: A B C D F E A B C D F E

8 Graph Traversal Algorithm graphTraverse(G) { for v ← 1 to n do Mark v as unvisited; for v ← 1 to n do if (v is marked unvisited) then doTraverse(G, v); } doTraverse can be either replaced by DFS or BFS. Why do we need this algorithm? Aren’t previous DFS and BFS algorithms enough?

9 Example A B C D F E A B C D F E

10 Applications: Topological Sort Problem: Given a set of jobs, courses, etc., with prerequisite constraints, output the jobs in an order that does not violate any of the prerequisites. Solution: Use graphTraverse algorithm with: –doTraverse replaced with DFS –Prevesit(G,v) =  ; // i.e. no action –Postvisit(G,v)=print v; // i.e. print label of v This will result in a reverse topological sort Starting with different nodes may give different orders

11 Example JobPrerequisite(s) ------------------ J1 J3 J2 J3 J3 ----- J4 J3 J5 J1,J4 J6 J2 J7 J2,J8 J8 J5,J6

12 Applications: Graph Acyclicity There is a cycle in a graph if at least one ………… edge has been detected during graph traversal of the graph using DFS.

13 Applications: Shortest Distance Problem: Let G be a connected undirected graph and s a vertex in V. BFS can be modified such that the shortest distance between s and all other vertices is computed, where the shortest distance between s and v is defined here as the least number of edges connecting vertex s to vertex v. –Where is this application useful?

14 Strongly Connected Components A strongly connected component in a directed graph DG= is a set of vertices C (which is a subset of V) such that for every pair of vertices x,y in C, there exists a directed path from x to y and a directed path from y to x. –Thus, starting at any vertex in a strongly connected component C, it is possible to reach every other vertex in C.

15 Example 1 2 3 4 5 6 7 8

16 SCC Algorithm 1.Perform a depth first search on the directed graph DG. 2.Number the depth first search tree (or forest) using a postorder traversal (postvisit). 3.Form a new graph DGr= by reversing every edge in E to form Er. 4.Perform a depth first search on the directed graph DGr, with vertices ordered in decreasing order of their numbers generated in 2. Assign a distinct component number to all visited vertices every time the DFS procedure is called from within the general traversal algorithm.

17 Example 1 2 3 4 5 6 7 8


Download ppt "Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application."

Similar presentations


Ads by Google