Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Data Structures and Algorithms

Similar presentations


Presentation on theme: "Fundamental Data Structures and Algorithms"— Presentation transcript:

1 15-211 Fundamental Data Structures and Algorithms
Graph Algorithms Fundamental Data Structures and Algorithms Peter Lee April 3, 2003

2 Announcements

3 Homework 5 Homework Assignment #5 is due on Tuesday, April 15, 11:59pm! The next quiz (#3) will be available tomorrow. Reading: Chapter 14.

4 Single-Source Shortest Paths

5 The Single Source Shortest Path Problem
Given: a directed graph G = (V,E) weight(u,v)  0 for all edges (u,v)  E Find: shortest path from start vertex s to every other vertex in G s f a b d e c g 4 2 5 1

6 Dijkstra’s algorithm Initialization a. Set D(s) = 0
(see Weiss, Section 14.3) Initialization a. Set D(s) = 0 b. For all vertices v  V, v  s, set D(v) =  c. Insert all vertices into priority queue P, using distances as the keys s f a b d e c g 4 2 5 1 s a b c d e f g

7 Dijkstra’s algorithm While P is not empty:
1. Select the next vertex u to visit u = P.deleteMin() 2. Update weights of each vertex w adjacent to u If D(u) + weight(u,w) < D(w), a. D(w) = D(u) + weight(u,w) b. Update the priority queue to reflect new distance for w

8 Dijkstra’s algorithm s f a b d e c g 4 2 5 1 Visited s a b c d e f g

9 Dijkstra’s algorithm b c a d e f g 2 4 5  Visited s (D = 0) s f a b d
1 Visited s (D = 0) b c a d e f g 2 4 5

10 Dijkstra’s algorithm d c a e f g 3 4 5 6  Visited s (D = 0) b (D = 2)
1 Visited s (D = 0) b (D = 2) d c a e f g 3 4 5 6

11 Dijkstra’s algorithm c a e f g 4 6  Visited s (D = 0) b (D = 2)
5 1 Visited s (D = 0) b (D = 2) d (D = 3) c a e f g 4 6

12 Dijkstra’s algorithm a e f g 4 6  Visited s (D = 0) b (D = 2)
c g 4 2 5 1 Visited s (D = 0) b (D = 2) d (D = 3) c (D = 4) a e f g 4 6

13 Dijkstra’s algorithm e f g 6  Visited s (D = 0) b (D = 2) d (D = 3)
c g 4 2 5 1 Visited s (D = 0) b (D = 2) d (D = 3) c (D = 4) a (D = 4) ... e f g 6

14 Dijkstra’s algorithm Visited s (D = 0) b (D = 2) d (D = 3) c (D = 4)
f a b d e c g 4 2 5 1 Visited s (D = 0) b (D = 2) d (D = 3) c (D = 4) a (D = 4) e (D = 6) f (D = 6) g (D = )

15 Features of Dijkstra’s Algorithm
A greedy algorithm “Visits” every vertex only once, when it becomes the vertex with minimal distance amongst those still in the priority queue Distances may be revised multiple times: current values represent ‘best guess’ based on our observations so far Once a vertex is visited we are guaranteed to have found the shortest path to that vertex…. why?

16 Correctness (via contradiction)
Assume u is the first vertex visited such that D(u) is not a shortest path (thus the true shortest path must pass through some unvisited vertex) Let x represent the first unvisited vertex on the true shortest path to u u x s visited unvisited D(x) must represent a shortest path to x, and D(x)  Dshortest(u). However, Dijkstra’s always visits the vertex with the smallest distance next, so we can’t possibly visit u before we visit x

17 Performance (using a heap)
Initialization of priority queue: O(|V|) Visitation loop: |V| calls deleteMin(): O(log|V|) Each edge is considered only once during entire execution, for a total of |E| updates of the priority queue, each O(log|V|) Overall cost: O(|V|log|V| + |E|log|V|)

18 Negative weights? Dijkstra’s greedy algorithm can only guarantee shortest paths for non-negative weights s f a b d e c g 4 2 5 - 3 1 b c a d e f g 2 4 5 visiting b incorrectly produces a path of distance 2 How can we address this problem?

19 The Bellman-Ford algorithm
(see Weiss, Section 14.4) Returns a boolean: TRUE if and only if there is no negative-weight cycle reachable from the source: a simple cycle <v0, v1,…,vk>, where v0=vk and FALSE otherwise If it returns TRUE, it also produces the shortest paths

20 Bellman-Ford algorithm
Initialization a. Set D(s) = 0 b. For all vertices v  V, v  s, set D(v) =  s f a b d e c g 4 2 5 - 3 1 - 2 s a b c d e f g

21 Bellman-Ford algorithm
Path updates and negative cycle check: 1. Do |V|-1 times: For each edge (u,v) in |E|, If D(u) + weight(u,v) < D(v) D(v) = D(u) + weight(u,v) 2. For each edge (u,v) in |E|: return false 3. Return true

22 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 1: s a b c d e f g 5 2 4

23 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 2: s a b c d e f g 5 2 4 3

24 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 2: s a b c d e f g 5 2 4 3 6

25 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 2: s a b c d e f g 5 1 4 3 6

26 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 2: s a b c d e f g 5 1 4 3 6

27 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 2: s a b c d e f g 4 1 3 6

