Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.

Similar presentations


Presentation on theme: "1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science."— Presentation transcript:

1

2 1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science

3 2 Topic Overview w Review of Graphs w Unweighted Shortest Path Problem w Positive Weighted Shortest Path Problem

4 3 Review of graphs w A graph represents relationship among items. w A graph consists of a set of vertices and a set of edges that connect the vertices. G = (V, E)

5 4 Review of graphs (cont.) w V:the set of vertices (or nodes) w E:the set of pairs of edges that connect the vertices V2V2 V3V3 V0V0 V1V1 Vertex Edge

6 5 Example: w computing the fastest route through mass transportation w computing the fastest way for routing electronic mail through a network of computers

7 6 Optimization Problem w Input: X w Output: Y subject to: an objective function f(Y) is optimized in all possible Y's satisfying constraints^^^^^^^^^^^^^ feasible solutions w The greedy method regards an optimization problem as finding a sequence of decisions that optimizes the objective function.

8 7 Selection Function ------------------ To measure "how promising" each candidate for a decision leads to an optimal solution Greedy Method ------------- At each stage of decision, choose a candidate which has the largest value of the selection function.

9 8

10 9

11 10 Who discovered the shortest path algorithm? algorithm? Edsger Wybe Dijkstra was born 1930. He began programming computers in the early 1950s at the University of Leiden. Dijkstra has been one of the most forceful proponents of programming as a scientific discipline. He has made fundamental contributions to the areas of operating systems, including deadlock avoidance; programming languages including the notion of structured programming and algorithm.

12 11

13 12

14 13

15 14

16 15

17 16

18 17

19 18

20 19

21 20

22 21

23 22

24 23 Traces the Dijkstra’s shortest path algorithm path algorithm 0 1 2 3 4 82 1 9 43 2 1 7 A weighted directed graph

25 24 Traces the Dijkstra’s shortest path algorithm path algorithm 0 1 2 3 4 0123401234  8  9 4   1    2  3    2  7   1   (Adjacency matrix)

26 25 Traces the Dijkstra’s shortest path algorithm path algorithm Step1: S initially contains vertex 0, and W is initially the first row of the graph’s adjacency matrix, shown in the previous slides.

27 26 Traces the Dijkstra’s shortest path algorithm path algorithm Step 2: W[4] = 4 is the smallest value in W, ignoring W[0] because 0 is in S. Thus, v = 4, so add 4 to S. For vertices not in S, that is, for u = 1, 2, and 3, check whether it is shorter to go from 0 to 4 and then along an edge to u instead of directly from 0 to u along an edge. For vertices 1 and 3, it is not shorter to include vertex 4 in the path.

28 27 Traces the Dijkstra’s shortest path algorithm path algorithm Step 2 (continue): However, for vertex 2 notice that W[2] =  > W[4] + A[4][2] = 4 + 1 = 5. Therefore, replace W[2] with 5. You can also verify this conclusion by examining the graph directly.

29 28 Traces the Dijkstra’s shortest path algorithm path algorithm Step2: The path 0 - 4 - 2 is shorter than 0 - 2. 0 2 4  4 1

30 29 Traces the Dijkstra’s shortest path algorithm path algorithm Step 3: W[2] = 5 is the smallest value in W, ignoring W[0] and W[4] because 0 and 4 are in S. Thus, v = 2, so add 2 to S. For vertices not in S, that is for u = 1 and 3, check whether it is shorter to go to u along as edge. Notion that W[1] = 8 > W[2] + A[2][1] = 5 +2 = 7. Therefore, replace W[1] with 7.

31 30 Traces the Dijkstra’s shortest path algorithm path algorithm Step 3: The path 0 - 4 - 2 - 1 is shorter that 0 - 1. 01 2 4 8 2 4 1

32 31 Traces the Dijkstra’s shortest path algorithm path algorithm Step 3 (continue): The path 0 - 4 - 2 - 3 is shorter than 0 - 3. 0 34 2 4 3 1 9

