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!