Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.

Slides:



Advertisements
Similar presentations
Bellman-Ford algorithm
Advertisements

* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Design and Analysis of Algorithms Approximation algorithms for NP-complete problems Haidong Xue Summer 2012, at GSU.
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
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
Chapter 25: All-Pairs Shortest-Paths
Management Science 461 Lecture 2b – Shortest Paths September 16, 2008.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
 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.
Introduction To Algorithms CS 445 Discussion Session 8 Instructor: Dr Alon Efrat TA : Pooja Vaswani 04/04/2005.
Multi-Source Shortest Paths T. Patrick Bailey CSC 5408 Graph Theory 4/28/2008.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
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.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Design and Analysis of Algorithms - Chapter 81 Dynamic Programming Dynamic Programming is a general algorithm design technique Dynamic Programming is a.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
Graph Algorithms Shortest path problems [Adapted from K.Wayne]
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Design and Analysis of Algorithms Minimum Spanning trees
Shortest Paths1 C B A E D F
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
The Shortest Path Problem
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
COSC 2007 Data Structures II Chapter 14 Graphs III.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Which of these can be drawn without taking your pencil off the paper and without going over the same line twice? If we can find a path that goes over all.
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
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
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.
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.
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Chapter 14 Section 3 - Slide 1 Copyright © 2009 Pearson Education, Inc. AND.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Shortest Path Graph represents highway system Edges have weights
CSE 373: Data Structures and Algorithms
Shortest Path Algorithms
A path that uses every vertex of the graph exactly once.
Floyd’s Algorithm (shortest-path problem)
All pairs shortest path problem
Graph Algorithms: Shortest Path
Presentation transcript:

Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU

Single-source shortest paths

Given this weighted, directed graph, what is the weight of path: ? 10+2=12

Single-source shortest paths Given this weighted, directed graph, what is the weight of path: ? = 17

Single-source shortest paths Given this weighted, directed graph, what is the weight of path: ?

Single-source shortest paths Given this weighted, directed graph, what is the weight of path: and ?? 4-6 = -2 and = -4 Negative cycleThere is no shortest path from 1 to 5

Single-source shortest paths Shortest path of a pair of vertices : a path from u to v, with minimum path weight Applications: – Your GPS navigator – If weights are time, it produces the fastest route – If weights are gas cost, it produces the lowest cost route – If weights are distance, it produces the shortest route

Single-source shortest paths Single-source shortest path problem: given a weighted, directed graph G=(V, E) with source vertex s, find all the shortest (least weight) paths from s to all vertices in V.

Single-source shortest paths Two classic algorithms to solve single-source shortest path problem – Bellman-Ford algorithm A dynamic programming algorithm Works when some weights are negative – Dijkstra’s algorithm A greedy algorithm Faster than Bellman-Ford Works when weights are all non-negative

Bellman-Ford algorithm Observation: If there is a negative cycle, there is no solution – Add this cycle again can always produces a less weight path If there is no negative cycle, a shortest path has at most |V|-1 edges Idea: Solve it using dynamic programming For all the paths have at most 0 edge, find all the shortest paths For all the paths have at most 1 edge, find all the shortest paths … For all the paths have at most |V|-1 edge, find all the shortest paths

Bellman-Ford algorithm //Initialize 0-edge shortest paths //bottom-up construct 0-to-(|V|-1)-edges shortest paths //test negative cycle

Bellman-Ford algorithm e.g What is the 0-edge shortest path from 1 to 1? <> with path weight 0 What is the 0-edge shortest path from 1 to 2? What is the 0-edge shortest path from 1 to 3? 0 20

Bellman-Ford algorithm e.g What is the at most 1-edge shortest path from 1 to 1? <> with path weight 0 What is the at most 1-edge shortest path from 1 to 2? with path weight 10 What is the at most 1-edge shortest path from 1 to 3? with path weight In Bellman-Ford, they are calculated by scan all edges once

Bellman-Ford algorithm e.g What is the at most 2-edges shortest path from 1 to 1? <> with path weight 0 What is the at most 2-edges shortest path from 1 to 2? with path weight 10 What is the at most 2-edges shortest path from 1 to 3? with path weight In Bellman-Ford, they are calculated by scan all edges once

Bellman-Ford algorithm All 0 edge shortest paths 0

Bellman-Ford algorithm Calculate all at most 1 edge shortest paths 0 /0

Bellman-Ford algorithm Calculate all at most 2 edges shortest paths /11 /7 /2 /0 /4

Bellman-Ford algorithm Calculate all at most 3 edges shortest paths /4 /7 /2 /0

Bellman-Ford algorithm Calculate all at most 4 edges shortest paths /4 /7 /-2 /0

Bellman-Ford algorithm Final result: What is the shortest path from 1 to 5? What is weight of this path? 1, 4, 3, 2, 5 -2 What is the shortest path from 1 to 2, 3, and 4?

Dijkstra’s Algorithm

The shortest path of the vertex with smallest distance is determined 0

Dijkstra’s Algorithm Choose a vertex whose shortest path is now determined 0 10

Dijkstra’s Algorithm Choose a vertex whose shortest path is now determined

Dijkstra’s Algorithm Choose a vertex whose shortest path is now determined

Dijkstra’s Algorithm Choose a vertex whose shortest path is now determined 0 8 9

Dijkstra’s Algorithm Final result: What is the shortest path from 1 to 5? What is weight of this path? 1, 4, 5 7 What is the shortest path from 1 to 2, 3, and 4?

All-pairs shortest paths

What are the weights of shortest paths with no intermediate vertices, D(0)?

All-pairs shortest paths D(0) What are the weights of shortest paths with intermediate vertex 1? D(1)

All-pairs shortest paths 4 D(1) What are the weights of shortest paths with intermediate vertices 1 and 2? D(2)

All-pairs shortest paths D(2) What are the weights of shortest paths with intermediate vertices 1, 2 and 3? D(3)

All-pairs shortest paths D(3) What are the weights of shortest paths with intermediate vertices 1, 2, 3 and 4? D(4)

All-pairs shortest paths D(0) D(1) D(2) D(3) Add predecessor information to reconstruct a shortest path 0/n 4/2 3/3 0/n 9/3 6/4 0/n If updated the predecessor i-j in D(k) is the same as the predecessor k-j in D(k-1) 0/n 4/2 3/3 0/n 9/3 6/4 0/n 4/2 3/3 0/n 6/26/2 6/4 0/n 4/2 3/3 0/n 6/2 6/4 0/n

All-pairs shortest paths D(4) What is the shortest path from 3 to 4? What is weight of this path? 3, 2, /n 4/2 3/3 0/n 6/2 6/4 0/n