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

Slides:



Advertisements
Similar presentations
NP-Hard Nattee Niparnan.
Advertisements

Chapter 9: Graphs Shortest Paths
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
Chapter 3 The Greedy Method 3.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Backtracking.
The Shortest Path Problem
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Dijkstra's algorithm.
More Graph Algorithms 15 April Applications of Graphs Graph theory is used in dealing with problems which have a fairly natural graph/network.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
Nattee Niparnan. Easy & Hard Problem What is “difficulty” of problem? Difficult for computer scientist to derive algorithm for the problem? Difficult.
Representing and Using Graphs
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
CS223 Advanced Data Structures and Algorithms 1 Maximum Flow Neil Tang 3/30/2010.
CIRCUITS, PATHS, AND SCHEDULES Euler and Königsberg.
Introduction to NP Instructor: Neelima Gupta 1.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Grade 11 AP Mathematics Graph Theory Definition: A graph, G, is a set of vertices v(G) = {v 1, v 2, v 3, …, v n } and edges e(G) = {v i v j where 1 ≤ i,
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Graphs – Part III CS 367 – Introduction to Data Structures.
Shortest Paths.
By Laksman Veeravagu and Luis Barrera
CSC317 Shortest path algorithms
Shortest Path Problems
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Shortest Path.
Shortest Path.
Graphs.
Shortest Path Problems
Shortest Path Algorithms
Applied Combinatorics, 4th Ed. Alan Tucker
Shortest Path.
Graph Algorithms: Shortest Path
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Chapter 9: Graphs Shortest Paths
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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

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

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)

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

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

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.

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.

8

9

10 Who discovered the shortest path algorithm? algorithm? Edsger Wybe Dijkstra was born 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.

11

12

13

14

15

16

17

18

19

20

21

22

23 Traces the Dijkstra’s shortest path algorithm path algorithm A weighted directed graph

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

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.

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.

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] = = 5. Therefore, replace W[2] with 5. You can also verify this conclusion by examining the graph directly.

28 Traces the Dijkstra’s shortest path algorithm path algorithm Step2: The path is shorter than  4 1

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.

30 Traces the Dijkstra’s shortest path algorithm path algorithm Step 3: The path is shorter that

31 Traces the Dijkstra’s shortest path algorithm path algorithm Step 3 (continue): The path is shorter than

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.

33 Traces the Dijkstra’s shortest path algorithm path algorithm Step 4 (continue): The path is shorter than 

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.

35 Traces the Dijkstra’s shortest path algorithm path algorithm Step V S W[0] W[1] W[2] W[3] W[4]  , ,4, ,4,2, ,4,2,1,

36 Exercise of finding the shortest path between A & Z a b c d e z a--> c--> b--> d--> e--> z = 13

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.

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

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 = = 1 & D V5 = = 1 V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V0 0      S

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 V0V  2 1  S

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 V0V  2 1  V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V  S

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 V0V  V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V S

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 V0V V2V2 V5V5 V6V6 V4V4 V3V3 V1V1 V0V S

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 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.

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   V1V1 V3V3 FrontRear Priority Queue S

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 V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V V1V1 V3V3 FrontRear Priority Queue V5V5 V2V2 V6V6 V4V4 V1V1 FrontRear Priority Queue

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 V1V V0V0 0 S V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V V5V5 V2V2 V6V6 V4V4 V1V1 FrontRear Priority Queue V6V6 V5V5 V2V2 V4V4 FrontRear Priority Queue

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 V1V V0V0 0 S V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V V5V5 V6V6 V2V2 FrontRear Priority Queue

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 V1V V0V0 0 S V5V5 V5V5 V6V6 FrontRear Priority Queue V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V

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 V1V V0V0 0 S V5V5 V5V5 V5V5 FrontRear Priority Queue V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V

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 V1V V0V0 0 S FrontRear Priority Queue V0V0 0 V2V2 V5V5 V6V6 V4V4 V3V3 V1V

52

53

54

55

56

57

58

59

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