Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

Size: px
Start display at page:

Download "Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University"

Transcription

1 Algorithms All-Pairs Shortest Paths Dong Kyue Kim Hanyang University

2 Contents Using single source shortest path algorithms Presents O(V 4 )-time algorithm, O(V 3 log V)-time algorithm, O(V 3 )-time algorithm O(V 4 )-time algorithm O(V 3 log V)-time algorithm Floyd-Warshall algorithm Transitive closure of a directed graph 2

3 Using single source shortest path algorithms Positive edges Negative edges 3

4 Using single source shortest path algorithms Using single source shortest path algorithms We can solve an all-pairs shortest-paths problem by running a single-source shortest-paths algorithm V times, once for each vertex as the source. 4

5 Using single source shortest path algorithms Positive edges Using dijkstra algorithm: The linear-array implementation O(V 3 + V E) = O(V 3 ). The binary min-heap implementation O(V E lg V), Fibonacci heap O(V 2 lg V + V E). 5

6 Using single source shortest path algorithms Negative edges Using Bellman-Ford algorithm: O(V 2 E) Dense graph O(V 4 ) 6

7 Using single source shortest path algorithms Use adjacency matrix Predecessor matrix 7

8 Using single source shortest path algorithms Use adjacency matrix We assume that the vertices are numbered 1, 2,..., V The input is an n n matrix W representing the edge weights of an n-vertex directed graph G = (V, E) w ij the weight of directed edge Negative-weight edges are allowed, but we assume for the time being that the input graph contains no negative-weight cycles. (i, j) if if if i i i j, jand (i, j) E, jand (i, j) E 8

9 Using single source shortest path algorithms Predecessor matrix Say Π = (π ij ) π ij = NIL if either i = j or there is not path from i to j. π ij is the predecessor of j on some shortest path from i to j. 9

10 Using single source shortest path algorithms For each vertex i V, we define predecessor subgraph of G for i as G π,i = (V π,i, E π,i ), where V π,i = {j V : π ij = NIL} {i} E π,i = {(π ij, j) j V π,i {i}} If G π,i is a shortest-paths tree, then PRINT-ALL- PAIRS-SHORTEST-PATH will print a shortest path from i to j. 1

11 Using single source shortest path algorithms PRINT-ALL-PAIRS-SHORTEST-PATH(Π, i, j) 1 if i = j 2 then print i 3 else if π ij = NIL 4 then print no path from i to j exists 5 else PRINT-ALL-PAIRS-SHORTEST-PATH(Π,i, π ij ) 6 print j 11

12 Shortest paths and matrix multiplication O(V 4 )-time algorithm Use dynamic programming Use matrix multiplication Associative (short explanation) Computing predecessor matrix 12

13 Shortest paths and matrix multiplication Use dynamic programming Characterize the structure of an optimal solution. Recursively define the value of an optimal solution. Compute the value of an optimal solution in a bottom-up fashion. 13

