cse101_11_14_11 - attend: Amazon press release Thu 2-6...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Shortest paths 1. Bellman-Ford algorithm a. Procedure update ((u, w) in E) If dist[w] > dist[u] + l(u, w): Dist[w] = dist[u] + l(u, w0 Prev[w] = u Procedure Bellman-Ford(G, l, s) For all u in V Dist[u = inf Prev[u] = nil Dist[s] = 0 Repeat v-1 times: For all e in E Update(e) A B C D 0/nil inf/nil inf/nil inf/nil 0/nil 10/A 6/A 8/A 0/nil 10/A 2/B 5/C 0/nil 10/A 2/B 1/C b. Negative cycles The more you go around in a cycle may make it less and less, it is undefined. Distance is not well-defined in a graph with negative cycles. c. To check for a negative cycle: d. Run Bellman-Ford, but for V iterations instead of V – 1. If any dist[] changes during the last iteration, there is a negative cycle. 2. DAG shortest paths a. Edge lengths can be positive or negative. b. Procedure DAG-shortest paths(G, l, s) For all u in V: Dist[u] = inf Dist[s] = 0 For each u in V in top. Order: For all (u, w) in E: Update (u, w) O(V + E) 3. Greedy Algorithms a. Interval scheduling You are a journalist covering tech news. There are many events you want to
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: attend: Amazon press release Thu 2-6 Steve Jobs memorial Wed 3-7 Nvidia open house Thurs 1-4 You want to attend as many of these as possible, but they overlap so you cant attend them all b. Interval scheduling Given: a collection of n intervals [s1, e1], [s2, e2], , [sn, en] Find the maximum # of intervals that are disjoint c. A greedy algorithm builds up a solution piece by piece, always picking the piece that looks best at the moment d. Idea 1: always pick the interval which is valid and has the earliest starting time (BAD) Idea 2: pick the shortest event (BAD) Idea 3: pick the event with the earliest finish time (this works) e. Code: Sort intervals by ending time, so that e1 <= e2 <= <= en T = -inf busy up to time T) For I = 1 to n: If si >T: Pick event [wi, ei] T = ei O(n log n) to sort. f....
View Full Document

Page1 / 2

cse101_11_14_11 - attend: Amazon press release Thu 2-6...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online