CS 200 Algorithm Analysis ADDITIONAL GRAPH ALGORITHMS DEPTHFIRST

  • Slides: 26
Download presentation
CS 200: Algorithm Analysis

CS 200: Algorithm Analysis

ADDITIONAL GRAPH ALGORITHMS DEPTH-FIRST search of a graph that may be directed or undirected

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.

DFS(G) for each vertex u in V[G] do color[u] = white time = 0

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

DFSVisit(u) //d[u] is discovery time of u, f[u] is finish time of u color[u]

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

Vertices are colored to indicate their state. A vertex becomes black only when all

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.

Runtime of DFS: – lines 1, 2 in O(|V|) time. – lines 4. .

Runtime of DFS: – lines 1, 2 in O(|V|) time. – lines 4. . 6 in O(V 2) 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].

Perform DFS on following graph

Perform DFS on following graph

Depth First Search continued There are certain kinds of edges that can be recognized

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

Depth First Search continued There are certain kinds of edges that can be recognized

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

Ancestor/Descendent Relation The ancestor/descendent notion is with respect to tree edges => White Path

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.

ADDENDUM TO ANCESTOR DESCENDENT RELATIONSHIP Descendent/ancestor relationship within trees in a DF forest (trees

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.

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

[1 12] [13 [2 7] [8 [3, 4] [5, 6] 11] 16] [14, 15] [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).

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

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

Lemma 22. 9, White Path Theorem If G is undirected then a DFS produces

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 :

An undirected graph is acyclic (no cycles=> a forest) iff a DFS yields no

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

Time to determine whether G has a cycle _____? using DFS. Run time is

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.

DIRECTED ACYCLIC GRAPHS - DAGS No directed cycles. Show examples, next slide. As in

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

DAG Examples

DAG Examples

TOPOLOGICAL SORT OF A DAG Construct an ordering of vertices st if (u, v)

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

Socks, shorts, hose, pants, skates, leg pads, tshirt, chest pad, sweater, mask, batting glove,

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

TOPOLOGICAL SORT 1. Do DFS(G) //compute finish times f(v) for each v. 2. Insert

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)

Correctness Proof : Depends on the claim (u, v) is in E => f(u)

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

Summary • Know DFS – Ancestor/Descendant (discovery/finish times), edge types, run-time • Topological Sort

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