1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25 2 39 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,

Slides:



Advertisements
Similar presentations
Spring 2007Shortest Paths1 Minimum Spanning Trees JFK BOS MIA ORD LAX DFW SFO BWI PVD
Advertisements

1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
Shortest Paths1 C B A E D F
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
© 2004 Goodrich, Tamassia Shortest Paths1 Shortest Paths (§ 13.6) Given a weighted graph and two vertices u and v, we want to find a path of minimum total.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
CSC311: Data Structures 1 Chapter 13: Graphs II Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
Shortest Paths1 C B A E D F
© 2004 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Dijkstra's algorithm.
Lecture20: Graph IV Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Shortest Paths C B A E D F
Graphs Part 2. Shortest Paths C B A E D F
1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
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.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
Shortest Paths 1 Chapter 7 Shortest Paths C B A E D F
Lecture 16. Shortest Path Algorithms
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Dijkstra’s Algorithm Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
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.
Paths in a Graph : A Brief Tutorial Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
1 Weighted Graphs CSC401 – Analysis of Algorithms Lecture Notes 16 Weighted Graphs Objectives: Introduce weighted graphs Present shortest path problems,
Data Structures and Algorithms1 Data Structures and algorithms (IS ZC361) Weighted Graphs – Shortest path algorithms – MST S.P.Vimal BITS-Pilani
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 COMP9024: Data Structures and Algorithms Week Twelve: Graphs (II) Hui Wu Session 1, 2014
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Shortest Paths C B A E D F Shortest Paths
Graphs Part 2.
COMP9024: Data Structures and Algorithms
Shortest Paths C B A E D F Shortest Paths
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Shortest Paths C B A E D F Shortest Paths 1
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Tree 11/3/ :04 AM Minimum Spanning Tree
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F
CMSC 341 Lecture 21 Graphs (Introduction)
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Chapter 13 Graph Algorithms
Graphs Part 2.
Graph Algorithms shortest paths, minimum spanning trees, etc.
Minimum Spanning Trees
Single-source shortest paths
Copyright © Aiman Hanna All rights reserved
Shortest Paths.
Weighted Graphs C B A E D F Sequences
Presentation transcript:

1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information, Computer and Communication Technology (ICT) Sirindhorn International Institute of Technology (SIIT) Thammasat University X 2005

2 ITS033 Topic 01-Problems & Algorithmic Problem Solving Topic 01 - Problems & Algorithmic Problem Solving Topic 02 – Algorithm Representation & Efficiency Analysis Topic 03 - State Space of a problem Topic 04 - Brute Force Algorithm Topic 05 - Divide and Conquer Topic 06-Decrease and Conquer Topic 06 - Decrease and Conquer Topic 07 - Dynamics Programming Topic 08 - Transform and Conquer Topic 09 - Graph Algorithms Topic 10 - Minimum Spanning Tree Topic 11 - Shortest Path Problem Topic 12 - Coping with the Limitations of Algorithms Power Midterm

3 Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge. Edge weights may represent, distances, costs, etc.

4 Weighted Graphs Example:  In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports ORD PVD MIA DFW SFO LAX LGA HNL

5 Shortest Path Problem Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v. Length of a path is the sum of the weights of its edges.

6 Shortest Path Problem Example: Find the Shortest path between SFO and MIA ORD PVD MIA DFW SFO LAX LGA HNL

7 Shortest Path Problem Example:  Shortest path between HNL and PVD ORD PVD MIA DFW SFO LAX LGA HNL

8 Shortest Path Problem Applications  Internet packet routing  Flight reservations  Driving directions

9 Internet as a graph

10 Shortest Path Properties Property 1: A subpath of a shortest path is itself a shortest path Property 2: There is a tree of shortest paths from a start vertex to all the other vertices

11 Shortest Path Properties Example: Tree of shortest paths from PVD ORD PVD MIA DFW SFO LAX LGA HNL

12 Shortest Path Problem: Dijkstra Algorithm Topic 11.1 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information, Computer and Communication Technology (ICT) Sirindhorn International Institute of Technology (SIIT) Thammasat University X 2005

13 Dijkstra’s Algorithm single-source shortest-paths problem: for a given vertex called the source in a weighted connected graph, find shortest paths to all its other vertices. The single-source shortest-paths problem asks for a family of paths, each leading from the source to a different vertex in the graph, though some paths may, of course, have edges in common. The best-known algorithm for the single-source shortest- paths problem,: Dijkstra’s algorithm. However, this algorithm is applicable to graphs with nonnegative weights only.

14 Shortest-Path Algorithm Single shortest path problem: Given as input a weighted graph, G = (V,E), and a distinguished vertex, s, find the shortest weighted path from s to any vertex in G.

15 Dijkstra’s Algorithm Assumptions:  the graph is connected  the edges are undirected (or directed)  the edge weights are nonnegative

16 Dijkstra’s Algorithm The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s

17 Dijkstra’s Algorithm Idea of Dijkstra’s algorithm. The subtree of the shortest paths already found is shown in bold. The next nearest to the source v 0 vertex, u *, is selected by comparing the lengths of the subtree’s paths increased by the distances to vertices adjacent to the subtree’s vertices.

