Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC317 Shortest path algorithms

Similar presentations


Presentation on theme: "CSC317 Shortest path algorithms"— Presentation transcript:

1 CSC317 Shortest path algorithms
So far we only looked at unweighted graphs. But what if we need to account for weights (and on top of it negative weights)? Definition of a shortest path problem: We are given a weighted graph G = (V,E) with a weight function mapping edges to real-valued values. The weight w(p) of path is the sum of the weights of its constituent weights We define the shortest path weight d(u,v) from u to v by if there exists a path from u to v otherwise A shortest path from u to v is then defined as any path with weight w(p) = d(u,v) CSC317

2 Variants: Single destination shortest path problem: Shortest paths to a given destination vertex t from each vertex v (what happens when we reverse edges?) Single pair shortest path problem: Shortest path from u to v for given u, v All pairs shortest path problem: Shortest path from u to v for all u, v Optimal substructure of shortest paths Shortest-paths algorithms typically rely on the property that a shortest path between two vertices contains other shortest paths within it (does that sound familiar?). Optimal substructure is one key element of dynamic programming and greedy algorithms. Dijkstra algorithm is a greedy algorithm Floyd-Warshall algorithm is a dynamic programming algorithm CSC317

3 CSC317 Negative weights: (what is the problem?)
Dijkstra’s algorithm only works on positive weights Floyd-Warshall allows negative weights CSC317

4 CSC317 Can a shortest path contain a cycle?
As we have just seen, it cannot contain a negative-weight cycle. Nor can it contain a positive-weight cycle, since removing the cycle from the path produces a path with the same source and destination vertices and a lower path weight. If is a path and is a positive weight cycle so that then vi = vj and w(c) > p then the path has weight w(p‘) = w(p) – w(c) < w(p) and so p cannot be a shortest path. That leaves only 0-weight cycles. We can remove a 0-weight cycle from any path to produce another path whose weight is the same. Thus, if there is a shortest path from a source vertex s to a destination vertex v that contains a 0-weight cycle, then there is another shortest path from s to v without this cycle. CSC317

5 CSC317 Dijkstra’s shortest path algorithm
This algorithm finds a single source shortest path on a directed graph, with weights for the edges (for instance, weights could be driving distances between locations). This is a greedy algorithm, and the weights must be non negative (otherwise, for instance if we want to represent positive profit and negative loss, we can use a “cousin” algorithm, Bellman Ford, which we do not discuss here, and uses dynamic programming). Remember that Breadth First Search (BFS) also finds a shortest path, but in the case of unweighted edges. Dijkstra can be seen as a generalization of BFS. CSC317

6 The main idea is that we maintain a set of vertices S whose final shortest path lengths have already been determined. In each time we consider not yet discovered vertices in the graph, and all edges going from a discovered vertex (u) to an undiscovered vertex (v). We choose an undiscovered vertex with an edge from u to v, that gives the shortest path length. The length from u to v for each vertex v, is given by the length of u, plus the weight between u and v. In the initialization step, we just include source node s in the set of discovered nodes, and set its length to 0. All other lengths are initially infinity. Then we keep expanding set S of discovered nodes in a greedy manner. CSC317

7 CSC317

8 CSC317

9 In the figure, the black vertices at each step are those vertices added to set S.
Initially, only s is in set S. s can go to t (length 10) or y (length 5), and y yields the shortest path. Now both s and y are in set S. We consider all possibilities from set S (vertices s and y) to other vertices. We already have the s to t (length 10) stored and we also look at y to t (5 + 3 = 8) y to z (5 + 2 = 7) and y to x (5 + 9) – 14. The shortest greedy choice is y to z (length 7), so now z is added to our set of discovered nodes S. And so on see figure below. CSC317

10 CSC317 Run time: We run through each node once.
For each node we look into it’s adjacency list. If there are n nodes and m edges, we have the usual (n+m) number of operations. However, each operation takes time since we need to find the minimum among all possible edges. This can be done by extracting the minimum from a queue with each minimum taking O(lg n) time. As a consequence we have O((n+m)lg n) complexity. CSC317

11 CSC317 Bellman-Ford algorithm
Given a weighted, directed graph G = (V, E) with source s and weight function the Bellman-Ford algorithm returns a boolean value indicating whether or not there is a negative-weight cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that no solution exists. If there is no such cycle, the algorithm produces the shortest paths and their weights. CSC317

12 CSC317

13 Run time: O(EV) Θ(V) Θ(EV) O(E) CSC317


Download ppt "CSC317 Shortest path algorithms"

Similar presentations


Ads by Google