1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Introduction to Algorithms 6.046J/18.401J/SMA5503
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Paths Definitions Single Source Algorithms
COS 423 Lecture 9 Shortest Paths II ©Robert E. Tarjan 2011.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
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.
CSCI-455/552 Introduction to High Performance Computing Lecture 18.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
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.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Introduction to Algorithms Jiafen Liu Sept
Lecture 13 Algorithm Analysis
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Algorithms and Data Structures Lecture XIII
Shortest Path Problems
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Graphs Chapter 11 Objectives Upon completion you will be able to:
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
All pairs shortest path problem
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
Directed Graphs (Part II)
Presentation transcript:

1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer

2 Graphs Many problems can be modeled with graphs -navigation systems -networks -planning problems -… A frequent task occurring in optimization problems is to find shortest paths in graphs.

3 Definition of a graph Definition: A directed graph G = (V,E) (or: digraph) consists of a set V = {1, 2,..., |V |} of vertices and a set E  V x V of edges (or: arcs). A pair (v, v’)  E is called an edge (or arc) from v to v’. Representation: Vertices are represented by dots (nodes). Edges are represented by connecting lines with an arrowhead pointing to the destination vertex. Restriction: Finite graphs, i.e.  V

4 Example of a digraph

5 Adjacency matrix Graphs can be stored as adjacency matrices. A graph G = (V,E) is stored in a Boolean |V | x |V | matrix A G = (a ij ), where 1 ≤ i ≤ |V |, 1 ≤ j ≤ |V | and class Graph { Graph(int n) { this.numberOfNodes = n; this.a = new boolean[n][n]; } private int numberOfNodes; private boolean[][] a; }       if Eji Eji a ij ),(1 ),(0

6 Example of an adjacency matrix

7 Properties of adjacency matrices The memory requirement for storing a graph with vertex set V in an adjacency matrix is Θ(|V | 2 ). This memory requirement is independent of the number of edges in the graph. Hence, adjacency matrices are not so efficient for graphs with a relatively small number of edges. Because of the necessary initialization of the matrix or the consideration of all matrix entries most algorithms take at least Ω(|V | 2 ) computational steps.

8 Adjacency lists Using adjacency lists, for each vertex we store a linear linked list of the edges originating from this vertex. The vertices are maintained in a linear array of |V | initial pointers to these lists. The i-th list contains one list element with key j for each destination vertex of an edge. Adjacency lists support many operations very well, e.g. tracing edges in a graph. On the other hand, some operations are not supported well, especially insertion and deletion of vertices. Eji  ),(

9 An example

10 Implementation of adjacency lists class graphAL{ graphAL(int n){ this.numberOfNodes = n; this.edgeTo = new edge[n]; } private int numberOfNodes; private edge[] edgeTo; } class edge { edge(int node, edge next){ this.node = node; this.next = next; } int node; edge next; }

11 Adjacency lists Memory requirement: O(|V| + |E|) Adjacency lists support many operations very well, e.g. tracing edges in a graph. On the other hand, some operations are not supported well, especially insertion and deletion of vertices. Eji  ),(

12 Doubly-connected edge list The missing dynamics of adjacency lists can be achieved by storing the vertices in a doubly-connected list instead of an array of fixed length. Each element of this list contains three references: 2 to the neighboring list elements and one to an edge list, just like an adjacency list. Each edge list is doubly connected; instead of a vertex number, each edge list element contains a pointer to the respective element of the vertex list.

13 Doubly-connected edge lists: example

14 Shortest paths in unweighted graphs Definition: The single-source shortest-path problem is the task of finding, for a graph G = (V,E) and a vertex v  V, the shortest paths from v to all other vertices in G. Examples: Graph g Shortest paths originating from vertex 1 1  2 1  3 1  7 1  7  5 1  7  5  4 1  7  5  4 

15 Weighted graphs In a weighted graph each edge is labeled with a real number. These weights are interpreted as distances or cost of traversal. In the following we assume that these weights are non-negative, i.e. that there is a mapping c : E  R 0 + assigning a weight to each edge

16 Shortest paths in weighted graphs Definition: The single-source shortest-path problem is the task of finding, for a graph G = (V,E) and a vertex v  V, the shortest paths from v to all other vertices in G. Difference from unweighted graphs: The length of a path in a weighted graph is given by the sum of all weights of the edges contained in the path

17 Dijkstra’s algorithm Optimality principle: For each shortest path p = (v 0, v 1,..., v k ) from v 0 to v k, each partial path p´ = (v i,..., v j ), 0 ≤ i ≤ j ≤ k, is a shortest path from v i to v j. Proof: 1.Assume the opposite: then there would be a shorter path p´´ from v i to v j, hence in p the partial path p´ could be replaced by p´´ and the resulting path from v 0 to v k would be shorter than p. 2.However, this contradicts the basic assumption the p is a shortest path from v 0 to v k.

18 Consequence (1) 1.For all shortest paths sp(s, v) and edges (v, v´): c(sp(s, v)) + c((v, v´)) ≥ c(sp(s, v´)) 2.For at least one shortest path sp(s, v) and one edge (v, v´): c(sp(s, v)) + c((v, v´)) = c(sp(s, v´)) We can compute shortest paths by adding one edge at a time to a shortest path already known, with the following invariant:

19 Consequence (2) Let p = (v 0, v 1,..., v k ) be a path from v 0 to v k. Let p´´ be a shorter path from v i to v j than the respective partial path in p. Then we can replace the partial path from v i to v j in p by p´´ and obtain a shorter path p´ from v 0 to v k.

20 Idea of Dijkstra’s algorithm Initially the distance d(v) of all vertices (other than s) to s is set to . Of course, the distance of s to itself is 0. We consider a set PQ of vertex-distance pairs (v, d(v)), initially containing only the element (s, 0). Then PQ is modified step by step, according to the “greedy” principle “vertex with shortest distance to s first”, until PQ is empty: 1. Delete vertex v with minimum distance d(v) to s from PQ. d(v) is the shortest distance of s to v. 2. For each vertex w  V with (v, w)  E do: (a) If w has already been deleted from PQ (see 1), do nothing. (b) If (w, d(w))  PQ, replace (w, d(w)) by (w, min{d(w); d(v) + c(v,w)}). (c) If w is not inside in PQ, insert (w, (d(v) + c(v,w)) into PQ.

21 Calculation of the shortest paths The above algorithm only gives us the length of the shortest path for each vertex v´, not the actual path (the sequence of vertices). However, if we also store the predecessor of each node, it is easy to calculate for any vertex v´ the shortest path to the source vertex v. We simply proceed from v´ to its predecessor v´´. Then we determine (by the same method) the shortest path from v´´ to v. If we arrive at v, we stop. By this backward way, we obtain the shortest path from v to v´.

22 Required data structures For each vertex v, we store the current provisional distance d(v) to the source vertex s. In addition, we store the predecessor of v on the current provisional shortest path (so we can re-trace the path). Furthermore, we need a data structure for the set PQ in order to store the vertices that still have to be handled. We need to be able to (a) delete the vertex with minimum distance (b) decrease the distance d(v) of a given node v (c) insert a new vertex v with a given distance d(v) These are exactly the priority queue operations (with d(v) as priority); those are efficiently supported by Fibonacci heaps!

23 Pseudo-code void Dijkstra { FibonacciHeap PQ = new FibonacciHeap(); PQ.insert(s, 0, s); while ( !PQ.isEmpty ) { x = PQ.deletemin(); x.done = true; for ( (x,y)  x.EdgeList ) { if (y.done) return; if (y  PQ) if (d(y) > d(x) + c(x,y)) PQ.decreasekey(y, d(x) + c(x,y), x); else PQ.insert(y, d(x) + c(x,y), x); } } }

24 Example Source vertex:

25 Example Entry in PQ: (vertex, distance, predecessor): (1,0,1) (2,2,1), (6,9,1), (7,15,1) (6,9,1), (7,8,2), (3,6,2) (6,9,1), (7,8,2), (4,8,3), (9,21,3) (6,9,1), (4,8,3), (9,10,7), (8,23,7) (6,9,1), (8,23,7), (9,9,4), (5,9,4) (9,9,4), (5,9,4), (8,20,6) (5,9,4), (8,13,9) (8,12,5) Ф;

26 Analysis void Dijkstra { FibonacciHeap PQ = new FibonacciHeap(); PQ.insert(s, 0, s); while ( !PQ.isEmpty ) { x = PQ.deletemin(); x.done = true; for ( (x,y)  x.EdgeList ) { if (y.done) return; if (y  PQ) if (d(y) > d(x) + c(x,y)) PQ.decreasekey(y, d(x) + c(x,y), x); else PQ.insert(y, d(x) + c(x,y), x); } } }

27 Analysis The (outer) while-loop has at most |V| iterations, since each node can only be deleted once! Each deletemin takes O(log |V|) amortized time. For each vertex, the number of iterations in the (inner) for-loop equals the number of edges originating from that vertex. Hence, for all the for-loops together we have a total of |E| iterations. Inside the for-loop, each iteration takes O(1) amortized time. Hence, the total (amortized) running time is O(|E| + |V | · log |V |). NOTE: The running time depends on the representation of the graph (adjacency matrix/list, double-connected edge list) as well as on the implementation of the priority queue (linear list, heap, Fibonacci heap).