18 Dijkstra’s Algorithm First, it finds the shortest path from the source to a vertex nearest to it, then to a second nearest, and so on. Before its i th iteration commences, the algorithm has already identified the shortest paths to i - 1 other vertices nearest to the source. These vertices, the source, and the edges of the shortest paths leading to them from the source form a subtree T i of the given graph

19 Dijkstra’s Algorithm We grow a “group” of vertices, beginning with s and eventually covering all the vertices We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices

20 Dijkstra’s Algorithm To facilitate the algorithm’s operations, we label each vertex with two labels. (1) The numeric label d indicates the length of the shortest path from the source to this vertex found by the algorithm so far; when a vertex is added to the tree, d indicates the length of the shortest path from the source to that vertex. (2) Label indicates the name of the next-to-last vertex on such a path, i.e., the parent of the vertex in the tree being constructed.

21 Dijkstra’s Algorithm After we have identified a vertex u * to be added to the tree, we need to perform two operations: Move u * from the fringe to the set of tree vertices. For each remaining fringe vertex u that is connected to u * by an edge of weight w(u *, u) such that d u* + w(u *, u) < d u, update the labels of u by u * and d u* + w(u *, u), respectively.

22 Dijkstra’s Algorithm At each step  We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u)  We update the labels of the vertices adjacent to u

23 Dijkstra’s Algorithm

24 Dijkstra’s Algorithm Analysis The time efficiency of Dijkstra’s algorithm depends on the data structures used for implementing the priority queue and for representing an input graph itself. it is in θ(|V |2) for graphs represented by their weight matrix and the priority queue implemented as an unordered array. For graphs represented by their adjacency linked lists and the priority queue implemented as a min-heap, it is in O(|E| log |V |).

25 Example 2 CB A E D F  Step 1

26 Example C B A E D F Step 2

27 Example C B A E D F Step 3

28 Example C B A E D F Step 4

29 Example (cont.) CB A E D F Step 5

30 Example (cont.) CB A E D F Step 6

31 Find shortest path from Melbourne to every town ?

32 Dijkstra’s algorithm – Exercise

33 An example on Directed graph

34 Why Dijkstra’s Algorithm Works Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. CB A E D F

35 Why It Doesn’t Work for Negative- Weight Edges  If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud. CB A E D F Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance. C’s true distance is 1, but it is already in the cloud with d(C)=5!

36 Shortest Path Problem: Bellman-Ford Algorithm Topic 11.2 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information, Computer and Communication Technology (ICT) Sirindhorn International Institute of Technology (SIIT) Thammasat University X 2005

37 Bellman-Ford Algorithm The Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph (where some of the edge weights may be negative). Dijkstra's algorithm accomplishes the same problem with a lower running time, but requires edge weights to be non-negative. Thus, Bellman–Ford is usually used only when there are negative edge weights.

38 Bellman-Ford Algorithm Bellman-Ford is in its basic structure very similar to Dijkstra's algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this |V| − 1 times, where |V| is the number of vertices in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since, in the absence of negative cycles, the shortest path can only visit each node at most once. Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case.

39 Bellman-Ford Algorithm Works even with negative-weight edges Must assume directed edges (for otherwise we would have negative-weight cycles) Running time: O(nm).

40 Bellman-Ford Algorithm Algorithm BellmanFord(G, s) for all v  G.vertices() if v  s setDistance(v, 0) else setDistance(v,  ) for i  1 to n-1 do for each e  G.edges() { relax edge e } u  G.origin(e) z  G.opposite(u,e) r  getDistance(u)  weight(e) if r  getDistance(z) setDistance(z,r)

41 Bellman-Ford Example  0    Nodes are labeled with their d(v) values Step 1

42  -2 Bellman-Ford Example  0    Nodes are labeled with their d(v) values Step 2

43 Bellman-Ford Example Nodes are labeled with their d(v) values   Step 3

44 Bellman-Ford Example Nodes are labeled with their d(v) values Step 4

45 Summary Dijkstra's algorithm Dijkstra's algorithm Definition: An algorithm to find the shortest paths from a single source vertex to all other vertices in a weighted, undirected graph. All weights must be nonnegative. The time complexity O(E + V log V), where V is the number of vertices and E is the number of edges. Bellman-Ford Example Bellman-Ford Example Works even with negative-weight edges Iteration i finds all shortest paths that use i edges. Running time: O(nm).

46 Maximum Profit

47 ITS033 Topic 01-Problems & Algorithmic Problem Solving Topic 01 - Problems & Algorithmic Problem Solving Topic 02 – Algorithm Representation & Efficiency Analysis Topic 03 - State Space of a problem Topic 04 - Brute Force Algorithm Topic 05 - Divide and Conquer Topic 06-Decrease and Conquer Topic 06 - Decrease and Conquer Topic 07 - Dynamics Programming Topic 08 - Transform and Conquer Topic 09 - Graph Algorithms Topic 10 - Minimum Spanning Tree Topic 11 - Shortest Path Problem Topic 12 - Coping with the Limitations of Algorithms Power Midterm

48 End of Chapter 11 Thank you!