14 Shortest paths and matrix multiplication The structure of a shortest path On a graph G = (V, E), all subpaths of a shortest path are shortest paths. (Lemma 24.1) k P 1 P 2 i j P` : {1,2,,k-1} p is a shortest path from i to k, and so δ(i, j) = δ(i, k) + w kj. 14

15 Shortest paths and matrix multiplication A recursive solution to the all-pairs shortest-paths problem (m) l ij Let be the minimum weight of any path from vertex i to vertex j that contains at most m edges When m =, then () l ij When m 1, then if if i j i j l ( m) ij min( l min{ l 1kn ( m1) ij ( m1) ij, min{ l 1kn w kj } ( m1) ij w kj }) 15

16 Shortest paths and matrix multiplication If the graph contains no negative-weight cycles, For every pair of vertices i and j for which δ(i, j) < There is a shortest path from i to j that is simple and thus contains at most n - 1 edges. A path from vertex i to vertex j with more than n - 1 edges cannot have lower weight than a shortest path from i to j. Therefore, ( i, j) l l ( n1) ( n) ( n1) ij ij ij l... 16

17 Shortest paths and matrix multiplication Computing the shortest-path weights bottom up Input the matrix W = (w ij ), we now compute a series of matrices L (1), L (2),..., L (n-1), ( m) ( m) Where for m = 1, 2,..., n - 1, we have l ( l ). The final matrix L (n-1) contains the actual shortest-path weights. Observe that for all vertices i, j V, and so L (1) = W ij. ij 17

18 Shortest paths and matrix multiplication Let L (m 1) = L, L (m) = L. We have EXTEND-SHORTEST-PATHS(L, W) 1 n rows[l] 2 let L = (l ij ) be an n n matrix 3 for i 1 to n 4 do for j to n 5 do 6 for k 1 to n 7 do 8 return L Costs Θ(n 3 ) time. 18

19 Shortest paths and matrix multiplication EXTEND-SHORTEST-PATHS(L, W) 1 n rows[l] 2 let C be an n n matrix 3 for i 1 to n 4 do for j 1 to n 5 do c ij 6 for k 1 to n 7 do c ij c ij + a ik b kj 8 return L 19

20 Shortest paths and matrix multiplication SLOW-ALL-PAIRS-SHORTEST-PATHS(W) 1 n rows[w] 2 L (1) W 3 for m 2 to n 1 4 do L (m) EXTEND-SHORTEST-PATHS(L (m 1),W) 5 return L (n 1) Costs Θ(n 4 ) time. 2

21 Shortest paths and matrix multiplication Improving the running time We are interested only in matrix L (n-1). Recall that in the absence of negative-weight cycles, implies L (m) = L (n-1) for all integers m n - 1. Therefore, we can compute L (n-1) with only lg(n - 1) matrix products by computing the sequence 21

22 22 Shortest paths and matrix multiplication (4) (3) (2) (1) L L L L

23 Shortest paths and matrix multiplication L (2 L L L L (1) (2) (4) (8) W W W W W W W W W 2 4 W W 2 4 W W lg( n1) 2lg( n1) 2lg( n1) 1 2lg( n1) 1 23

24 Shortest paths and matrix multiplication

25 Shortest paths and matrix multiplication FASTER-ALL-PAIRS-SHORTEST-PATHS(W) 1 n rows[w] 2 L (1) W 3 m 1 4 while m < n do L (2m) EXTEND-SHORTEST-PATHS(L (m), L (m) ) 6 m 2m 7 return L (m) Costs Θ(n 3 ) time. 25

26 The Floyd-Warshall algorithm Intermediate Vertex An intermediate vertex of a simple path p = <v 1, v 2,, v l > is any vertex of p other than v 1 and v l. 26

27 The Floyd-Warshall algorithm The structure of a shortest path Floyd-Warshall algorithm is based on the observation of the intermediate vertices, which costs Θ( V 3 ) time. Let V = {1, 2,, n}. For any pair of vertices i, j V, consider all paths from i to j whose intermediate vertices are all drawn from {1, 2,, k}, and let p be a minimum weight path from among them. 27

28 The Floyd-Warshall algorithm If k is not an intermediate vertex of path p, then all intermediate vertices of p are in {1, 2,, k 1}. If k is an intermediate vertex of path p, then we break p down into all intermediate vertices in {1,2..,k-1} all intermediate vertices in {1,2..,k-1} P 1 P 2 k i j P: all intermediate vertices in {1,2,,k-1} 28

29 The Floyd-Warshall algorithm A recursive solution to the all-pairs shortest-paths problem (k ) d ij Let be the weight of a shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1, 2,, k}. We have the following recurrence: d ( k ) ij wij min(d (k -1) ij,d (k -1) ik d (k -1) kj ) if k, if k 1. (25.5) Because for any path, all intermediate vertices are in the set {1, 2,, n}, the matrix D (n) (n) dij gives the final answer: ( ) d n ( i, j) for all i, j V. ij 29

30 The Floyd-Warshall algorithm FLOYD-WARSHALL(W) 1 n rows[w] 2 D () W 3 for k 1 to n 4 do for i 1 to n 5 do for j 1 to n ( k) ( k1) ( k1) ( k1) 6 do dij min d ij, dik dkj 7 return D (n) costs Θ(n 3 ) time. 3

31 The Floyd-Warshall algorithm 31

32 The Floyd-Warshall algorithm 32

33 The Floyd-Warshall algorithm 33

34 The Floyd-Warshall algorithm Constructing A Shortest Path k ij Let be the predecessor of vertex j on a shortest path from vertex i with all intermediate vertices in {1, 2,, k}. () ij NIL if i if i i jor w ij jand w,. ij ( k ) ij ( ( k1) ij k1) ik if if d d ( k1) ij ( k1) ij d d ( k1) ik ( k1) ik d d ( k1) kj ( k1) kj,. 34

35 The Floyd-Warshall algorithm Transitive Closure of Graph Given a directed graph G = (V, E) with vertex set V = {1, 2,, n}. The transitive closure of G is defined as the graph G = (V, E ), where E = {(i, j) : there is a path from vertex i to vertex j in G}. 35

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati

MA 252: Data Structures and Algorithms Lecture 36. Partha Sarathi Mandal. Dept. of Mathematics, IIT Guwahati MA 252: Data Structures and Algorithms Lecture 36 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati The All-Pairs Shortest Paths Problem

More information

Chapter 25: All-Pairs Shortest-Paths

Chapter 25: All-Pairs Shortest-Paths Chapter : All-Pairs Shortest-Paths When no negative edges Some Algorithms Using Dijkstra s algorithm: O(V ) Using Binary heap implementation: O(VE lg V) Using Fibonacci heap: O(VE + V log V) When no negative

More information

Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis

Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2007 Simonas Šaltenis 3.2.12 simas@cs.aau.dk All-pairs shortest paths Main goals of the lecture: to go through one more example of dynamic programming

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no

More information

All Pairs Shortest Paths

All Pairs Shortest Paths All Pairs Shortest Paths Given a directed, connected weighted graph G(V, E), for each edge u, v E, a weight w(u, v) is associated with the edge. The all pairs of shortest paths problem (APSP) is to find

More information

Single Source Shortest Path (SSSP) Problem

Single Source Shortest Path (SSSP) Problem Single Source Shortest Path (SSSP) Problem Single Source Shortest Path Problem Input: A directed graph G = (V, E); an edge weight function w : E R, and a start vertex s V. Find: for each vertex u V, δ(s,

More information

from notes written mostly by Dr. Carla Savage: All Rights Reserved

from notes written mostly by Dr. Carla Savage: All Rights Reserved CSC 505, Fall 2000: Week 9 Objectives: learn about various issues related to finding shortest paths in graphs learn algorithms for the single-source shortest-path problem observe the relationship among

More information

Shortest Path Problem

Shortest Path Problem Shortest Path Problem CLRS Chapters 24.1 3, 24.5, 25.2 Shortest path problem Shortest path problem (and variants) Properties of shortest paths Algorithmic framework Bellman-Ford algorithm Shortest paths

More information

Shortest path problems

Shortest path problems Next... Shortest path problems Single-source shortest paths in weighted graphs Shortest-Path Problems Properties of Shortest Paths, Relaxation Dijkstra s Algorithm Bellman-Ford Algorithm Shortest-Paths

More information

Artificial Intelligence

Artificial Intelligence Artificial Intelligence Shortest Path Problem G. Guérard Department of Nouvelles Energies Ecole Supérieur d Ingénieurs Léonard de Vinci Lecture 3 GG A.I. 1/42 Outline 1 The Shortest Path Problem Introduction

More information

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶

Design and Analysis of Algorithms 演算法設計與分析. Lecture 13 December 18, 2013 洪國寶 Design and Analysis of Algorithms 演算法設計與分析 Lecture 13 December 18, 2013 洪國寶 1 Homework #10 1. 24.1-1 (p. 591 / p. 654) 2. 24.1-6 (p. 592 / p. 655) 3. 24.3-2 (p. 600 / p. 663) 4. 24.3-8 (p. 601) / 24.3-10

More information

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

CS420/520 Algorithm Analysis Spring 2009 Lecture 14 CS420/520 Algorithm Analysis Spring 2009 Lecture 14 "A Computational Analysis of Alternative Algorithms for Labeling Techniques for Finding Shortest Path Trees", Dial, Glover, Karney, and Klingman, Networks

More information

Lecture 11: Analysis of Algorithms (CS ) 1

Lecture 11: Analysis of Algorithms (CS ) 1 Lecture 11: Analysis of Algorithms (CS583-002) 1 Amarda Shehu November 12, 2014 1 Some material adapted from Kevin Wayne s Algorithm Class @ Princeton 1 2 Dynamic Programming Approach Floyd-Warshall Shortest

More information

Mon Tue Wed Thurs Fri

Mon Tue Wed Thurs Fri In lieu of recitations 320 Office Hours Mon Tue Wed Thurs Fri 8 Cole 9 Dr. Georg Dr. Georg 10 Dr. Georg/ Jim 11 Ali Jim 12 Cole Ali 1 Cole/ Shannon Ali 2 Shannon 3 Dr. Georg Dr. Georg Jim 4 Upcoming Check

More information

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest Paths. Nishant Mehta Lectures 10 and 11 Shortest Paths Nishant Mehta Lectures 0 and Communication Speeds in a Computer Network Find fastest way to route a data packet between two computers 6 Kbps 4 0 Mbps 6 6 Kbps 6 Kbps Gbps 00 Mbps 8 6 Kbps

More information

Shortest Paths. Nishant Mehta Lectures 10 and 11

Shortest Paths. Nishant Mehta Lectures 10 and 11 Shortest Paths Nishant Mehta Lectures 0 and Finding the Fastest Way to Travel between Two Intersections in Vancouver Granville St and W Hastings St Homer St and W Hastings St 6 Granville St and W Pender

More information

Algorithms for Data Science

Algorithms for Data Science Algorithms for Data Science CSOR W4246 Eleni Drinea Computer Science Department Columbia University Shortest paths in weighted graphs (Bellman-Ford, Floyd-Warshall) Outline 1 Shortest paths in graphs with

More information

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation The Shortest Path Problem jla,jc@imm.dtu.dk Department of Management Engineering Technical University of Denmark The Shortest Path Problem Given a directed network G = (V,E,w) for which the underlying

More information

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Dynamic Programming

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Dynamic Programming Computer Science 385 Design and Analysis of Algorithms Siena College Spring 29 Topic Notes: Dynamic Programming We next consider dynamic programming, a technique for designing algorithms to solve problems

More information

Graphs II - Shortest paths

Graphs II - Shortest paths Graphs II - Shortest paths Single Source Shortest Paths All Sources Shortest Paths some drawings and notes from prof. Tom Cormen Single Source SP Context: directed graph G=(V,E,w), weighted edges The shortest

More information

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1

Directed Graphs. DSA - lecture 5 - T.U.Cluj-Napoca - M. Joldos 1 Directed Graphs Definitions. Representations. ADT s. Single Source Shortest Path Problem (Dijkstra, Bellman-Ford, Floyd-Warshall). Traversals for DGs. Parenthesis Lemma. DAGs. Strong Components. Topological

More information

Sample Solutions to Homework #4

Sample Solutions to Homework #4 National Taiwan University Handout #25 Department of Electrical Engineering January 02, 207 Algorithms, Fall 206 TA: Zhi-Wen Lin and Yen-Chun Liu Sample Solutions to Homework #4. (0) (a) Both of the answers

More information

1 Non greedy algorithms (which we should have covered

1 Non greedy algorithms (which we should have covered 1 Non greedy algorithms (which we should have covered earlier) 1.1 Floyd Warshall algorithm This algorithm solves the all-pairs shortest paths problem, which is a problem where we want to find the shortest

More information

More Dynamic Programming Floyd-Warshall Algorithm (All Pairs Shortest Path Problem)

More Dynamic Programming Floyd-Warshall Algorithm (All Pairs Shortest Path Problem) More Dynamic Programming Floyd-Warshall Algorithm (All Pairs Shortest Path Problem) A weighted graph is a collection of points(vertices) connected by lines(edges), where each edge has a weight(some real

More information

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution Taking Stock IE170: Algorithms in Systems Engineering: Lecture 26 Jeff Linderoth Last Time Department of Industrial and Systems Engineering Lehigh University April 2, 2007 This Time Review! Jeff Linderoth

More information

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3? Multiple-choice Problems: Problem 1. Which of the following is true of functions =100+log and =+log? a) = b) =Ω c) =Θ d) All of the above e) None of the above Problem 2. Which of the following is true

More information

Analysis of Algorithms, I

Analysis of Algorithms, I Analysis of Algorithms, I CSOR W4231.002 Eleni Drinea Computer Science Department Columbia University Thursday, March 8, 2016 Outline 1 Recap Single-source shortest paths in graphs with real edge weights:

More information

Lecture 6 Basic Graph Algorithms

Lecture 6 Basic Graph Algorithms CS 491 CAP Intro to Competitive Algorithmic Programming Lecture 6 Basic Graph Algorithms Uttam Thakore University of Illinois at Urbana-Champaign September 30, 2015 Updates ICPC Regionals teams will be

More information

More Graph Algorithms: Topological Sort and Shortest Distance

More Graph Algorithms: Topological Sort and Shortest Distance More Graph Algorithms: Topological Sort and Shortest Distance Topological Sort The goal of a topological sort is given a list of items with dependencies, (ie. item 5 must be completed before item 3, etc.)

More information

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

( ) + n. ( ) = n 1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1 CSE 0 Name Test Summer 00 Last Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose you are sorting millions of keys that consist of three decimal

More information

Single Source Shortest Path

Single Source Shortest Path Single Source Shortest Path A directed graph G = (V, E) and a pair of nodes s, d is given. The edges have a real-valued weight W i. This time we are looking for the weight and the shortest path from s

More information

Lecture I: Shortest Path Algorithms

Lecture I: Shortest Path Algorithms Lecture I: Shortest Path Algorithms Dr Kieran T. Herley Department of Computer Science University College Cork October 201 KH (21/10/1) Lecture I: Shortest Path Algorithms October 201 1 / 28 Background

More information

Lecture 18 Solving Shortest Path Problem: Dijkstra s Algorithm. October 23, 2009

Lecture 18 Solving Shortest Path Problem: Dijkstra s Algorithm. October 23, 2009 Solving Shortest Path Problem: Dijkstra s Algorithm October 23, 2009 Outline Lecture 18 Focus on Dijkstra s Algorithm Importance: Where it has been used? Algorithm s general description Algorithm steps

More information

COL 702 : Assignment 1 solutions

COL 702 : Assignment 1 solutions COL 70 : Assignment 1 solutions 1(a ( n T (n = T + bn log n ( n = T + b n ( n log + bn log n ( n = T + b n ( n 8 log + b n log ( n + bn log n = bn log n + b n log n + bn log n +... log n terms = nb [(log

More information

CS 758/858: Algorithms

CS 758/858: Algorithms CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Wheeler Ruml (UNH) Class 17, CS 758 1 / 14 Problems Shortest Path Problems Wheeler Ruml (UNH) Class 17, CS 758 2 / 14 Problems Problems single source/destination

More information

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms Professor Henry Carter Fall 2016 Recap Space-time tradeoffs allow for faster algorithms at the cost of space complexity

More information

Lecture #10: RELAX(u, v, w) if d[v] >d[u]+w(u, v) then d[v] d[u]+w(u, v) π[v] u

Lecture #10: RELAX(u, v, w) if d[v] >d[u]+w(u, v) then d[v] d[u]+w(u, v) π[v] u Lecture #:.. raph lgorithms: hortest Path (hapter ) Problem iven a directed graph =[V,], and weight function w : R mapping edges to real valued weights. The weight of a path p = hv o,v,..., v k i is the

More information

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance

Dynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance Dynamic Programming: 1D Optimization Fibonacci Sequence To efficiently calculate F [x], the xth element of the Fibonacci sequence, we can construct the array F from left to right (or bottom up ). We start

More information

CS350: Data Structures Dijkstra s Shortest Path Alg.

CS350: Data Structures Dijkstra s Shortest Path Alg. Dijkstra s Shortest Path Alg. James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Shortest Path Algorithms Several different shortest path algorithms exist

More information

Parallel Graph Algorithms

Parallel Graph Algorithms Parallel Graph Algorithms Design and Analysis of Parallel Algorithms 5DV050/VT3 Part I Introduction Overview Graphs definitions & representations Minimal Spanning Tree (MST) Prim s algorithm Single Source

More information

n 2 ( ) ( ) + n is in Θ n logn

n 2 ( ) ( ) + n is in Θ n logn CSE Test Spring Name Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply an m n matrix and a n p matrix is in: A. Θ( n) B. Θ( max(

More information

LEC13: SHORTEST PATH. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee. Lecture slide courtesy of Prof.

LEC13: SHORTEST PATH. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee. Lecture slide courtesy of Prof. CSE 373 Analysis of Algorithms Fall 2016 Instructor: Prof. Sael Lee LEC13: SHORTEST PATH 1 SHORTEST PATHS Finding the shortest path between two nodes in a graph arises in many different applications: Transportation

More information

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20. Title Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis Nesrine Damak Fakultät für Informatik TU München 20. September 2010 Nesrine Damak: Classical Shortest-Path Algorithms 1/ 35

More information

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the Floyd-Warshall algorithm in O(n 3 ) time. Neither of

More information

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

CSC 373 Lecture # 3 Instructor: Milad Eftekhar Huffman encoding: Assume a context is available (a document, a signal, etc.). These contexts are formed by some symbols (words in a document, discrete samples from a signal, etc). Each symbols s i is occurred

More information

TIE Graph algorithms

TIE Graph algorithms TIE-20106 1 1 Graph algorithms This chapter discusses the data structure that is a collection of points (called nodes or vertices) and connections between them (called edges or arcs) a graph. The common

More information

Unit-5 Dynamic Programming 2016

Unit-5 Dynamic Programming 2016 5 Dynamic programming Overview, Applications - shortest path in graph, matrix multiplication, travelling salesman problem, Fibonacci Series. 20% 12 Origin: Richard Bellman, 1957 Programming referred to

More information

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

( ) ( ) C.  1 n. ( ) $ f n. ( ) B.  log( n! ) ( ) and that you already know ( ) ( )  % g( n) ( )  #& CSE 0 Name Test Summer 008 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time for the following code is in which set? for (i=0; i

More information

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Dynamic Programming Shabsi Walfish NYU - Fundamental Algorithms Summer 2006 Dynamic Programming What is Dynamic Programming? Technique for avoiding redundant work in recursive algorithms Works best with optimization problems that have a nice underlying structure Can often be used

More information

Data Structures in Java. Session 17 Instructor: Bert Huang

Data Structures in Java. Session 17 Instructor: Bert Huang Data Structures in Java Session 17 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3134 Announcements Homework 4 due Homework 5 posted All-pairs shortest paths Review Graphs Topological

More information

Optimization II: Dynamic Programming

Optimization II: Dynamic Programming Chapter 12 Optimization II: Dynamic Programming In the last chapter, we saw that greedy algorithms are efficient solutions to certain optimization problems. However, there are optimization problems for

More information

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1 Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm ADS: lects 14 & 15 slide 1 Weighted Graphs Definition 1 A weighted (directed or undirected graph) is a pair (G, W ) consisting

More information

ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo

ALGORITHM DESIGN DYNAMIC PROGRAMMING. University of Waterloo ALGORITHM DESIGN DYNAMIC PROGRAMMING University of Waterloo LIST OF SLIDES 1-1 List of Slides 1 2 Dynamic Programming Approach 3 Fibonacci Sequence (cont.) 4 Fibonacci Sequence (cont.) 5 Bottom-Up vs.

More information

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies: UNIT 5 CSE 103 - Unit V- Graph GRAPH Graph is another important non-linear data structure. In tree Structure, there is a hierarchical relationship between, parent and children that is one-to-many relationship.

More information

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4. Weighted Graphs 1 Outline (single-source) shortest path Dijkstra (Section 4.4) Bellman-Ford (Section 4.6) (all-pairs) shortest path Floyd-Warshall (Section 6.6) minimum spanning tree Kruskal (Section 5.1.3)

More information

Lecture 4: Graph Algorithms

Lecture 4: Graph Algorithms Lecture 4: Graph Algorithms Definitions Undirected graph: G =(V, E) V finite set of vertices, E finite set of edges any edge e = (u,v) is an unordered pair Directed graph: edges are ordered pairs If e

More information

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2

CSE 101, Winter Design and Analysis of Algorithms. Lecture 11: Dynamic Programming, Part 2 CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 11: Dynamic Programming, Part 2 Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: continue with DP (Knapsack, All-Pairs SPs, )

More information

END-TERM EXAMINATION

END-TERM EXAMINATION (Please Write your Exam Roll No. immediately) Exam. Roll No... END-TERM EXAMINATION Paper Code : MCA-205 DECEMBER 2006 Subject: Design and analysis of algorithm Time: 3 Hours Maximum Marks: 60 Note: Attempt

More information

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8 Fundamental Algorithms CSCI-GA.70-00/Summer 206 Solution to Homework 8 Problem (CLRS 23.-6). ( point) Show that a graph has a unique minimum spanning tree if, for every cut of the graph, there is a unique

More information

Lecture 14: Shortest Paths Steven Skiena

Lecture 14: Shortest Paths Steven Skiena Lecture 14: Shortest Paths Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Suppose we are given

More information

Shortest Path Algorithm

Shortest Path Algorithm Shortest Path Algorithm Shivani Sanan* 1, Leena jain 2, Bharti Kappor 3 *1 Assistant Professor, Faculty of Mathematics, Department of Applied Sciences 2 Associate Professor & Head- MCA 3 Assistant Professor,

More information

Total Points: 60. Duration: 1hr

Total Points: 60. Duration: 1hr CS800 : Algorithms Fall 201 Nov 22, 201 Quiz 2 Practice Total Points: 0. Duration: 1hr 1. (,10) points Binary Heap. (a) The following is a sequence of elements presented to you (in order from left to right):

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Graphs : Shortest Paths Marius Kloft Content of this Lecture Single-Source-Shortest-Paths: Dijkstra s Algorithm Single-Source-Single-Target All-Pairs Shortest Paths Transitive

More information

Dijkstra s Algorithm. Single source shortest paths for a directed acyclic graph

Dijkstra s Algorithm. Single source shortest paths for a directed acyclic graph Dijkstra s Algorithm Single source shortest paths for a directed acyclic graph Single-Source Shortest Paths We want to find the shortest paths between Binghamton and New York City, Boston, and Washington

More information

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24 /8/11 Single-Source Shortest Path Chapter 1 Shortest Paths Finding the shortest path between two nodes comes up in many applications o Transportation problems o Motion planning o Communication problems

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

1. Basic idea: Use smaller instances of the problem to find the solution of larger instances

1. Basic idea: Use smaller instances of the problem to find the solution of larger instances Chapter 8. Dynamic Programming CmSc Intro to Algorithms. Basic idea: Use smaller instances of the problem to find the solution of larger instances Example : Fibonacci numbers F = F = F n = F n- + F n-

More information

Parallel Graph Algorithms

Parallel Graph Algorithms Parallel Graph Algorithms Design and Analysis of Parallel Algorithms 5DV050 Spring 202 Part I Introduction Overview Graphsdenitions, properties, representation Minimal spanning tree Prim's algorithm Shortest

More information

Classical Shortest-Path Algorithms

Classical Shortest-Path Algorithms DISTANCE PROBLEMS IN NETWORKS - THEORY AND PRACTICE Classical Shortest-Path Algorithms Nesrine Damak October 10, 2010 Abstract In this work, we present four algorithms for the shortest path problem. The

More information

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths Outline Computer Science 33 Computation of Minimum-Cost Paths Dijkstra s Mike Jacobson Department of Computer Science University of Calgary Lecture #34 Dijkstra s to Find Min-Cost Paths 3 Example 4 5 References

More information

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM

UML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM NAME: This exam is open: - books - notes and closed: - neighbors - calculators ALGORITHMS QUALIFYING EXAM The upper bound on exam time is 3 hours. Please put all your work on the exam paper. (Partial credit

More information

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2)

Recursive-Fib(n) if n=1 or n=2 then return 1 else return Recursive-Fib(n-1)+Recursive-Fib(n-2) Dynamic Programming Any recursive formula can be directly translated into recursive algorithms. However, sometimes the compiler will not implement the recursive algorithm very efficiently. When this is

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Session 9. April 6, 009 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/7 Announcements Homework due by midnight tonight Homework 5 assigned Review Topological

More information

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n) CSE 0 Test Your name as it appears on your UTA ID Card Fall 0 Multiple Choice:. Write the letter of your answer on the line ) to the LEFT of each problem.. CIRCLED ANSWERS DO NOT COUNT.. points each. The

More information

) $ f ( n) " %( g( n)

) $ f ( n)  %( g( n) CSE 0 Name Test Spring 008 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to compute the sum of the n elements of an integer array is: # A.

More information

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2 Breadth-First Search, Slides for CIS DPV Chapter Jim Royer EECS October, 00 Definition In an undirected graph, the distance between two vertices is the length of the shortest path between them. (If there

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

( D. Θ n. ( ) f n ( ) D. Ο%

( D. Θ n. ( ) f n ( ) D. Ο% CSE 0 Name Test Spring 0 Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to run the code below is in: for i=n; i>=; i--) for j=; j

More information

Dynamic Programming Algorithms

Dynamic Programming Algorithms Dynamic Programming Algorithms Introduction In our study of divide-and-conquer algorithms, we noticed that a problem seemed conducive to a divide-and-conquer approach provided 1. it could be divided into

More information

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects

More information

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο CSE 220 Name Test Fall 20 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 4 points each. The time to compute the sum of the n elements of an integer array is in:

More information

All Shortest Paths. Questions from exercises and exams

All Shortest Paths. Questions from exercises and exams All Shortest Paths Questions from exercises and exams The Problem: G = (V, E, w) is a weighted directed graph. We want to find the shortest path between any pair of vertices in G. Example: find the distance

More information

LET G =(V (G), E(G)) be a finite connected unweighted

LET G =(V (G), E(G)) be a finite connected unweighted All-pairs shortest-paths problem for unweighted graphs in O(n 2 log n) time Udaya Kumar Reddy K. R, and K. Viswanathan Iyer Abstract Given a simple connected unweighted undirected graph G =(V (G), E(G))

More information

Dynamic Programming Version 1.0

Dynamic Programming Version 1.0 Dynamic Programming Version 1.0 1 1 Dynamic programming: the key idea The solution to many problems can be written as recurrences. Often these problems are optimization problems. This usually means that

More information

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.) CSE 0 Name Test Spring 006 Last 4 Digits of Student ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. Suppose f ( x) is a monotonically increasing function. Which of the

More information

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms Introduction to Parallel & Distributed Computing Parallel Graph Algorithms Lecture 16, Spring 2014 Instructor: 罗国杰 gluo@pku.edu.cn In This Lecture Parallel formulations of some important and fundamental

More information

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018 Object-oriented programming and data-structures CS/ENGRD 20 SUMMER 208 Lecture 3: Shortest Path http://courses.cs.cornell.edu/cs20/208su Graph Algorithms Search Depth-first search Breadth-first search

More information

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards

Dijkstra s Algorithm. Dijkstra s algorithm is a natural, greedy approach towards CPSC-320: Intermediate Algorithm Design and Analysis 128 CPSC-320: Intermediate Algorithm Design and Analysis 129 Dijkstra s Algorithm Dijkstra s algorithm is a natural, greedy approach towards solving

More information

CSE 5311 Notes 10: Medians/Selection

CSE 5311 Notes 10: Medians/Selection CSE Notes 0: Medians/Selection (Last updated 7/8/ :0 PM) MINIMUM AND MAXIMUM IN NO MORE THAN " n # COMPARISONS (CLRS 9.) Ordinary method takes n - comparisons:. Find minimum with n - comparisons.. Remove

More information

Graphs. There is no restriction on the relation E. Directed graphs also known as digraphs

Graphs. There is no restriction on the relation E. Directed graphs also known as digraphs 3: Graphs Graphs A directed graph is a pair (N,E) consisting of a set of nodes (or vertices) N, together with a relation E on N. Each element (n 1,n 2 ) of E is called an edge There is no restriction on

More information

Dijkstra s Algorithm

Dijkstra s Algorithm Dijkstra s Algorithm 7 1 3 1 Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example:

More information

Shortest Paths D E. Shortest Paths 1

Shortest Paths D E. Shortest Paths 1 Shortest Paths A 2 7 2 B C D 2 E F Shortest Paths Outline and Reading Weighted graphs ( 7.) Shortest path problem Shortest path properties Dijkstra s algorithm ( 7..) Algorithm Edge relaxation The Bellman-Ford

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

S Postgraduate Course on Signal Processing in Communications, FALL Topic: Iteration Bound. Harri Mäntylä

S Postgraduate Course on Signal Processing in Communications, FALL Topic: Iteration Bound. Harri Mäntylä S-38.220 Postgraduate Course on Signal Processing in Communications, FALL - 99 Topic: Iteration Bound Harri Mäntylä harri.mantyla@hut.fi ate: 11.10.1999 1. INTROUCTION...3 2. ATA-FLOW GRAPH (FG) REPRESENTATIONS...4

More information

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

& ( D.  mnp ' ( ) n 3. n 2. ( ) C.  n CSE Name Test Summer Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to multiply two n " n matrices is: A. " n C. "% n B. " max( m,n, p). The

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS45-05S-7 Shortest Path Dijkstra s Algorithm David Galles Department of Computer Science University of San Francisco 7-0: Computing Shortest Path Given a directed weighted

More information

Chapter 6. Dynamic Programming

Chapter 6. Dynamic Programming Chapter 6 Dynamic Programming CS 573: Algorithms, Fall 203 September 2, 203 6. Maximum Weighted Independent Set in Trees 6..0. Maximum Weight Independent Set Problem Input Graph G = (V, E) and weights

More information

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity. 1. T F: Consider a directed graph G = (V, E) and a vertex s V. Suppose that for all v V, there exists a directed path in G from s to v. Suppose that a DFS is run on G, starting from s. Then, true or false:

More information

Algebraic method for Shortest Paths problems

Algebraic method for Shortest Paths problems Lecture 1 (06.03.2013) Author: Jaros law B lasiok Algebraic method for Shortest Paths problems 1 Introduction In the following lecture we will see algebraic algorithms for various shortest-paths problems.

More information

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed. CS490 Quiz 1 NAME: STUDENT NO: SIGNATURE: This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed. Not all questions are of the same

More information