760 likes | 770 Views
Explore and implement algorithms related to shortest path, maximum flow, and min cost flow problems. Discover interesting graph classes and unknown solutions.
E N D
Sadna in Algorithms Haim Kaplan and Svetlana Olonetsky Tel Aviv University, fall 07/08
Suggested topics • Shortest path • Maximum flow • Min cost flow
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
Inspiration There has been a lot of work recently on so called “algorithm engineering”: • Three yearly conferences: • WAE • ALENEX • ESA (track B)
v u Distances and Shortest Paths
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
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. 3 3 s 2 1 3 2
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
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 !
Dial’s implementation • Assume that weights are small integers • Maintain vertices in an array according to their distance label
0 1 An Example Initialize distance labels Initialize array 4 2 4 2 2 2 1 3 1 6 4 2 3 3 5
Scan vertex 1 2 4 2 4 2 2 0 2 1 3 1 6 4 2 3 3 5 4 1 3 2
2 Scan vertex 2 2 4 2 4 2 2 0 2 1 3 1 6 4 2 3 3 5 4 3 1 3 2
2 Scan vertex 2 2 4 2 4 2 2 0 2 1 3 1 6 4 2 3 3 5 4 4 3 1 2 3 5
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 ?
2-level buckets 0 C-1 ......... .........
2-level buckets 0 C-1 ......... .........
2-level buckets 0 C-1 ......... .........
Analysis • What is the running time ? • How can you do better ?
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
k simple shortest paths s a g b c d e f t
Maintain a trie that described the paths found so far s s a sgt g t b c d e f t
The heap s s a sgt g t b c d e f sabeft sgft t
s s sg a g gft gt g t b t c d e f sabeft t
s s sabeft sg a t g gft gt g t b t c d e f t
s s sabeft sg a t g gft gt g t b t c d e f saceft t
s sa s a sg aceft a g abeft gft gt g t t b t t c d e f sabdft sacdft t
s sa s a sg aceft a g ab gft gt b g t t b t c bdft beft d e t t f sacdft t
s sa s a sg aceft a g ab gft gt b g t t b t c bdft beft d e t t f sacdft t
A* search • 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)
Bidirectional A* • Could be made to work • Need to be careful: lower bounds must be consistent
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 A v v w w
Landmark selection • There has been research on it: • selection at preprocessing • selection for the query
Reach s t v • 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) = maxP {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.
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. w v s t • The lower bound is natural if the search is bidirectional
Shortcuts • Consider a sequence of vertices of degree two on the path below: • – they all have high reach; 1000 1000 10 10 10 10 10 10 10 10 s s 1000 t 1010 1000 1020 1030 1040 1020 1010 1030
Shortcuts Add a shortcut: – single edge bypassing a path (with same length). – assume ties are broken by taking path with fewer nodes. 80 1000 1000 10 10 10 10 10 10 10 10 s s 1000 t 1010 1000 1020 1030 1040 1020 1010 1030
Shortcuts Decrease reaches 80 1000 1000 10 10 10 10 10 10 10 10 s s 1000 t 60 1000 50 30 50 60 40 40
Shortcuts Can add more nested shortcuts 80 1000 1000 40 40 10 10 10 10 10 10 10 10 s s 1000 t 60 1000 50 30 50 60 40 40
Shortcuts Can add more nested shortcuts 80 1000 1000 40 40 10 10 10 10 10 10 10 10 s s 1000 t 20 1000 10 30 10 20 20 20
Spanners Let G be a weighted undirected graph. A subgraph H of G is a t-spanner of G iff u,vG, H(u,v) tG(u,v) . Awerbuch ’85 Peleg-Schäffer ‘89