Advanced Algorithms Analysis and Design

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS138A Single Source Shortest Paths Peter Schröder.
Shortest-paths. p2. Shortest-paths problems : G=(V,E) : weighted, directed graph w : E  R : weight function P=
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Introduction to Algorithms 6.046J/18.401J/SMA5503
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
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)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Greedy Algorithms General principle of greedy algorithm
Single-Source Shortest Path
Single-Source Shortest Paths
Algorithms and Data Structures Lecture XIII
CS200: Algorithm Analysis
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
Minimum Spanning Trees
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
SINGLE-SOURCE SHORTEST PATHS
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Page 620 Single-Source Shortest Paths
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Basic Graph Algorithms
CS 583 Analysis of Algorithms
2018/12/27 chapter25.
Analysis of Algorithms CS 477/677
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 12 Algorithm Analysis
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
Shortest Path Problems
Greedy Algorithms Comp 122, Spring 2004.
Advanced Algorithms Analysis and Design
Brad Karp UCL Computer Science
Slide Courtesy: Uwash, UT
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
CS 3013: DS & Algorithms Shortest Paths.
Chapter 24: Single-Source Shortest-Path
Single-Source Shortest Path & Minimum Spanning Trees
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Topological Sorting Minimum Spanning Trees Shortest Path
Advanced Algorithms Analysis and Design
Presentation transcript:

Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Lecture No. 35 Dijkstra’s Algorithm Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Problem Statement Given a graph G = (V, E) with a source vertex s, weight function w, edges are non-negative, i.e., w(u, v) ≥ 0,  (u, v)  E The graph is directed, i.e., if (u, v)  E then (v, u) may or may not be in E. The objective is to find shortest path from s to every vertex u  V. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Approach Approach A “cloud S” of vertices, beginning with s, will be constructed, finally covering all vertices of graph For each vertex v, a label d(v) is stored, representing distance of v from s in the subgraph consisting of the cloud and its adjacent vertices At each step We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u) We update labels of the vertices adjacent to u Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Mathematical Statement of Problem Input Given graph G(V, E) with source s, weights w Assumption Edges non-negative, w(u, v) ≥ 0,  (u, v)  E Directed, if (u, v)  E then (v, u) may be in E Objective: Find shortest paths from s to every u  V Approach Maintain a set S of vertices whose final shortest-path weights from s have been determined Repeatedly select, u  V – S with minimum shortest path estimate, add u to S, relax all edges leaving u. Greedy, always choose light vertex in V-S , add to S Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Edge Relaxation Consider edge e = (u, z) such that V- S Consider edge e = (u, z) such that u is vertex most recently added to the cloud S z is not in the cloud Relaxation of edge e updates distance d(z) as d(z) = min {d(z), d(u) + weight(e)} d(z) = 75 d(u) = 50 10 z s u d(z) = 60 e S S V - S Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Dijkstra’s Algorithm DIJKSTRA(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S ← Ø 3 Q ← V[G] 4 while Q ≠ Ø 5 do u ← EXTRACT-MIN(Q) 6 S ← S  {u} 7 for each vertex v  Adj[u] 8 do RELAX (u, v, w) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dijkstra’s Algorithm For each vertex v  V(G) d[v] ← ∞ π[v] ← NIL Considering s as root node d[s]  0 S   t x 1 ∞ ∞ 10 9 2 3 s 4 6 7 5 ∞ ∞ 2 y z Q s t x y z Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dijkstra’s Algorithm s is extracted form queue S  S  {s} Adj[s] = t, y d[t] > d[s] + w(s, t) (∞ > 0 + 10) d[t] ← d[s] + w(s, t) 0 + 10 = 10 π[t] ← s d[y] > d[s] + w(s, y) (∞ > 0 + 5) d[y] ← d[s] + w(s, y) 0 + 5 = 5 π[y] ← s t x 1 10 ∞ 10 9 2 3 s 4 6 7 5 5 ∞ 2 y z Q t x y z 10 5 Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dijkstra’s Algorithm y is extracted form queue S  S  {y} Adj[y] = t, x, z d[t] > d[y] + w(y, t) (10 > 5 + 3) d[t] ← d[y] + w(y, t) 5 + 3 = 8 π[t] ← y d[x] > d[y] + w(y, x) (∞ > 5 + 9) d[x] ← d[y] + w(y, x) 5 + 9 = 14 π[x] ← y d[z] > d[y] + w(y, z) (∞ > 5 + 2) d[z] ← d[y] + w(y, z) 5 + 2 = 7 π[z] ← y t x 1 8 14 10 9 2 3 s 4 6 7 5 5 7 2 y z Q t x z 8 14 7 Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dijkstra’s Algorithm z is extracted form queue S  S  {z} Adj[z] = s, x d[s] > d[z] + w(s, z) But (0 < 7 + 7) d[x] > d[z] + w(z, x) (14 > 7 + 6) d[x] ← d[z] + w(z, x) 7 + 6 = 13 π[x] ← z t x 1 8 13 10 9 2 3 s 4 6 7 5 5 7 2 y z Q t x 8 13 Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dijkstra’s Algorithm t is extracted form queue S  S  {t} Adj[t] = x, y d[x] > d[t] + w(t, x) (13 > 8 + 1) d[x] ← d[t] + w(t, x) 8 + 1 = 9 π[x] ← t d[y] > d[t] + w(t, y) But (5 < 8 + 3) t x 1 8 9 10 9 2 3 s 4 6 7 5 5 7 2 y z Q x 13 Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Example: Dijkstra’s Algorithm x is extracted form queue S  S  {x} Adj[x] = z d[z] > d[x] + w(x, z) But (7 < 9 + 4) t x 1 8 9 10 9 2 3 s 4 6 7 5 5 7 2 y z Q  Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Analysis: Dijkstra’s Algorithm Cost depends on implementation of min-priority queue Case 1: Vertices being numbered 1 to |V| INSERT, DECREASE-KEY operations takes O(1) EXTRACT-MIN operation takes O(V) time Sub cost is O(V2) Total number of edges in all adjacency list is |E| Total Running time = O (V2 + E) = O(V2) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Analysis: Dijkstra’s Algorithm Case 2: Graph is sufficiently spare, e.g., E = O (V2 /lgV) Implement min-priority queue with binary min heap Vertices being numbered 1 to |V| Each EXTRACT-MIN operation takes O(lgV) There |V| operations, time to build min heap O(V) Sub cost is O(V lgV) Each DECREASE-KEY operation takes time O(lgV), and there are |E| such operation. Sub cost is O(E lgV) Hence Total Running time = O (V + E) lgV = E lgV Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Analysis: Dijkstra’s Algorithm Case 3: Implement min-priority queue with Fibonacci heap Vertices being numbered 1 to |V| Each EXTRACT-MIN operation takes O(lgV) There |V| operations, time to build min heap O(V) Sub cost is O(V lgV) Each DECREASE-KEY operation takes time O(1), and there are |E| such operation. Sub cost is O(E) Hence Total Running time = O (V.lgV + E) = V lgV Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Case 1: Computation Time INITIALIZE-SINGLE-SOURCE(V, s) S ←  Q ← V[G] while Q   do u ← EXTRACT-MIN(Q) S ← S  {u} for each vertex v  Adj[u] do RELAX(u, v, w) Running time: O(V2 + E) = O((V2) Note: Running time depends on Impl. Of min-priority (Q) (V) O(V) build min-heap O(V) O(V) O(E) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

INITIALIZE-SINGLE-SOURCE(V, s) Case 2 : Binary min Heap INITIALIZE-SINGLE-SOURCE(V, s) S ←  Q ← V[G] while Q   do u ← EXTRACT-MIN(Q) S ← S  {u} for each vertex v  Adj[u] do RELAX(u, v, w) Running time: O(VlgV + ElgV) = O(ElgV) (V) O(V) build min-heap Executed O(V) times O(lgV) O(E) times O(lgV) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

INITIALIZE-SINGLE-SOURCE(V, s) Case 3 : Fibonacci Heap INITIALIZE-SINGLE-SOURCE(V, s) S ←  Q ← V[G] while Q   do u ← EXTRACT-MIN(Q) S ← S  {u} for each vertex v  Adj[u] do RELAX(u, v, w) Running time: O(VlgV + E) = O(VlgV) (V) O(V) build min-heap Executed O(V) times O(lgV) O(E) times O(1) Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Theorem : Correctness of Dijkstra’s Algorithm Dijkstra’s algorithm, runs on a weighted, directed graph G = (V, E) with non-negative weight function w and source s, terminates with d[u] = δ(s, u) for all vertices u  V. Proof We use the following loop invariant: At start of each iteration of the while loop of lines 4-8, d[v] = δ(s, v) for each vertex v  S. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. It suffices to show for each vertex u  V, we have d[u] = δ(s, u) at the time when u is added to set S. Once we show that d[u] = δ(s, u), we rely on the upper-bound property to show that the equality holds at all times thereafter. Initialization: Initially, S = Ø, and so the invariant is trivially true Maintenance: We wish to show that in each iteration, d[u] = δ(s, u), for the vertex added to set S. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. On contrary suppose that d[u] ≠ δ(s, u) when u is added to set S. Also suppose that u is the first vertex for which the equality does not hold. We focus on situation at beginning of while loop in which u is added to S and derive a contradiction. First of all, u ≠ s because s is the first vertex added to set S and d[s] = δ(s, s) = 0 at that time. Secondly S ≠ Ø just before u is added to S, this is because s is at least in S. There must be some path from s to u, otherwise d[u] = δ(s, u) = ∞ by no-path property, which would violate our assumption that d[u] ≠ δ(s, u). Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Because there is at least one path, there must be a shortest path p from s to u. Prior to adding u to S, path p connects a vertex in S, namely s, to a vertex in V - S, namely u. s x y u p2 S V - S Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Let us consider the first vertex y along p such that y  V - S, and let x  S be y's predecessor. Thus, path p can be decomposed: s x  y  u (either of paths p1 or p2 may have no edges.) We claim that d[y] = δ(s, y) when u is added to S. Proof of Claim: observe that x  S. Because u is chosen as the first vertex for which d[u] ≠ δ(s, u) when it is added to S, we had d[x] = δ(s, x) when x was added to S. Edge (x, y) was relaxed at that time, and hence d[y] = δ(s, y) (convergence property). p1 p2 Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Because y occurs before u on a shortest path from s to u and all edge weights are nonnegative (on path p2), we have δ(s, y) ≤ δ(s, u), Now d[y] = δ(s, y) ≤ δ(s, u) ≤ d[u]  d[y] ≤ d[u] (1) But because both vertices u and y were in V - S when u was chosen, we have d[u] ≤ d[y]. (2) From (1) and (2), d[u] = d[y] Now, d[y] = δ(s, y) ≤ δ(s, u) = d[u] = d[y]  δ(s, y) = δ(s, u) . Finally, d[u] = δ(s, u), it contradicts choice of u Hence, d[u] = δ(s, u) when u is added to S, and this equality is maintained at all times after that Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Termination: At termination, Q = Ø which, along with our earlier invariant that Q = V - S, implies that S = V. Thus, d[u] = δ(s, u) for all vertices u  V. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Lemma 1 Statement Let G = (V, E) be a weighted, directed graph with weight function w : E → R, let s  V be a source vertex. Assume that G contains no negative-weight cycles reachable from s. Then, after the graph is initialized by INITIALIZE-SINGLE-SOURCE(G, s), the predecessor sub-graph Gπ forms a rooted tree with root s, and any sequence of relaxation steps on edges of G maintains this property as an invariant. Proof Initially, the only vertex in Gπ is the source vertex, and the lemma is trivially true. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Let Gπ be a predecessor subgraph that arises after a sequence of relaxation steps. a. First we prove that Gπ is a rooted tree. Gπ is acyclic On contrary suppose that some relaxation step creates a cycle in the graph Gπ . Let c = <v0, v1,..., vk> be cycle, where vk = v0. Then, π[vi] = vi-1 for i = 1, 2,..., k Now, without loss of generality, we can assume that it was the relaxation of edge (vk-1, vk) that created the cycle in Gπ. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Claim: all vertices on cycle c reachable from s. Because each vertex has non-NIL predecessor, and it was assigned a finite shortest-path estimate when it was assigned non-NIL π value By upper-bound property, each vertex on c has a finite shortest-path weight, and reachable from s. Shortest-path on c just prior RELAX(vk-1, vk, w) Just before call, π[vi] = vi-1 for i = 1, 2,..., k - 1. Thus, for i = 1, 2,..., k - 1, last update to d[vi] was d[vi] ← d[vi-1] + w(vi-1, vi). Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. It is obvious that, d[vk] > d[vk-1] + w(vk-1, vk). Summing it with k - 1 inequalities, Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Thus, sum of weights around cycle c is negative, which provides the contradiction. We have proved that Gπ is a directed, acyclic. To show that it forms a rooted tree with root s Sufficient to prove that  v  Vπ, there is a unique path from s to v in Gπ. On contrary, suppose there are two simple paths from s to some vertex v, and (x ≠ y) p1: s u  x  z  v p2: s u  y  z  v π[z] = x and π[z] = y,  x = y, a contradiction. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design

Contd.. Hence there exists unique path in Gπ from s to v. Thus Gπ forms a rooted tree with root s. b. Now by predecessor subgraph property d[v] = δ(s, v) for all vertices v  V. Proved Lemma 2 If we run Dijkstra's algorithm on weighted, directed graph G = (V, E) with nonnegative weight function w and source s, then at termination, predecessor subgraph Gπ is a shortest paths tree rooted at s. Proof: Immediate from the above lemma. Dr. Nazir A. Zafar Advanced Algorithms Analysis and Design