1 / 26

CS200: Algorithm Analysis

Learn about depth-first search algorithm on directed/undirected graphs through timestamps and edge classifications. Understand ancestor/descendent relationships and white path theorem. Discover how DFS helps identify cycles in graphs.

inger
Download Presentation

CS200: Algorithm Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS200: Algorithm Analysis

  2. ADDITIONAL GRAPH ALGORITHMS DEPTH-FIRST search of a graph that may be directed or undirected (Lab 4). • Assume an adjacency list representation. • Goal of Algorithm: methodically explore each vertex and every edge by searching deeper in the graph whenever possible. Pseudo-code as in text but omit setting of predecessor field.

  3. DFS(G) for each vertex u in V[G] do color[u] = white //color vertices white time = 0 //set global time for each vertex u in V[G] do if color[u] = white then DFSVisit(u)

  4. DFSVisit(u) //d[u] is discovery time of u, f[u] is finish time of u color[u] = gray time = time + 1 d[u] = time for each v in ADJ[u] do //each adj is colored gray if color[v] = white do DFSVisit(v) color[u] = black//when all vertices of u visited time = time + 1 //color it black f[u] = time

  5. Vertices are colored to indicate their state. A vertex becomes black only when all adjacent vertices becomes black. DFS timestamps each vertex when it is discovered (d[u]) and when it is finished (f[u]). Each timestamp is an integer between 1..2|V|. Timestamps are used to reason about behavior of DFS.

  6. Runtime of DFS: • lines 1,2 in O(|V|) time. • lines 4..6 in O(V2) time because DFSVisit is called exactly once for each vertex (can’t be more because its colored gray). • but during an execution of DFSVisit, the loop in lines 4..6 is executed |ADJ[v]| times and since S(|ADJ[v]| = Q(E) v in V Therefore Runtime is Q(V+E). Show example trace of algorithm filling in colors and d[u] and f[u].

  7. Perform DFS on following graph

  8. Depth First Search continued There are certain kinds of edges that can be recognized during DFS (the following edges can be determined via DFS).

  9. Depth First Search continued There are certain kinds of edges that can be recognized during DFS(the following edges can be determined via DFS). • Tree edge occurs when encountering a new white vertex. i.e edge(gray,white). An edge in a DF forest. • Back edge occurs when encountering an ancestor from a descendent. i.e. edge(gray,gray). • Forward edge occurs when encountering a descendant from an ancestor. i.e edge(gray,black). Non-tree edge. • Cross edge occurs when encountering remaining edges. i.e between sub-trees or tree edges. i.e edge(gray,black).

  10. Ancestor/Descendent Relation The ancestor/descendent notion is with respect to tree edges => White Path Theorem. Typically, tree and back edges are of significance and most algorithms don’t differentiate between cross and forward edges.

  11. ADDENDUM TO ANCESTOR DESCENDENT RELATIONSHIP Descendent/ancestor relationship within trees in a DF forest (trees are constructed via a DFS). 2 definitions • White Path Theorem: if v is a vertex in G and u is reachable from v along a white path then u is a descendent of v. => ancestor/descendent relationship is defined by tree edges. • We can use discovery and finish times to define relationship. If an interval (dv..fv) is completely enclosed in the interval (du..fu) then u is an ancestor of v. Show drawing.

  12. [ 1 12] [13 16] [2 7] [8 11] [14,15] [3, 4] [5, 6] [9, 10] Descendants are intervals that are contained within a larger interval (lines 2 and 3 represent descendants). Two trees are formed in this depth first search [1 …12] and [13 … 16]. • An edge from an ancestor to its unvisited descendant is a tree edge (within same tree). • An edge from an ancestor to its finished descendant is a forward edge (within same tree). • An edge from a descendant to an ancestor is a back edge. • Remaining edges are cross edges (between trees or when ancestor/descendant relationship does not hold).

  13. [ 1 12] [13 16] [2 7] [8 11] [14,15] [3, 4] [5, 6] [9, 10]

  14. Lemma 22.9, White Path Theorem If G is undirected then a DFS produces only tree and back edges. Sketch of proof: • Assume there’s a forward edge, F. But F must really be a back edge since we must finish processing bottom vertex before we resume at top vertex. i.e. both a ,c are gray. • Assume there’s a cross edge, C, between sub-trees. But C can’t be a cross edge since it will be explored from one of the vertices it connects and one of the T edges is really a B edge. Show by example, both a and c are gray. From the above :

  15. An undirected graph is acyclic (no cycles=> a forest) iff a DFS yields no back-edges. Both of the above examples are cyclic. Argument: • Given an acyclic graph => no back edges...trivial because back edges imply a cycle. • Given no back edges => acyclic graph....because if there are no back edges then there are only tree edges (and by above lemma) graph must be a forest and a forest has no cycles. This means we can use DFS to determine whether a graph contains a cycle

  16. Time to determine whether G has a cycle _________? using DFS. Run time is O(V) not O(V+E). WHY? Theorem B.2 - 5, Properties of Free Trees - a connected, acyclic, undirected graph • If we see |V| distinct edges , one of those must be a back edge because in an acyclic undirected forest, |E| <= |V| - 1. • Forest - an acyclic, undirected graph which may be disconnected.

  17. DIRECTED ACYCLIC GRAPHS - DAGS No directed cycles. Show examples, next slide. As in the undirected case, a directed graph is acyclic iff DFS of G yields no back edges. See proof: Theorem 22.8 in text. Runtime is the same as DFS of undirected graph = O(V+E).

  18. DAG Examples

  19. TOPOLOGICAL SORT OF A DAG Construct an ordering of vertices st if (u,v) is in E then u < v in the ordering. Used to sort a DAG by precedence constraints. There may exist more than 1 valid ordering. Show example graph G from text. Edges show precedence. Do DFS on G, output vertex v when finished ( discovered all vertices u, adjacent to v).

  20. Socks, shorts, hose, pants, skates, leg pads, tshirt, chest pad, sweater, mask, batting glove, catch glove, blocker

  21. TOPOLOGICAL SORT 1. Do DFS(G) //compute finish times f(v) for each v. 2. Insert v in linked list //as v is finished place at front of list. 3. Return linked list of vertices. Trace algorithm with (3,5,7) Run time of Topological Sort. • Time to do DFS: O(V+E) • Time to insert v in list:O(V) => O(V+E)

  22. Correctness Proof : Depends on the claim (u,v) is in E => f(u) > f(v). When (u,v) is explored, u is gray so what is v? • If v = gray then (u,v) is a back-edge but this is a contradiction since a DAG has no cycles. • If v = white then v is a descendent of u and (u,v) is a tree edge therefore f(u) > f(v). That is, we must finish v before we go back and finish u. • If v = black then f(v) < f(u).

  23. Summary • Know DFS – Ancestor/Descendant (discovery/finish times), edge types, run-time • Topological Sort – Run time • White Path Theorem

More Related