Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.

Similar presentations


Presentation on theme: "Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some."— Presentation transcript:

1 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)

2 Depth-first search Depth-first search (DFS) is a general technique for traversing a graph Explores “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

3 Depth-first search A DFS traversal of a graph G
Visits all the vertices and edges of G Computes a spanning forest of G Determines whether G is connected Computes the connected components of G Topological sort Articulation points

4 Depth First Search Algorithm
Algorithm DFS Input: A (un)directed graph G = (V, E). Output: Preordering and postordering of the vertices of the corresponding DFS forest. 1. for each vertex v  V 2. mark v unvisited 3. end for 4. predfn  0; postdfn  0 5. for each vertex v  V 6. if v is marked unvisited then dfs(v) 7. end for

5 Depth First Search Algorithm
Procedure dfs(x) 1. mark x visited 2. predfn  predfn + 1; x.pre  predfn 3. for each edge (x, y)  E 4. if y is marked unvisited then dfs(y) 5. end for 6. postdfn  postdfn + 1; x.post  postdfn

6 Depth First Search DFS introduces an important distinction among edges in the original graph: Tree edge: encountered new vertex Back edge: encountered old vertex from descendent to ancestor Further, for Directed graphs Forward edge: from ancestor to descendent Cross edge: (all others) between trees

7 Example unexplored vertex visited vertex unexplored edge tree edge
B A C E A visited vertex unexplored edge tree edge back edge D B A C E D B A C E

8 Example (cont.) D B A C E D B A C E D B A C E D B A C E

9 Example 1 c a b g f h d e i j 1, 10 a 6, 8 f b 2, 9 g 7, 7 3, 3 c 8, 6
10, 4 9, 5 8, 6 7, 7 6, 8 5, 1 4, 2 3, 3 2, 9 1, 10 a b c d e f g h i j c a b g f h d e i j

10 (Blackboard) Example 2 e c d f b a

11 Time complexity of DFS Running time
The first for loop in DFS takes time Q(V). DFS-Visit is called once for every vertex its only invoked on unvisited vertices for each dfs(v), a loop interates over all v.adj checking each edge from v the total cost for all dfs calls is Q(E) each edge checked twice (in undirected gr) the running time of DFS is Q(V+E)

12 Applications: Cycle Detection
Let G = (V, E) be a directed or undirected graph with n vertices and m edges. To test whether G has at least one cycle: If G is known to be a connected undirected graph, Then, G is acyclic if and only if G is a tree if and only if . Apply depth-first search on G: If a back edge is detected during the search, then G is cyclic; otherwise G is acyclic.

13 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. If there is just one vertex s with in_degree=0, perform DFS starting from vertex s. Order the vertices in decreasing order of the postdfn counter If there are more than one such vertex, add a dummy vertex s, and connect all those vertices to s.

14 (Blackboard) Example Job Prerequisite(s) --- --------------- J1 J3
J J3 J J3 J J J3 J5 J1,J4 J J2 J7 J2,J8 J8 J5,J6

15 A A B C B C D E F D E F Application: Doing tasks in a reasonable order. Taking courses that have pre-reqs… (Assuming you’re only taking one at a time) A C F B D E

16 Articulation points Suppose you are a hacker seeking to disrupt a company’s intranet. Which router in the diagram below should you choose to blow up in order to cause the maximum amount of damage? B C D A E F G H I J

17 Articulation points A vertex v in an undirected graph G=(V,E), |V| 3 is called an articulation point if there exist 2 dist vertices u and w that are diff from v such that any path between u and w must pass through v. If G is connected, the removal of v and its incident edges will result in a disconnected subgraph of G.

18 Articulation points The easiest (brute force) solution is to remove a vertex (and its edges) one by one from G and test whether the resulting graph is still connected or not (say by DFS). The running time is O(V*(V+E)). Some useful observations about DFS trees: If the root has two or more children, it is an articulation point. A leaf is not an articulation point. Some parts of the tree have (back) edges that ’climbs’ to the upper part of the tree, while other does not have such edges.

19 Articulation points We define the highest point a node climb upto:
highest(v) = minimum of the following: v.pre, u.pre for each vertex u where (v,u) is a back edge. highest(w) for each child w of v in DFS tree. A vertex v other than the root is an articulation point iff v has a child w with highest(w)  v.pre.

20 Articulation points The articulation points are: b, c, g and h.
Find the articulation points of this graph. 5, 3 4, 3 3, 3 2, 1 1, 1 a b c d e 10, 8 9, 8 8, 8 7, 1 6, 1 f g h i j The articulation points are: b, c, g and h.

21 Strongly Connected Components
G is strongly connected if every pair (u, v) of vertices in G is reachable from one another. A strongly connected component (SCC) of G is a maximal set C  V such that every pair (u, v) of vertices in C is reachable from one another. A C B D E G F H I J

22 Algorithm to determine SCCs
SCC(G): Time: (V + E). call DFS(G) to compute u.post for all u call DFS(GT), but consider the vertices in order of decreasing u.post output the vertices in each tree of the DFS forest formed in second DFS as a separate SCC A D E F B C Workout on Blackboard

23 Another Example for students
1 2 3 4 5 6 7 8

24 Summary Depth-First-Search (DFS)
Explores “deeper” in the graph whenever possible When all of v’s edges are finished, backtrack to the vertex from which v was discovered Applications of DFS Determines whether G is connected/cyclic Topological sort Articulation points SCC


Download ppt "Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some."

Similar presentations


Ads by Google