33 32 Traces the Dijkstra’s shortest path algorithm path algorithm Step 4: W[1] = 7 is the smallest value in W, ignoring W[0], W[2], and W[4] because 0, 2 and 4 are in S. Thus, v = 1, so add 1 to S. For vertex 3, which is the only vertex not in S, notion that W[3] = 8 < W[1] + A[1][3] = 7 + . Therefore, leave W[3] as it is.

34 33 Traces the Dijkstra’s shortest path algorithm path algorithm Step 4 (continue): The path 0 - 4 - 2 - 3 is shorter than 0 - 4 - 2 - 1 - 3. 012 3 4 4 2 3 1 

35 34 Traces the Dijkstra’s shortest path algorithm path algorithm Step 5 : The only remaining vertex not in S is 3, so add it to S and stop.

36 35 Traces the Dijkstra’s shortest path algorithm path algorithm Step V S W[0] W[1] W[2] W[3] W[4] 1 - 0 0 8  9 1 2 4 0, 4 0 8 5 9 4 3 2 0,4,2 0 7 5 8 4 4 1 0,4,2,1 0 7 5 8 4 5 3 0,4,2,1,3 0 7 5 8 4

37 36 Exercise of finding the shortest path between A & Z a b c d e z 4 2 1 5 8 10 6 2 3 a--> c--> b--> d--> e--> z 0 + 2 + 1 + 5 + 2 + 3 = 13

38 37 Theory w We will use Dijkstra’s algorithm to solve the weighted shortest path problem. w We can also use the same algorithm to solve the unweighted shortest path problem by considering its edges’ weights as 1. w Under this algorithm, we use breadth-first search to process vertices in layers: the closest to the start are evaluated first.

39 38 Unweighted Shortest Path Problem w To find the shortest path (measured by number of edges) from a designed vertex S to every vertex. V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 We will use the breadth-first search to process vertices in layers. We will use a tool called eyeball to travel from vertex to vertex. We will use V 2 as the starting vertex S. Eyeball at V 2 initially S

40 39 Unweighted Shortest Path Problem We define D i be the length of the shortest path from S to i. FSo, D S = 0 Fand we set D i =  initially for all i  S If v is the vertex that the eyeball is currently on, then, for all w that are adjacent to v, we set D w = D v + 1 if D w =  Since the eyeball is currently on V 2, we set: D V0 = 0 + 1 = 1 & D V5 = 0 + 1 = 1 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0      S

41 40 Unweighted Shortest Path Problem Then, we move the eyeball to V 0. From V 0, we can find vertices whose shortest path from S is exactly 2, they are V 1 and V 3. V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 1    1  V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12  2 1  S

42 41 Unweighted Shortest Path Problem After we have processed all of V 0 ’s adjacent vertices (V 1 & V 3 ), we move the eyeball to V 5 which has the same distance from vertex S as V 0 does. Since V 5 does not have any out-edge, we move the eyeball to V 1 whose length is larger than V 5 by 1. V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12  2 1  V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12 3 2 1  S

43 42 Unweighted Shortest Path Problem From V 1, we can find vertices whose shortest path from S is exactly 3, it is V 4. There is also an edge from V 1 to V 3. However, we do not change D V3 since V 3 had already been processed. After we have processed all of V 1 ’s adjacent vertices (V 4 ), we move the eyeball to V 3 and set D V6 to 3. V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12 3 2 1  V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12 3 2 1 3 S

44 43 Unweighted Shortest Path Problem After we have processed all of V 3 ’s adjacent vertices (V 6 ), we move the eyeball to V 4. From V 4, we look for any unprocessed vertex. We cannot find any. Then, we move the eyeball to V 6. From V 6, we look for any unprocessed vertex again. Since we cannot find another vertex to move the eyeball, we are done. V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12 3 2 1 3 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0 12 3 2 1 3 S

