# Shortest Paths and Treewidth Algorithms and Networks.

## Presentation on theme: "Shortest Paths and Treewidth Algorithms and Networks."— Presentation transcript:

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(nk 2 ) time solve the Single Pair Shortest Paths problem –In O(nk 2 ) time determine if there is a negative cycle –In O(n 2 k) time solve the All Pairs Shortest Paths problem Shortest paths and treewidth2

Contents Short background Nice tree decompositions and top bags Directed Path Consistency on nice tree decompositions Main algorithm Conclusions Shortest paths and treewidth3

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 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 treewidth4

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 X i = X j = X j’. –Introduce: node with one child j with X i = X j  {v} for some vertex v –Forget: node with one child j with X i = X j  {v} for some vertex v 5

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. Shortest paths and treewidth6

Top bags For each vertex v, there is a exactly one bag i with v in X i and v not in X parent(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 Shortest paths and treewidth7

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) Shortest paths and treewidth8

A property of tree decompositions If P is a path from v to w, then X contains at least one vertex from P Shortest paths and treewidth X v v w w 9

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 X i we compute B i (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) Shortest paths and treewidth10

Plan A Dynamic Programming algorithm computes a table B i for all bags i, each in O(k 2 ) time Note that we must compute O(k 2 ) values for each bag: O(1) time per computation Procedure for each of the four types of nodes Total time is O(nk 2 ) for these steps Shortest paths and treewidth11

Shortest paths and treewidth Leaf nodes Let i be a leaf node. For all v,w in X i : B i (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 X i : B i (v,w) = min (B j (v,w), B j’ (v,w) ) + = Why? 13

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 X i. –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 X i or have an edge from a vertex in the subtree of j to a vertex in the subtree of j’ Shortest paths and treewidth14

Shortest paths and treewidth Introduce nodes Let i be a node with child j, with X i = X j  {v}. For all w,x in X j : –B i (v,w) = length of arc (v,w) if existing,  otherwise –B i (w,x) = B j (w,x) v Trivial case 15

Shortest paths and treewidth Forget nodes Let i be a node with child j, with X i = X j  {v}. For all w,x in X i : –B i (w,x) = min (B j (w,x), B j (w,v)+B j (v,x)) v v The path now can use v as internal vertex w x v 16

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) = B TB(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) = B TB(w) (v,w) Shortest paths and treewidth17

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(nk 2 ) time Shortest paths and treewidth18

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.) Shortest paths and treewidth19

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 Shortest paths and treewidth20

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 Shortest paths and treewidth21

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 X TB(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 Shortest paths and treewidth22

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 Shortest paths and treewidth23

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) Shortest paths and treewidth24

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) –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 treewidth25

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) Shortest paths and treewidth xwv x w TB(x) x … 26

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) Shortest paths and treewidth xwv 27

Proof of correctness (3) w is in X TB(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 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 X TB(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 Shortest paths and treewidth28

Time If we have a tree decomposition of width k: O(n k 2 ) for DPC O(n 2 k) for 2 nd step Total: O(n 2 k) time 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 X TB(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 Shortest paths and treewidth29

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 Shortest paths and treewidth30