IE172 – Algorithms in Systems EngineeringLecture 18Pietro BelottiDepartment of Industrial & Systems EngineeringLehigh UniversityFebruary 27, 2009Next time: Read chapter 23Directed Acyclic Graph s◮Directed ⇒ arcs, not edges◮Acyclic ⇒ there is no cycle◮DAGs are good at modeling processes and structures thathave a partial order (≺)◮A ≺ B a nd B ≺ C ⇒ A ≺ C◮May have neither A ≺ B nor B ≺ C◮Think of a partial order as “the way in wh ich you must dotasks” to ensure succes sful completion◮If you can’t relate A and B, i.e., if A ⊀ B and B ⊀ A, then itdoesn’t matter if you do A first or B first...The dressing algorithmTopological Sort◮A to pological sort of a directed acyclic graph (DAG) is alinear ordering of its nodes which is compatible with thepartial order ≺ induced on the nodes.◮u ≺ v if there’s a directed path from u to v in the DAG.◮An equivalent definition is that each node comes before allnodes to which it has edges.◮i.e. u must be done before v◮DAGs have at least one t opological sort (may have many)Topological S ort: The Whole Algorithm◮DFS search the gr aph◮List nodes in order of decreasing finishing timeTopological SortWhy Does This Work?◮Show that (u, v) ∈ E ⇒ f(v) < f (u)◮When we explore (u, v), u is gray◮Is v gray? No, since then DAG would have a cycle◮Is v white? Then it becomes descendant of u and (by par.theorem), d(u) < d(v) < f (v) < f (u)◮Is v black? Then w e’re finished and f (v) < f (u) since we’restill ex ploring uTherefore if (u, v) ∈ E, f (v) < f (u)Connectivity in graphsAn undirected graph is connected if, for any two n odes u, v,there is at least one (undirected!) path between u and v.A connected graph A graphAn digraph is strongly connecte d if, for any two n odes u, v,there is a directed path between u and v and one from v to u.A strongly connected digraph A digraphTranspose of a digraphGiven a digraph G = (V, A),its transpose GT= (V, AT) is a graphwith all arcs flipped:GT= (V, AT), AT= {(u, v) : (v, u) ∈ A}◮What is the running time to create GTgiven G?◮If a digraph is acyclic, its transpose is also acyclicStrongly Connected Com ponentsGiven a digr aph G = (V, A), a strongly connected componentof G is a maximal set of node s C ⊆ V such that ∀u, v, ∈ C thereexists a directed path both from u to v and from v to u.◮maximal means that there is no other set C′⊃ C with thesame property as C◮G and GThave the same Strongly Connected Components◮u and v are both reachable from each other in G if and onlyif they are both reachable in GTFinding Strongly Connected Com p on ents1. Call DFS(G) to topologically sort G2. Compute GT3. Call DFS(GT) but consider, in main DFS loop , nodes intopologically sorted order (from G)⇒ Each tree of depth-first forest from step 3 forms a SCCComponent Graph◮GSCC(G) = (VSCC, ASCC)◮VSCC: has one node for each strongly connectedcomponent of G◮(u, v) ∈ ASCCif there is an edge between SCC’s in GLemmaGSCCis a DAG◮For C ⊆ V, f (C)def= maxv∈C{f(v)}More LemmaLemmaLet C and C′be distinct SCC in G.◮If (u, v) ∈ A and u ∈ C, v ∈ C′, then f (C) > f (C′)◮If (u, v) ∈ ATand u ∈ C, v ∈ C′, then f (C) < f (C′)◮If f(C) > f (C′), there is no edge from C to C′in GTWhy SCC Works (Intuition)◮DFS on GTstarts with SCC C s uch th at f (C) is maximum.Since f(C) > f (C′), there are no edg es from C to C′in GT◮This means that the DFS will visit only nodes in C◮The next root has the largest f (C′) for all C′6= C. DFS visitsall nodes in C′, and any other edgesmust go to C, whichwe have already visitedThe Minimum S p anning Tree (MST) problemA Canonical Problem. . .◮A to wn as a set of houses and a s et of potential roads◮Each road connects two and only two houses◮Constructing road from house u to house w costs wuvThe Objective: Construct roads such that◮Everyone is Connected◮The total construction cost is minimumSpanning TreeWe model the problem as a gr aph problem.◮G = (V, E) is an undirected gr aph◮Weights w : E → R|E|, i.e., wuv∀(u, v) ∈ EFind T ⊂ E such that◮T connects all vertices◮The w eight w(T)def=X(u,v)∈Twuvis minimized◮The notation T is not a coincidence.◮The set of edges T will form a tree. (Why?)◮T is known as a minimum spanning tree (MST) of GHow to Buil d It?◮Let T be the emp ty set◮Add to T keeping the following loop invariant:T is always a su bset of an MST◮An edge {u, v} ∈ E is safe for T if T ∪ {{u, v}} is also asubset o f an MST◮The initial T = ∅ is safe◮In the i-th iteration of th e loop, we add a safe edge◮The goal for the algorithms is to quickly detect and addsafe edges.GENERIC-MST(V, E, w)1 T ← ∅2 while A is not a spanning tree3 do find {u, v} ∈ E that is safe for T4 T ← T ∪ {(u, v)}5 return TFinding Safe EdgesHow do I know if (u, v) is safe? (Intuition)◮Let S ⊂ V be any set of vertices that includes u but not v◮In any MST there must be at least one edge that connectsS to V \ S, so let’s make thegreedy choice of choosingthe one with the minimum weigh
View Full Document