IE172 – Algorithms in Systems EngineeringLecture 16Pietro BelottiDept. of Industrial and Systems EngineeringLehigh UniversityFebruary 24, 2010Next time: midterm practiceBreadth-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 empty5. u ← pop(Q) (u is the node to process)6. For all nodes 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 (D FS)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 the latest nodes over the earliest ones.We can slightly modify the BFS to change this priority scheme:◮the BFS uses a FIFO structure (queue)◮the DFS simply requires a LIFO (stack)Depth-First Search — almost the reInput: 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 nodes 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 purpose.◮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 gr aphs.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 undirected graphs too.DFS ColorsIn this implementation, we will use colo rs:◮white: node is undiscovered◮gray: node is discovered, but not finished◮black: node is finished (i.e. we have completely exploredeverything from this node)Discovery and Finish Times: d(v) and f (v)◮Unique integers from 1 to 2|V| denoting when you firstdiscover a node and when you 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 e xplores a graphu v wx y z1/u v wx y z1/2/u v wx y z1/2/3/u v wx y z1/2/3/4/How DFS e xplores a graphu v wx y z1/2/3/4/5u v wx y z1/2/4/5 3/6u v wx y z1/4/5 3/62/7u v wx y z4/5 3/62/71/8How DFS e xplores a graphu v wx y z4/5 3/62/71/89/u v wx y z4/5 3/62/71/89/10/u 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 nodev.◮Since DFS visit is called exactly once per node, 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 graphGraph Review◮A path in G is a sequence o f nodes such that each node isadjacent to the node preceding it in the sequence. Simplepathsdo not repeat nodes.◮A (simple) cycle is a (simple) path except that the first andlast nodes are the same.◮Paths and cycles can either be directed or undirected◮If I say “cycle” or “path,” I will often mean simple,undirected cycle or pathTrees and Forests◮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 connected 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 (e.g. a directed forest)◮A subtree is simply 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 thepredecessor relationship in Gπ(surely, d(v) < f (v)).If I finish exploring u before first exploring v, then v is not adescendant of u: d(u) < f (u) < d(v) < f(v)[d(u), f(u)] ⊂ [d(v), f (v)] ⇒ u is a descendent of v in the DFS tree[d(u), f(u)] ⊂ [d(v), f (v)]⇒ v is a descendent of u in the DFS treeClassifying Edges in the DFS Treeu v wx y z4/5 3/62/71/810/119/12Given a DFS Tree Gπ, there are four type of edges (u, v)◮Tree Edges: Edges in Eπ. These are found by exploring(u, v) in the DFS procedure◮Back Edges: Connect u to an ancestor v in a DFS tree◮Forward Edges: Connect u to a descendent v in a DFS tree◮Cross Edges: All o ther edges. They can be edges in thesame DFS tree, or can cross trees in teh DFS forest
View Full Document