45 44 Positive Weighted Shortest Path Problem First, we start from V 0 and consider it as vertex S. Then, we define D i be the length of the shortest path from S to i. So, D S = 0 and we set D i =  initially for all i  S V2V2 V5V5 V6V6 V4V4 V3V3 V1V1       V0V0 0 S 4 1 2 3 10 1 4 22 8 5 6 FrontRear Priority Queue Moreover, we use a priority queue to store the vertices needed to be visited next. Every time the D i changes, its vertex will be put into the priority queue.

46 45 Positive Weighted Shortest Path Problem From V 0, we can find the adjacent vertices V 3 and V 1. We put them into the priority queue and update their lengths (D i ) from  to 1 and 2 respectively. Every time D i changes to a smaller number, we put the vertex into the priority queue. V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1  2  1   4 1 2 3 10 1 4 22 8 5 6 V1V1 V3V3 FrontRear Priority Queue S

47 46 Positive Weighted Shortest Path Problem Then, we move the eyeball to V 3 according to the priority queue. From V 3, we can find the adjacent vertices V 4, V 2, V 6, and V 5. We put them into the priority queue and update their lengths (D i ). V2V2 V5V5 V6V6 V4V4 V3V3 V1V1  2  1   V0V0 0 S 4 1 2 3 10 1 4 22 8 5 6 V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 1 4 22 8 5 6 V1V1 V3V3 FrontRear Priority Queue V5V5 V2V2 V6V6 V4V4 V1V1 FrontRear Priority Queue

48 47 Positive Weighted Shortest Path Problem Then, we move the eyeball to V 1 according to the priority queue. From V 1, we find the adjacent vertices V 4 and V 3. However, since their new D i from V 1 are not less than their previous D i, no updating is performed. V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 V0V0 0 S V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 1 4 22 8 5 6 V5V5 V2V2 V6V6 V4V4 V1V1 FrontRear Priority Queue V6V6 V5V5 V2V2 V4V4 FrontRear Priority Queue

49 48 V6V6 V5V5 V2V2 V4V4 FrontRear Priority Queue Positive Weighted Shortest Path Problem After that, we move the eyeball to V 4. From V 4, we find the adjacent vertices V 6. However, since its new D i (9) from V 4 is not less than their previous D i (5), no updating is performed. V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 V0V0 0 S V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 1 4 22 8 5 6 V5V5 V6V6 V2V2 FrontRear Priority Queue

50 49 V5V5 V6V6 V2V2 FrontRear Priority Queue Positive Weighted Shortest Path Problem Then, we move the eyeball to V 2. From V 2, we find the adjacent vertices V 5. Since its new D i (8) from V 2 is less than its previous D i (9), we update the value and put V 5 into the priority queue again for next visit. V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 V0V0 0 S V5V5 V5V5 V6V6 FrontRear Priority Queue V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 8

51 50 V5V5 V5V5 V6V6 FrontRear Priority Queue Positive Weighted Shortest Path Problem Then, we move the eyeball to V 6. From V 6, we find the adjacent vertices V 5. Since its new D i (6) from V 6 is less than its previous D i (8), we update the value and put V 5 into the priority queue again for next visit. V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 8 V0V0 0 S V5V5 V5V5 V5V5 FrontRear Priority Queue V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 8 6

52 51 V5V5 V5V5 V5V5 FrontRear Priority Queue Positive Weighted Shortest Path Problem Finally, we move the eyeball to V 5. There is no out-edge in V 5, so we cannot find any adjacent vertices. Therefore, the eyeball is relocated to V 5 two more time until the priority queue is cleared. Then, we are done. V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 8 6 V0V0 0 S FrontRear Priority Queue V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 3 2 3 1 9 5 4 1 2 3 10 1 4 22 8 5 6 8 6

53 52

54 53

55 54

56 55

57 56

58 57

59 58

60 59

61 60 Various Graph Problems ------------------------- Maximum Network Flow O(e n log n) Bipartite Matching O(n^{5/2}) Planarity O(n) Graph Isomorphism Graph Coloring Maximum Clique Maximum Independent Set


Download ppt "1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science."

Similar presentations


Ads by Google