Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Source Shortest Paths

Similar presentations


Presentation on theme: "Single-Source Shortest Paths"— Presentation transcript:

1 Single-Source Shortest Paths
CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

2 Terminology A network is a directed graph. We will use both terms interchangeably. The weight of a path is the sum of weights of the edges that make up the path. The shortest path between two vertices s and t in a directed graph is a directed path from s to t with the property that no other such path has a lower weight.

3 Shortest Paths We will consider two problems: Assumptions:
Single-source: find the shortest path from the source vertex s to all other vertices in the graph. It turns out that these shortest paths form a tree, with s as the root. All-pairs: find the shortest paths for all pairs of vertices in the graph. Assumptions: Directed graphs Edges cannot have non-negative weights.

4 Assumptions Allow directed graphs.
In all our shortest path algorithms, we will allow graphs to be directed. Obviously, any algorithm that works on directed graphs will also work on undirected graphs. Why? Negative edge weights are not allowed. Why?

5 Assumptions Negative edge weights are not allowed. Why?
Obviously, any algorithm that works on directed graphs will also work on undirected graphs. Why? Undirected graphs are a special case of directed graphs. Negative edge weights are not allowed. Why? With negative weights, "shortest paths" may not be defined. If a cycle has negative weight, then repeating that cycle infinitely on a path make it "shorter" and "shorter" Note that the weight of the whole path needs to be negative (not just an edge) for this to happen. If all weights are nonnegative, a shortest path never needs to include a cycle.

6 Shortest-Paths Spanning Tree
Given a network G and a designated vertex s, a shortest-paths spanning tree (SPST) for s is a tree that contains s and all vertices reachable from s, such that: Vertex s is the root of this tree. Each tree path is a shortest path in G.

7 Dijkstra’s Algorithm Total distance from s to v (through u). For each vertex, v, record the shortest distance from s to it and the edge that connects it (like Prim). Add the vertex with the shortest distance.

8 Dijkstra’s Algorithm: Runtime
Total distance from s to v (through u). Time complexity: O(E lg V) O(V) O(VlgV + ElgV) = O(ElgV) (if all vertices are reachable from s) O(V) O(lgV) => O(VlgV) O(E) (together with the while loop) O(ElgV) (aggregate from both for loop and while loop), (RELAX has an implicit Decrease-Key operation of O(lgV). )

9 Dijkstra's Algorithm Computes an SPST for a graph G and a source s.
Very similar to Prim's algorithm, but: First vertex to add is the source, s. Works with directed graphs as well, whereas Prim's only works with undirected graphs. Requires edge weights to be non-negative. It looks at the total path weight, not just the weight of the current edge. Time complexity: O(E lg V) using a heap for the priority-queue.

10 Dijkstra’s Algorithm 6 2 1 7 3 5 4 Find the SPST(G,5). 20 4 20 30 10 9
Show the distance and the parent for each vertex. 1 7 2 5 3 4 6 20 4 20 30 10 18 15 9 11 30 15 25 5 12

11 Dijkstra’s Algorithm 1 7 2 5 3 4 6 20 4 20 30 10 18 15 9 11 30 15 25 5 12

12 All-Pairs Shortest Paths
Run Dijkstra to compute the Shortest Path Spanning Tree for each vertex used as source. Trick to get the successor (rather than the predecessor/parent) on a path: reverse direction of arrows, solve problem in reversed graph. That solution gives the predecessor in the reversed graph which correspond to successors in the original graph.


Download ppt "Single-Source Shortest Paths"

Similar presentations


Ads by Google