Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?

Similar presentations


Presentation on theme: "11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?"— Presentation transcript:

1 11 Graph Search Algorithms

2 2 What parts of the graph are reachable from a given vertex ?

3 3 Exploring a graph is like Navigating a Maze During the exploration process we will collect information. For each node, we will save the times of two important events: 1.The moment of first discovery (previsit) 2.Final departure (postvisit). We will define a simple counter clock, initially set to 1, which is advanced after every event.

4 4 Explore Input: G = (V,E); v  V Output: –visited(u) = true for all nodes u reachable from v –pre(u),post(u): start & end times Explore(G,v) 1.visited(v) = true 2.pre(v) = clock 3.clock ++ 4.for each edge (v, u)  E if not visited(u) { explore(G,u) } 5.post(v) = clock 6.clock ++

5 5 Explore on a Directed Graph BA EF c D G H D B A E F c G H D 1 2 12,15 3 13,14 8,9 4 5 16,11, 10,7, 6

6 6 DFS The explore procedure visits only parts of the graph reachable from its starting point. To examine the rest of the graph, we need to restart the procedure elsewhere, at some node that has not yet been visited. The algorithm depth-first search (DFS), does this repeatedly until the entire graph has been traversed. DFS calls explore to vertices which were not visited yet.

7 7 DFS DFS(G) for all v  V visited(v) = false // initialization for all v  V if not visited(v) explore(v)

8 8 Types of Edges A is the root of the search tree Everything else is its descendant. Similarly, E has descendants F, G, and H, and conversely, is an ancestor of these three nodes. C is the parent of D, which is its child. For undirected graphs we distinguish between tree edges and non-tree edges. In the directed case, there is a slightly more elaborate taxonomy. B A E F c G H D

9 9 Types of Edges-Directed Graph Tree edges are actually part of the DFS forest. Forward edges lead from a node to a non-child descendant in the Explore tree. Back edges lead to an ancestor in the Explore tree. Cross edges lead to neither descendant nor ancestor; they therefore lead to a node that has already been completely explored (that is, already post-visited). B A E F c G` H D

10 10 Parenthesis Structure Ancestor and descendant relationships, as well as edge types, can be read off directly from pre and post numbers. Because of our exploration strategy, vertex u is an ancestor of vertex v exactly in those cases where u is discovered first and v is discovered during explore(u). pre(u) < pre(v) < post(v) < post(u), which we can depict pictorially as two nested intervals: u uvv

11 11 Brackets Property For any nodes u and v, the two intervals [pre(u),post(u)] and [pre(v),post(v)] are either disjoint or one is contained within the other. Why? [pre(u),post(u)] is the time after the discovery of u and before the departure of u. Every node is assigned pre time stamp exactly once, and post time stamp exactly once. The algorithm will departure u only after exploring all the nodes discovered starting from him. u u v v Not possible !

12 12 Types of Edges-Directed Pre/post ordering for (u,v) Edge Type Tree/Forward Back Cross u uvv v vuu u u v v

13 13 Checking for Acyclicity We can test for acyclicity in linear time, with a single Explore search. Property: A directed graph has a cycle if and only if its Explore search reveals a back edge. Proof: “→”: if (u, v) is a back edge, then there is a cycle consisting of this edge together with the path from v to u in the search tree. “←”: if the graph has a cycle v 0 → v 1 → v 2 → … → v k → v 0, look at the first node on this cycle to be discovered (the node with the lowest pre number). Suppose it is v i. All the other v j on the cycle are reachable from it and will therefore be its descendants in the search tree. In particular, the edge v i-1 → v i (or v k → v 0 if i = 0) leads from a node to its ancestor and is thus by definition a back edge.

14 14 DAG A cycle in a directed graph is a circular path v 0 → v 1 → v 2 → … → v k → v 0 A graph without cycles is acyclic. DAG=Directed Acyclic Graphs Is this a DAG? 12 54 3 6

15 15 Getting Dressed undershorts pants belt socks shoes shirt tie jacket watch undershortspantsbeltsocksshoesshirttiejacketwatch

16 16 Topological Sort What types of DAGs can be linearized? All of them. And once again Explore search tells us exactly how to do it: simply perform tasks in decreasing order of their post numbers. The only edges (u, v) in a graph for which post(u) < post(v) are back edges (recall the table of edge types) and we have seen that a DAG can NOT have back edges.

17 17 Topological Sort –Algorithm I Property: In a DAG, every edge leads to a node with a lower post number. This gives a linear-time algorithm for ordering the nodes of a DAG. Sort the nodes by decreasing post numbers When DFS departures the t node, we can store it in index n-t in an output array. Time complexity: O(|E|+|V|) (DFS)

18 18 Source and Sink The node with the smallest post number comes last in this linearization, and it must be a sink: no outgoing edges. The node with the highest post is a source: a node with no incoming edges. 12 54 3 6

19 19 Topological Sort-Algorithm II Property: Every DAG has at least one source and at least one sink. This suggests an approach to linearization: Find a source, output it, and delete it from the graph. Repeat until the graph is empty.

20 20 Algorithm II-Time Complexity We can save for each node the number of incoming edges (in-degree) When we erase a node u from the graph, we update the in-degree of all nodes that have an incoming edge from u (u,v). If the nodes are stored in a priority queue implemented by as heap and sorted by in- degree, complexity is: O(|E|log|V|+|V|log|V| )


Download ppt "11 Graph Search Algorithms. 2 What parts of the graph are reachable from a given vertex ?"

Similar presentations


Ads by Google