Shortest Paths and Treewidth Algorithms and Networks This lecture • Given a graph G=(V,E) with a tree decomposition of width k, and with each edge a length (possibly negative), we can: – In O(nk2) time solve the Single Pair Shortest Paths problem – In O(nk2) time determine if there is a negative cycle – In O(n2k) time solve the All Pairs Shortest Paths problem 2 Shortest paths and treewidth Contents • Short background • Nice tree decompositions and top bags • Directed Path Consistency on nice tree decompositions • Main algorithm • Conclusions 3 Shortest paths and treewidth Background • Directed Path Consistency, by Dechter, Meiri, Pearl, 1991 • Earlier results: – Chaudhuri, Zaroliagis, 2000 • Slower, but also dynamic – Chleq, 1995 • Single Source Shortest Paths; gives same asympthotic bound for APSP, but larger factor in O-notation • Algorithm here (SNOWBALL) from • 4 Plancken, de Weerdt, van der Krogt, Computing All-Pairs Shortest Paths by Leveraging Low Treewidth, Journal of Artificial Intelligence Research 43 (2012) 353-388 Shortest paths and treewidth Nice tree decompositions (variant with larger leaf bags) • Rooted tree, and four types of nodes i: – Leaf: leaf of tree (note: the size 1 restriction of usual nice tree decompositions is dropped) – Join: node with two children j, j’ with Xi = Xj = Xj’. – Introduce: node with one child j with Xi = Xj {v} for some vertex v – Forget: node with one child j with Xi = Xj - {v} for some vertex v 5 Shortest paths and treewidth Making it nice Theorem. Given a tree decomposition of a graph G=(V,E) with n vertices, of width k and with r bags, one can build a nice tree decomposition of G of width k, with O(n) bags, in O(k(n+r)) time. 6 Shortest paths and treewidth Top bags • For each vertex v, there is a exactly one bag i with v in Xi and v not in Xparent(i) • Call this bag the top bag of v, TB(v) • The parent of the top bag of v is a forget node that forgets v: different vertices have different top bags 7 Shortest paths and treewidth A numbering of the vertices • Traverse the tree of the tree decomposition in pre-order • Number the vertices in the order in which their top bags appear in this ordering • Equivalently: number the vertices in the root bag, and then number vertices in order we see their forget nodes • Claim: if the top bag of v is a descendant of the top bag of w, then nb(v) > nb(w) 8 Shortest paths and treewidth A property of tree decompositions • If P is a path from v to w, then X contains at least one vertex from P v X w 9 Shortest paths and treewidth Directed Path Consistency • DPC Problem: – For all pairs v, w, such that there is a bag that contains v and w, compute B(v,w) = the length of a shortest path from v to w that uses (except v and w) only vertices with numbers larger than max(nb(v),nb(w)) • What we do: – For each bag i, for all v,w in Xi we compute Bi(v,w) = the length of a shortest path from v to w that uses only vertices whose top bag is a descendant of i (i.e. only live in the subtree below i) 10 Shortest paths and treewidth Plan • A Dynamic Programming algorithm computes a table Bi for all bags i, each in O(k2) time • Note that we must compute O(k2) values for each bag: O(1) time per computation • Procedure for each of the four types of nodes • Total time is O(nk2) for these steps 11 Shortest paths and treewidth Leaf nodes • Let i be a leaf node. • For all v,w in Xi: Bi (v,w) = length of arc from v to w (and if arc does not exist) 12 Shortest paths and treewidth Join nodes • Let i be a join node with children j, j’. • For all v,w in Xi: Bi (v,w) = min (Bj (v,w) , Bj’ (v,w) ) + 13 Why? = Shortest paths and treewidth Why? • Take a path P from v to w using only vertices with top bag below i • Claim: P uses only nodes in the subtree of j OR P uses only nodes in the subtree of j’ – If not, it must use another node in the bag Xi. – To see this: suppose not. Choose a vertex x with top bag in subtree of j from P and a vertex y with top bag in subtree of j’ – Go along P from x to y. At some point, we either visit a vertex in Xi or have an edge from a vertex in the subtree of j to a vertex in the subtree of j’ 14 Shortest paths and treewidth Introduce nodes • Let i be a node with child j, with Xi = Xj {v}. • For all w,x in Xj: v – Bi(v,w) = length of arc (v,w) if existing, otherwise – Bi(w,x) = Bj(w,x) Trivial case 15 Shortest paths and treewidth Forget nodes • Let i be a node with child j, with Xi = Xj - {v}. • For all w,x in Xi: v v – Bi(w,x) = min (Bj(w,x), Bj(w,v)+Bj(v,x)) The path now can use v as internal vertex w x v 16 Shortest paths and treewidth Back to DPC • For all pairs v, w, such that there is a bag that contains v and w, compute B(v,w) = the length of a shortest path from v to w that uses (except v and w) only vertices with numbers larger than max(nb(v),nb(w)) • If nb(v) < nb(w): – The top bag of w is a descendant of the top bag of v – B(v,w) = BTB(v)(v,w) – Argument uses again properties of tree decompositions: induction shows you cannot leave the subtree rooted at v in the path • Similar, if nb(w) < nb(v): B(v,w) = BTB(w)(v,w) 17 Shortest paths and treewidth Single pair shortest paths • The shortest path from s to t: – Add (s,t) as edge to G – Make tree decomposition of G+(s,t) – Width is at most one more than width of G (e.g., add t to all bags of a tree decomposition of G) – Choose a bag that contains s and t – Make a nice tree decomposition with root bag precisely {s,t} – The distance of s to t equals B(s,t) • O(nk2) time 18 Shortest paths and treewidth Checking for negative cycles • Claim: there is a negative cycle, if and only if there are two vertices (that appear together in a bag) v, w with B(v,w) + B(w,v) < 0 – : trivial – : take a negative cycle C. Let v and w be the two lowest numbered vertices on the cycle • Take a node i of minimum distance to root bag that contains two vertices of C. All vertices of C must belong to bag i or a descendant of i. (If not, then C would have a separator of size at most 1, namely C intersected with the bag of the parent of i.) 19 Shortest paths and treewidth All Pairs Shortest Path algorithm Step 1 • Compute the values B(v,w) as discussed • Check if there is a negative cycle, as discussed • If there is no negative cycle, we do Step 2 20 Shortest paths and treewidth Step 2 - Scheme • Scheme: • For q = 1 to n do – Suppose nb(v) = q – Compute d(v,w) and d(w,v) correctly for all w with nb(w) < q 21 Shortest paths and treewidth Step 2 - Code • For all v,w, initialize d(v,w) = • For all v, initialize d(v,v) = 0 • For q = 1 to n do – Suppose nb(v) = q – For all w in XTB(v) , w v do • For r = 1 to q-1 do – Suppose nb(x) = r – d(x,v) = min { d(x,v), d(x, w) + B(w,v) } – d(v,x) = min { d(v,x), B(v,w) + d(w,x) } • Return d 22 Shortest paths and treewidth Correctness • Induction hypothesis: – For all v, w with nb(v) < q and nb(w) < q, d(v,w) equals the length of the shortest path from v to w • From standard shortest path algorithms analysis, it is clear that always – d(x,v) distance from x to v – d(v,x) distance from v to x 23 Shortest paths and treewidth To show the IH • No negative cycles: d(v,v) = 0 is correct • We must argue that after the round working with v with nb(v) = q, all values d(v,x) and d(x,v) for nb(x) < q are correct • Our proof looks at the values d(x,v); the other case d(v,x) is similar • Consider one vertex x with nb(x) < nb(v) 24 Shortest paths and treewidth Proof of correctness (1) • nb(x) < nb(v) • Note: top bag of x is not a descendant of top bag of v • Consider the shortest path P from x to v. • Claim 1: P contains another vertex (v) from TB(v) – – – – 25 Say, last arc of P is (y,v) y belongs to subtree of v x belongs to bags not in subtree of v TB(x) contains a vertex of part of P from x to y Shortest paths and treewidth Proof of correctness 2 • Look at the vertices y on P that belong to the top bag of v • Let w be the last vertex of this type on P • Note: TB(w) is ancestor of TB(v), so nb(w) < nb(v) x… x w v xw 26 TB(x) Shortest paths and treewidth Proof of correctness (2) Claim 2: d(x,v) = d(x, w) + B(w,v) • When we work with v, the value d(x,w) (first part of path) was already correctly computed, as nb(x) = r < q = nb(v) and nb(w) < nb(v) • Second (blue) part of path uses only higher numbered vertices: by DPC-subroutine, given in B(w,v) x 27 w v Shortest paths and treewidth Proof of correctness (3) • w is in XTB(v) • So, d(x,v) will be set to the min of its old value at the length of P when we consider w • After this step, equal to distance • • • • For r = 1 to q-1 do – Suppose nb(x) = r – d(x,v) = min { d(x,v), d(x, w) + B(w,v) } – d(v,x) = min { d(v,x), B(v,w) + d(w,x) } • 28 For all v,w, initialize d(v,w) = For all v, initialize d(v,v) = 0 For q = 1 to n do – Suppose nb(v) = q – For all w in XTB(v) , w v do Return d Shortest paths and treewidth Time • If we have a tree decomposition of width k: • O(n k2) for DPC • O(n2 k) for 2nd step • Total: O(n2 k) time • • • • For r = 1 to q-1 do – Suppose nb(x) = r – d(x,v) = min { d(x,v), d(x, w) + B(w,v) } – d(v,x) = min { d(v,x), B(v,w) + d(w,x) } • 29 For all v,w, initialize d(v,w) = For all v, initialize d(v,v) = 0 For q = 1 to n do – Suppose nb(v) = q – For all w in XTB(v) , w v do Return d Shortest paths and treewidth Remarks • Most results for treewidth have faster algorithms for (NP-) hard problems • This one: faster for polynomial time solvable problem • Experiments by Plancken show results are practical 30 Shortest paths and treewidth