1 Sadna in Algorithms Haim Kaplan and Svetlana Olonetsky Tel Aviv University, fall 07/08
2 Suggested topics Shortest path Maximum flow Min cost flow
3 What do you have to do: Choose algorithms to implement Understand them well Choose the graphs you’d run on The structure/language of your program What is the purpose ? Why do you do it ? There should be a goal, something that you do not know in advance, interesting graph classes
4 Inspiration There has been a lot of work recently on so called “algorithm engineering”: Three yearly conferences: 1)WAE 2)ALENEX 3)ESA (track B)
5 Some examples
6 Distances and Shortest Paths u v
7 There are many variations graph is undirected/directed weights: negative/nonnegative real/integer given pair(s)/single source/all pairs one shot/build a data structure or spaner exact/approximate randomized/deterministic
8 Dijkstra’s shortest path algorithm Let G = (V,E) be a weighted (weights are non-negative) undirected/directed graph, let s V. Want to find the distance (length of the shortest path), d(s,v) from s to every other vertex. s
9 Dijkstra: Maintain an upper bound d(v) on d(s,v). Every vertex is either scanned, labeled, or unlabeled. Initially: d(s) = 0 and d(v) = for every v s. s is labeled and all others are unlabeled. Pick a labeled vertex with d(v) minimum. Make v scanned. For every edge (v,w) if d(v) + w(v,w) < d(w) then 1) d(w) := d(v) + w(v,w) 2) label w if it is not labeled already
10 Dijkstra’s shortest path algorithm (implementation) Maintain the labeled vertices in a heap, using d(v) as the key of v. We perform n delete-min operations and n insert operations on the heap. O(n log(n)) For each edge we may perform a decrease-key. With regular heaps O(m log (n)). But if you can do decrease-key in O(1) time then you can implement Dijkstra’s algorithm to run in O(n log(n) + m) time !
11 Dial’s implementation Assume that weights are small integers Maintain vertices in an array according to their distance label
12 An Example Initialize distance labels 0 Initialize array 1
13 Scan vertex
14 Scan vertex
15 Scan vertex
16 Analysis Let C-1 be the maximum length of an arc What should be the length of the array ? What is the running time ? How can you do better ?
17 2-level buckets 0 C
18 2-level buckets 0 C
19 2-level buckets 0 C
20 Analysis What is the running time ? How can you do better ?
21 Goldberg et al Has done extensive experimental work on Dijkstra’s algorithm It may be interesting to repeat some of them ? Run on different graph classes, check new ideas, etc
22 k simple shortest paths a b e f t s c d g
23 Maintain a trie that described the paths found so far a b e f t s c d g t s sgt
24 The heap a b e f t s c d g t s sgt sabeft sgft
25 a b e f t s c d g t s sg sabeft g t gt gft
26 a b e f t s c d g t s sg g t gt gft t sabeft
27 a b e f t s c d g t s sg g t gt gft t sabeft saceft
28 a b e f t s c d g t s sg g t gt gft t t sa abeft a aceft sabdft sacdft
29 a b e f t s c d g t s sg g t gt gft t t sa a aceft sacdft t b ab bdft beft
30 a b e f t s c d g t s sg g t gt gft t t sa a aceft sacdft t b ab bdft beft
31 Point to point shortest paths
32 Running Dijkstra...
33 Bidirectional Dijkstra
34 Define potential function π(v) and modify lengths: – ℓ(v,w) = c(v,w) − π(v) + π(w) – ℓ(v,w): reduced cost of arc (v,w). All s-t paths change by same amount: π(t) − π(s). A* search: – Equivalent to Dijkstra on the modified graph: correct if ℓ(v,w) ≥ 0 (π is feasible). Take π(v) to be lower bounds on dist(v, t) A* search
35 Bidirectional A* Could be made to work Need to be careful: lower bounds must be consistent
36 Where do we get lower bounds ? Use landmarks dist(v,w) ≥ dist(A,w) − dist(A,v) dist(v,w) ≥ dist(v,A) − dist(w,A) A v w A v w
37 Query with landmarks
38 Landmark selection There has been research on it: –selection at preprocessing –selection for the query
39 Reach Reach of v with respect to P: reach(v, P) = min{ dist(s, v), dist(v, t) } Reach of v with respect to the whole graph: reach(v) = max P {reach(v, P)}, over all shortest paths P that contain v [Gutman’04]. Intuition: – vertices on highways have high reach; – vertices on local roads have low reach. v t s
40 How do you use reaches While scanning an edge (v,w): – If reach(w) < min{d(s, v) + ℓ(v,w), LB(w, t)}, then w can be pruned. t s w The lower bound is natural if the search is bidirectional v
41
42 Shortcuts Consider a sequence of vertices of degree two on the path below: – they all have high reach; s s t
43 Shortcuts s s t Add a shortcut: – single edge bypassing a path (with same length). – assume ties are broken by taking path with fewer nodes.
44 Shortcuts s s t Decrease reaches
45 Shortcuts s s t Can add more nested shortcuts 40
46 Shortcuts s s t Can add more nested shortcuts 40
47 Reaches + shortcuts
48 Combine reaches+shortcuts and landmarks
49 Spanners Let G be a weighted undirected graph. A subgraph H of G is a t -spanner of G iff u,v G, H (u,v) t G (u,v). Awerbuch ’85 Peleg-Schäffer ‘89
50 Example
51 t-spanner Computing a t-spanner of smallest possible size is NP-complete. For a graph on n vertices, how large can a t-spanner be ? u v 2-spanner may require Ω(n 2 ) edges
52 Theorem For every k>1, every weighted undirected graph on n vertices has a (2k-1)-spanner with at most n 1+1/k edges. Tight for k=1,2,3,5. Conjectured to be tight for any k equivalent to a girth conjecture of Erdös.
53 Proof/Algorithm: Consider the edges in non-decreasing order of weight. If an edge closes a cycle of length ≤ 2k discard it, otherwise add it. The resulting graph is a (2k-1)-spanner and it does not contain a cycle of length ≤ 2k. Such graph has at most n 1+1/k edges. [Althöfer, Das, Dobkin, Joseph, Soares ‘93]
54 If |cycle| 2k, then red edge can be removed.
55 k=2, Baswana & Sen
56 k=2, Baswana & Sen
57 k=2, Baswana & Sen
58 k=2, Baswana & Sen
59 k=2, Baswana & Sen
60 k=2, Baswana & Sen
61 k=2, Baswana & Sen
62 k=2, Baswana & Sen
63 k=2, Baswana & Sen
64 k=2, Baswana & Sen
65 k=2, Baswana & Sen
66 k=2, Baswana & Sen
67 k=2, Baswana & Sen
68 Analysis What is the average # of edges in the spanner ?
69 Correctness Why is this a 3-spanner ? Show: Where the weight of the red edges is smaller than the weight of the green one
70 Correctness This clearly holds if the edge became green at the first stage:
71 Correctness Otherwise, an edge (u,v’) must exist u v x
72 Correctness Otherwise, an edge (u,v’) must exist u v x v’
73 Implementation
74 (a,b)-Spanners Let G be an unweighted undirected graph. A subgraph H of G is an (a,b)-spanner of G iff u,v G, H (u,v) a G (u,v) + b. (Dor-Halperin-Zwick ’96, a=1) Peleg-Elkin ’01 Baswana, Kavitha, Mehlhorn, Pettie ‘05 Thurup Zwick ‘06
75 Find interesting graph classes to test spanners on ? Should be relatively dense…
76 Other SP problems Distance oracles All pairs shortest paths geometric shortest paths/spanners time dependent shortest paths