Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.

Slides:



Advertisements
Similar presentations
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Advertisements

The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Chapter 25: All-Pairs Shortest-Paths
Discussion #34 1/17 Discussion #34 Warshall’s and Floyd’s Algorithms.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
Dr. Shahriar Bijani Shahed University Feb  Kleinberg and Tardos, Algorithm Design, CSE 5311, M Kumar, Spring  Chapter 4, Computer Algorithms,
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Discrete Math for CS Chapter 8: Directed Graphs. Discrete Math for CS digraph: A digraph is a graph G = (V,E) where V is a finite set of vertices and.
All-Pairs Shortest Paths
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
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.
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.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
More Dynamic Programming Floyd-Warshall Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COMP108 Time Complexity of Pseudo Code. Example 1 sum = 0 for i = 1 to n do begin sum = sum + A[i] end output sum O(n)
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.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
Introduction to Algorithms Jiafen Liu Sept
Shortest Path Graph Theory Basics Anil Kishore.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
Lecture 23 CSE 331 Oct 24, Reminder 2 points for Piazza participation 3 points for mini-project.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
All-Pairs Shortest Paths
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Graph Algorithms BFS, DFS, Dijkstra’s.
Lecture 23 CSE 331 Oct 26, 2016.
Shortest Path Problems
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Analysis and design of algorithm
Page 620 Single-Source Shortest Paths
Lecture 22 CSE 331 Oct 23, 2017.
Lecture 23 CSE 331 Oct 25, 2017.
Lecture 24 CSE 331 Oct 29, 2012.
Lecture 23 CSE 331 Oct 24, 2011.
Lecture 22 CSE 331 Oct 24, 2016.
Shortest Path Problems
Floyd-Warshall Algorithm
Shortest Path Algorithms
Slide Courtesy: Uwash, UT
Advanced Algorithms Analysis and Design
Lecture 22 CSE 331 Oct 15, 2014.
Text Book: Introduction to algorithms By C L R S
Slide Courtesy: Uwash, UT
Lecture 23 CSE 331 Oct 24, 2011.
Lecture 26 CSE 331 Nov 1, 2010.
Directed Graphs (Part II)
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem

Single-Source Shortest Paths A motorist wishes to find the shortest possible route from from Perth to Brisbane. Given the map of Australia on which the distance between each pair of cities is marked, how can we determine the shortest route?

Single Source Shortest Path In a shortest-paths problem, we are given a weighted, directed graph G = (V,E), with weights assigned to each edge in the graph. The weight of the path p = (v0, v1, v2, …, vk) is the sum of the weights of its constituent edges: v0  v1  v2...  vk-1  vk The shortest-path from u to v is given by d(u,v) = min {weight (p) : if there are one or more paths from u to v =  otherwise

The single-source shortest paths problem Given G (V,E), find the shortest path from a given vertex u  V to every vertex v  V ( u  v). For each vertex v  V in the weighted directed graph, d[v] represents the distance from u to v. Initially, d[v] = 0 when u = v. d[v] =  if (u,v) is not an edge d[v] = weight of edge (u,v) if (u,v) exists. Dijkstra's Algorithm : At every step of the algorithm, we compute, d[y] = min {d[y], d[x] + w(x,y)}, where x,y  V. Dijkstra's algorithm is based on the greedy principle because at every step we pick the path of least weight.

Dijkstra's Algorithm : At every step of the algorithm, we compute, d[y] = min {d[y], d[x] + w(x,y)}, where x,y  V. Dijkstra's algorithm is based on the greedy principle because at every step we pick the path of least path.

Example: Step # Vertex to be marked Distance to vertexUnmarked vertices uabcdefgh 0u015  9  a,b,c,d,e,f,g,h 1a01539  b,c,d,e,f,g,h 2c01537  12  b,d,e,f,g,h 3b  d,e,f,g,h 4d  e,f,g,h 5e f,g,h 6h g,h 7g h 8f

Dijkstra's Single-source shortest path Procedure Dijkstra's Single-source shortest path_G(V,E,u) Input: G =(V,E), the weighted directed graph and v the source vertex Output: for each vertex, v, d[v] is the length of the shortest path from u to v. mark vertex u; d[u]  0; for each unmarked vertex v  V do if edge (u,v) exists d [v]  weight (u,v); else d[v]   ; while there exists an unmarked vertex do let v be an unmarked vertex such that d[v] is minimal; mark vertex v; for all edges (v,x) such that x is unmarked do if d[x] > d[v] + weight[v,x] then d[x]  d[v] + weight[v,x]

Complexity of Dijkstra's algorithm: Steps 1 and 2 take  (1) time Steps 3 to 5 take O(  V  ) time The vertices are arranged in a heap in order of their paths from u Updating the length of a path takes O(log V) time. There are  V  iterations, and at most  E  updates Therefore the algorithm takes O((  E  +  V  ) log  V  ) time.

All-Pairs Shortest Path Problem Consider a shortest path p from vertex i to vertex j If i =j then there is no path from i to j. If i  j, then we decompose the path p into two parts, dist(i,k) and dist(k,j) dist (i,j) = dist(i,k) + dist(k,j) Recursive solution

Floyd' s Algorithm for Shortest Paths Procedure FLOYDs_G=[V,E] Input: n  n matrix W representing the edge weights of an n-vertex directed graph. That is W =w(i,j) where, (Negative weights are allowed) Output: shortest path matrix, dist(i,j) is the shortest path between vertices i and j. for v  1 to n do for w  1 to n do dist[v,w]  arc[v,w]; for u  1 to n do for v  1 to n do for w  1 to n do if dist[v,u] + dist[u,w] < dist[v,w] then dist[v,w]  dist[v,u] + dist[u,w] Complexity :  (n 3 )

ABCDEFG A01  4  B  02  32 C  02  D  0  2 E  410  3 F  304 G2  5  0

A BCDEFG A01  4  B  02  32 C  02  D  0  2 E  410  3 F  304 G235  60 Distances after using A as the pivot

ABCDEFG A013  43 B  02  32 C  02  D  0  2 E  410  3 F  304 G235  60 Distances after using B as the pivot

ABCDEFG A B C D E F G Distances after using G as the pivot

Transitive Closure Given a directed graph G=(V,E), the transitive closure C =(V,F) of G is a directed graph such that there is an edge (v,w) in C if and only if there is a directed path from v to w in G. Security Problem: the vertices correspond to the users and the edges correspond to permissions. The transitive closure identifies for each user all other users with permission (either directly or indirectly) to use his or her account. There are many more applications of transitive closure. The recursive definition for transitive closure is

Warshall's Algorithm for Transitive Closure Procedure WARSHALL's(G=[V,E]) Input: n  n matrix A representing the edge weights of an n-vertex directed graph. That is a =a(i,j) where, Output: transitive closure matrix, t(i,j) =1 if there is a path from i to j, 0 otherwise for v  1 to n do for w  1 to n do t[v,w]  a(v,w) for u  1 to n do for v  1 to n do for w  1 to n do if NOT t[v,w] then t[v,w]  t[v,u] AND t[u,w] return T