Unformatted text preview:

IE172 – Algorithms in Systems EngineeringLecture 17Pietro BelottiDepartment of Industrial & Systems EngineeringLehigh UniversityFebruary 18, 2009Next time: midterm practiceThe BFSBreadth-First TreeBFS creates a predecessor subgraph, defin ed by arcs (π(i), i) fo r allnodes i, exce pt s, such that π(i) 6= NIL and he n ce d(i) < +∞:Gπ= (V, Aπ); Aπ= {(π(v), v) ∀v ∈ V : π(v) 6= NIL}r s t uv w x y011 22233◮It is a tree (in general, not binary)⇒ One path between s and a node i, described by π(·)e.g. s ↔ u: (u; π(u) = t; π (t) = w; π(w) = s) ⇒ (u; t; w; s)◮It is a breadth-first tree: Th e path described by π() is theshortest w.r.t. the number of edgesBreadth-First Search ( BFS)Input: graph G = (V, E), node s ∈ V1. For each i ∈ V: c(i) ← white; π(i) ← NIL ; d(i) ← +∞2. c(s) ← gray; d(s) ← 0 (initializes source node)3. Create a queue Q; push(Q, s)4. While Q is not emp ty5. u ← pop(Q) (u is the node to process)6. For all nod es v in AdjList(u): (all neighbors of u)7. if c(v) = white8. c(v) ← gray9. d(v) ← d(u) + 110. π(v) ← u11. push(Q, v)12. c(u) ← blackDepth-first search (DFS)Unlike BFS, the DFS procedure visits every neighborhood assoon as it finds it out.◮DFS (s ∈ V):◮For each non-visited node i in N(s):◮DFS(i)While keeping track of all visited and covered nodes, the DFSprivileges th e latest nod es over the earliest ones.We can slightly modify the B FS to change this priority scheme:◮the BFS uses a FIFO structure (queue)◮the DF S simply requires a LIFO (stack)Depth-First Search — almost thereInput: graph G = (V, E), node s ∈ V1. For each i ∈ V: c(i) ← white; π(i) ← NIL ; d(i) ← +∞2. c(s) ← gray; d(s) ← 0 (initializes source node)3. Create a stack S; push(S, s)4. While S is not empty5. u ← pop(S) (u is the node to process)6. For all nod es v in AdjList(u): (all neighbors of u)7. if c(v) = white8. c(v) ← gray9. d(v) ← d(u) + 110. π(v) ← u11. push(S, v)12. c(u) ← blackDepth-First Search (the real one)Actually, the DFS has a different pur pose.◮We want to explore all nodes◮We don’t have a starting node in mind◮We might start a search from any node in the graph, if notvisited yetAlso, we’ll consider digraphs instead of graphs.Depth-First SearchInput: Digraph G = (V, A)1Output: Two timestamps for each node d(v), f (v)π(v), predecessor of v (not on shortest path necessarily)◮BFS: Discovers all nodes at distance k from starting node sbefore discovering any node at distance k + 1◮DFS: As soon as we discover a node, we explore from it.◮Here we are after creating a different predecessor subgraphGπ= (V, Aπ) with Aπ= {(π(v), v) | v ∈ V, π(v) 6= NIL}◮Not shortest edge-path lengths1Works for undi rected graphs too.DFS ColorsIn this implementation, we will use colors:◮white: node is undiscovered◮gray: n ode is discovered, but not finished◮black: node is finished (i.e. we have completely exploredeverything from t h is node)Discovery and Finish Times: d(v) and f (v)◮Unique integers from 1 to 2|V| denot ing when you firstdiscover a node and w h en yo u are done with it◮d(v) < f (v) ∀v ∈ VDFSDFS(V, A)1 for each u in V2 do color(u) ← white3 π(u) ← NIL4 time ← 05 for each u in V6 do if color(u) = white7 then DFS-VISIT(u)DFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeHow DFS explores a graphDFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeu v wx y z1/PSfragu v wx y z1/2/How DFS explores a graphDFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeu v wx y z1/2/3/u v wx y z1/2/3/4/How DFS explores a graphDFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeu v wx y z1/2/3/4/5u v wx y z1/2/4/5 3/6How DFS explores a graphDFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeu v wx y z1/4/5 3/62/7u v wx y z4/5 3/62/71/8How DFS explores a graphDFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeu v wx y z4/5 3/62/71/89/u v wx y z4/5 3/62/71/89/10/How DFS explores a graphDFS-VISIT(u)1 color(u) ← gray2 time++3 d(u) ← time4 for each v in AdjList(u)5 do if color(v) = white6 then π(v) ← u7 DFS-VISIT(v)8 color(u) ← black9 time++10 f (u) = timeu v wx y z4/5 3/62/71/89/10/11u v wx y z4/5 3/62/71/810/119/12Analysis of D FS◮Loop on lines 1-3: O(|V|)◮DFS-VISIT is called exactly once for each node v◮(Because the first thing you do is paint the node gray)◮Loop on lines 4-7 calls DFS-VISIT |AdjList(v)| times for n odev.◮Since DFS visit is called exactly once per n ode, the totalrunning time to do loop on lines 3-6 isXv∈V|AdjList(v)| = Θ(|A|)◮Therefore: running time of DFS on G = (V, A) isΘ(|V| + |A|):Linear in the (adjacency list) size of the g r aphGraph Review◮A path in G is a sequence of nodes such that each n ode isadjacent to the node preceding it in the sequence. Simplepathsdo not repeat nodes.◮A (simple) cycle is a (s imple) path except that the first andlast n odes are th e same.◮Paths and cycles can either be directed or undirected◮If I say “cycle” or “path,” I will often mean simple,undirected cycle or pathI Ca n’t See the Forest Through the...◮The DFS graph: Gπ= (V, Eπ) forms a forest of subtreesA tree T = (V, E)◮is a connected graph that does not contain a cycle◮All pairs of nodes in V are conne cted by a simple(undirected) path◮|E| = |V| − 1◮Adding any edge to E forms a cycle in T◮A (Undirected) acyclic graph is usually called a forest◮A DAG is a Directed, Acyclic Graph (A directed forest...)◮A subt ree is s imply a subgraph that is a treeParenthesis Theoremu v wx y z4/5 3/62/71/810/119/12The intervals [d(v), f (v)], for each node v ∈ V, tell us about t h epredecessor relationship in Gπ(surely, d(v) < f (v)).If I finish exploring u be fo re first


View Full Document

Clemson IE 172 - Lecture 17

Download Lecture 17
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 17 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 17 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?