28 Bellman-Ford path updates
Assume edges are examined in lexicographic order (i.e., (b,d), (b,e), (c,b), (c,f), (d,a), (f,e), (s,a), (s,b), (s,c) ) s f a b d e c g 4 2 5 - 3 1 - 2 Iteration 2: s a b c d e f g 4 1 3 6 etcetera...

29 Bellman-Ford cycle check
After Iteration 7: s f a b d e c g 4 2 5 - 3 1 - 2 s a b c d e f g 3 1 4 2 6 Performs one final iteration for all edges If any weights change at this point, a negative cycle exists. For this graph, the algorithm returns TRUE.

30 Key features If the graph contains no negative-weight cycles reachable from the source vertex, after |V| - 1 iterations all distance estimates represent shortest paths…why? We assumed edges were considered in the same order for each iteration. Would the algorithm still work if we changed the order for every iteration?

31 Correctness Case 1: Graph G=(V,E) doesn’t contain any negative-weight cycles reachable from the source vertex s Consider a shortest path p = < v0, v1,..., vk>, which must have k  |V| - 1 edges By induction: D(s) = 0 after initialization Assume D(vi-1) is a shortest path after iteration (i-1) Since edge (vi-1,vi) is updated on the ith pass, D(vi) must then reflect the shortest path to vi. Since we perform |V| - 1 iterations, D(vi) for all reachable vertices vi must now represent shortest paths The algorithm will return true because on the |V|th iteration, no distances will change

32 Correctness Case 2: Graph G=(V,E) contains a negative-weight cycle < v0, v1,..., vk> reachable from the source vertex s Proof by contradiction: Assume the algorithm returns TRUE Thus, D(vi-1) + weight(vi-1, vi)  D(vi) for i = 1,…,k Summing the inequalities for the cycle: leads to a contradiction since the first sums on each side are equal (each vertex appears exactly once) and the sum of weights must be less than 0.

33 Performance Initialization: O(|V|) Path update and cycle check:
|V| calls checking |E| edges, O(|VE|) Overall cost: O(|VE|)

34 STRETCH!

35 Network Flow Problems

36 Network flow problems An important application of graphs is to model flows (of items or information) from one point to another. A directed graph G = (V,E), with each edge e  E having a capacity Ce. The problem: Compute the maximum possible flow from a given source vertex to a given target vertex Edge capacities must not be violated

37 A simple example s a b c d t 3 2 1 4

38 Network flow applications
Network flow problems have many applications bandwidth in large networks traffic capacities in road networks flow in electrical circuits or water pipes

39 A possible greedy algorithm
Initial graph G We incrementally building the flow graph Gf. Gf shows the maximum flow attained thus far on each edge At the same time, build up the residual graph Gr. Gr shows, for edge, what is the remaining capacity

40 Greedy algorithm, cont’d
Initialize Gf: all edges have 0 flow Gr: initialize to G At each stage, find a path in Gr from s to t The edge with the minimum capacity is the amount of flow that can be added to every edge on that path adjust Gf and Gr accordingly Remove saturated edges from Gr

41 Greedy example, step 0 G Gf Gr s a b c d t s a b c d t s a b c d t 3 2
1 4 s a b c d t s a b c d t 3 2 1 4

42 Greedy example For each stage, we will randomly choose a path from s to t

43 Greedy example, step 1 G Gf Gr s s a b c d t s a b a b c d c d t t 2 3
2 s 3 2 3 1 1 a b a b 3 2 3 4 4 c d c d 2 3 2 1 t t

44 Greedy example, step 2 G Gf Gr s s a b c d t s a b a b c d c d t t 2 3
s 3 2 1 1 1 a b a b 3 2 1 4 4 c d c d 2 3 1 t t

45 Greedy example, step 3 G Gf Gr s s a b c d t s a b a b c d c d t t
2 1 s 3 2 1 1 a b a b 3 2 1 4 3 c d c d 2 3 t t Maximum flow of 5 units into t

46 Greedy doesn’t work! Although we have managed to compute the correct maximum flow, in general this greedy algorithm doesn’t work Suppose, for example, we started out differently…

47 Greedy failure G Gf Gr s s a b c d t s a b a b c d c d t t
3 s 3 2 2 1 1 a b a b 3 2 3 2 4 1 c d c d 2 3 2 t t Algorithm terminates with maxflow=3

48 Fixing the algorithm In order to fix this problem, we need a way to “undo” greedy choices How: Whenever we add flow x to an edge (v,w) in Gf, we will add a new edge (w,v) with capacity x to Gr This will allow the path to be undone later, if necessary

49 Correct algorithm, step 1
Gf Gr s s a b c d t 3 s 3 2 3 2 1 1 a b a b 3 3 2 3 2 4 1 c d c d 2 3 2 3 t t

50 Correct algorithm, step 2
Gf Gr s s a b c d t 3 2 s 3 2 3 2 1 1 a b a b 3 2 1 3 2 1 2 4 3 c d c d 2 3 2 3 t t Maximum flow of 5 units into t

51 Running time If all capacities are integers and the max flow is f, then at most f stages to compute the max flow Each stage requires finding a shortest path (O(|E|)) So, total running time is O(f|E|)

52 The worst case s a b t n n 1 n n
What could happen when we run the algorithm? How might this be avoided?


Download ppt "Fundamental Data Structures and Algorithms"

Similar presentations


Ads by Google