Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.

Similar presentations


Presentation on theme: "CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg."— Presentation transcript:

1 CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.

2 CSE 780 Algorithms Shortest Path Given a graph G = (V, E) with weight function w : E --> R Shortest path weight:

3 CSE 780 Algorithms Various Problems Single-source shortest-paths problem Given source node s to all nodes from V Single-destination shortest-paths problem From all nodes in V to a destination u Single-pair shortest-path problem Shortest path between u and v All-pairs shortest-paths problem Shortest paths between all pairs of nodes

4 Example CSE 2331 / 5331

5 CSE 780 Algorithms Negative-weight Edges Some edges may have negative weights If there is a negative cycle reachable from s: Shortest path is no longer well-defined Example Otherwise, it is fine

6 CSE 780 Algorithms Cycles Shortest path cannot have cycles inside Negative cycles : already eliminated Positive cycles: can be removed 0-weight cycles: can be removed A path with no cycles inside is also called a simple path. No-Cycle Theorem : Given any two vertices of graph, there exists a shortest path between them with no cycle.

7 CSE 780 Algorithms Optimal Substructure Property

8 CSE 780 Algorithms Shortest-paths Tree For every node v  V, π[v] is the predecessor of v in a shortest path from source s to v Nil if does not exist A shortest-path tree Root is source s Edges are (π[v], v) The shortest path between s and v is the unique tree path from root s to v.

9 CSE 780 Algorithms Example: directed graph

10 Example: undirected graph CSE 2331 / 5331

11 Shortest Path Tree Question: Given a shortest path tree of G rooted at s, how fast can we report the shortest path distance from s to all other vertices in G? CSE 2331 / 5331 O(V) The output of shortest-path algorithms usually contains both shortest-path tree and distances.

12 Shortest Path Tree Related to minimum spanning tree? CSE 2331 / 5331

13 CSE 780 Algorithms Goal:

14 Breadth-First Search Recall BFS: Shortest path for unweighted graph (i.e, each edge has weight 1). Would the same idea work for weighted graph? CSE 2331 / 5331

15 Example CSE 2331 / 5331 We can no longer guarantee that at the time the algorithm first discovers a node, the distance is shortest. BFS is only for shortest number of edges to reach a node! How to guarantee that when we first reach a node, the distance is shortest?

16 Dijkstra Algorithm CSE 2331 / 5331

17

18 Example CSE 2331 / 5331

19

20 Correctness CSE 2331 / 5331

21 Proof of Induction Step CSE 2331 / 5331

22

23 Why do we require that the weights are all positive? Example. CSE 2331 / 5331

24 Running Time Analysis CSE 2331 / 5331

25 Running Time Analysis CSE 2331 / 5331

26 Storing Cost at Vertices CSE 2331 / 5331

27 Running Time Analysis CSE 2331 / 5331

28 Second Improvement: Priority Queue CSE 2331 / 5331

29 Running Time Analysis CSE 2331 / 5331

30 Remarks CSE 2331 / 5331

31 A more general algorithm CSE 2331 / 5331

32 Shortest Path Given a graph G = (V, E) with weight function w : E --> R Shortest path weight:

33 CSE 2331/5331 Goal: Input: directed weighted graph G = (V, E), source node s  V Output: For every vertex v  V, d[v] =  (s, v) π[v] Shortest-paths tree induced by π[v]

34 Previously Dijkstra Algorithm: Compute shortest paths from a source node when the input graph has no negative edge weights! CSE 2331/5331 Now: A single source shortest path algorithm for arbitrary edge weight!

35 CSE 2331/5331 Relaxation Maintain shortest-path estimate d[v] for each node d[v]: initialize  Algorithms will repeatedly apply Relax. Differ in the order of Relax operation Intuition: Do we have a shorter path if use edge (u,v) ?

36 CSE 2331/5331 Shortest-paths Properties Triangle inequality For (u, v)  E,  (s, v) ≤  (s, u) + w(u, v) Equality holds when u is predecessor of v. Upper-bound property If start with d[v] =  and apply Relax operations Always have d[v] ≥  (s, v) Once d[v] =  (s, v), never changes Proof by contradiction

37 CSE 2331/5331 Properties cont. Convergence property Suppose s  u -> v is the shortest path from s to v Currently d[u] =  (s, u) Relax (u,v) will set d[v] =  (s, v) Proof: follow from Relaxation defintion Path-relaxation property Given a shortest path Apply Relax in order Then, afterwards, Proof: Follow from above property

38 CSE 2331/5331 Bellman-Ford Algorithm Allow negative weights Follow the frame work that first, initialize: Then apply a set of Relax compute d[v] and π[v] Return False if there exists negative cycles

39 CSE 2331/5331 Pseudo-code Time complexity: O(VE)

40 CSE 2331/5331 Example

41 CSE 2331/5331 Correctness Why d[v] =  (s, v) for every node v ? Proof: Use path-relaxation property Return True (or False) iff there is (or is no) negative cycles ? If there is no negative cycle: obvious use triangle inequality If there is negative cycle: Proof by contradiction


Download ppt "CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg."

Similar presentations


Ads by Google