Lecture 6: Shortest distance, Eulerian cycles, Maxflow

Size: px
Start display at page:

Download "Lecture 6: Shortest distance, Eulerian cycles, Maxflow"

Transcription

1 DD2458, Problem Solving and Programming Under Pressure Lecture 6: Shortest distance, Eulerian cycles, Maxflow Date: Scribe(s): Adam Kull, David Vuorio Lecturer: Douglas Wikström 1 Shortest path from a given node Problem: Finding the shortest path from a source to every other node in a directed graph. As we saw in the previous lecture this can be solved efficiently with Dijkstra s algorithm, but with one condition: the graph has only positive edge weights. What happens if we try to run Dijkstra on a graph with negative weights? As Dijkstra is a greedy algorithm that only cares about the edges adjacent to the set of already visited vertices, it will not consider edges two steps away. As long as we only consider positive weights this will never be a problem. But when there are negative weights involved we are no longer guaranteed that we are choosing the best way in each step. This is because adding two edges together might actually produce a path with a smaller weight than just choosing the edge with the smallest value at the current step. Figure 1: Dijkstra As in figure?? we can see that Dijkstra would have chosen edge (b) because this currently has the smallest weight. But in reality the shortest path is via edge (a) and (c), as edge (c) is negative and thus the total weight will decrease. Dijkstra won t find this path as it does not "look" two steps away. Extending Dijkstra to look two edges ahead is however not a solution, as we might have to look arbitrary many edges away to find the shortest path. The solution to this is the Bellman-Ford algorithm. This algorithm works in a manner similar to Dijkstra, but it does so iteratively and updates the estimated distances several times. It will first update the distances to all the vertices using the 1

2 2 DD2458 Popup HT 2008 same approach as Dijkstra. We now know that these have the correct distances if we use a path with at most one edge. It will then continue to update all the vertices again, correcting distances along the way as it finds smaller distances. After each iteration i we know that the estimated distance for each vertex is correct if we use a path with at most i edges. This is repeated n 1 times, as the longest path in the graph has at most n 1 edges. So when the algorithm stops we are sure that each distance is correct as we are sure that we got the shortest distance for each vertex using at most i edges in each iteration and no path is longer than n 1 edges. Lemma: After i iterations in the for-loop is this true: If d(u) there exists a path of length d(u) from s to u. If there is a path from s to u with at most i edges, then d(u) is the length of a shortest path from s to u with at most i edges. By using this approach the distances will be corrected at each vertex as we find a path with a lower weight and the correct distances will propagate throughout the graph until we are sure that all the estimates are correct. Because we update all edges n 1 times the running time for this algorithm is O(nm), wich means that it is slower than Dijkstra. Algorithm 1: Bellman-Ford Input: A graph G with weighted edges and a start node s. Output: The minimum distance from a node s to all other nodes in G. BellmanFord(G, w( ), s) (1) p(u) and d(u) for u V. (2) d(s) 0 (3) for i = 1 to n 1 (4) foreach (u,v) E (5) if d(u) + w(u,v) < d(v) (6) d(v) d(u) + w(u,v) (7) p(v) u (8) return (d( ),p( )) Algorithm 2: Negative cycle detection. Input: A graph G with weighted edges and a start node s, and a Bellman- Ford solution (d( ),p( )) Output: true or false depending on whether G has a negative cycle or not. NegCycles(G,w( ),s,d( ),p( )) (1) foreach (u,v) E (2) if d(u) + w(u,v) < d(v) (3) return true (4) return false One problem arises when there is a negative cycle in the graph. A negative cycle means that there is some cycle in the graph that when we traverse it we will

3 Shortest distance, Eulerian cycles, Maxflow 3 end up with a lower total weight when we are back at the start node than we had when we started. This implies that going around and around in this cycle will result in an infinitely negative weight. This means that there is no shortest path between two nodes with a negative cycle somewhere between them as the total weight can always be made smaller. The presence of negative cycles can be quite easy to dectect in Bellman-Ford by running an extra iteration. When we are done with the n 1 iterations we know that all the vertices have correct values and therefore the extra iteration will not change any values. But if there are a negative cycle somewhere in the graph, running an additional iteration means that some vertices will be updated with a lower value as we can always walk this cycle one more time and always get a smaller value. 2 Shortest distance between all pairs of nodes If given any two nodes in a directed weighted graph, we would like to find the shortest path in between them, in the sense that it has a minimum total weight. One way to calculate this for all pairs of nodes would be to apply Dijkstra repeatedly, which works for non-negative weights. This takes O(nm log n) time. Alternatively, we can use Bellman-Ford repeatedly, which takes O(n 2 m) time. We note that applying any of these algorithms repeatedly leads to some amount of repeated efforts, so we may want to use dynamic programming instead. Let s define d k,u,v as the minimum distance from node u to node v, where only nodes 1,..., k (except u and v) can be passed. We can then make a recursive definition of d k,u,v like so: d 0,u,v = w(u, v) d k,u,v = min{d k 1,u,v, d k 1,u,k + d k 1,k,v } for k = 1,..., n d k 1,u,k + d k 1,k,v is the shortest path that includes node k (see figure??), whereas d k 1,u,v is the shortest path (if any, see figure??) that does not. Note that w(u, v) is considered to have an infinite value for any pairs of nodes that are not connected by an edge. d k 1,u,v will thus have a value of infinity if there is no path which does not include k. Figure 2: Shortest path, also considering node k Let s build an n 3 table D which in its final state has D[k, u, v] = d k,u,v,. Similarly we will have a table where P[k, u, v] denotes the last node in an optimal

4 4 DD2458 Popup HT 2008 Figure 3: Shortest path, not considering node k Algorithm 3: Floyd-Warshall, intialization Input: A graph G with weighted edges. Output: Nothing. Init(G) (1) foreach u V (2) foreach v V (3) for k = 0 to n (4) D[k,u,v] (5) P[k,u,v] (6) foreach u V (7) foreach k V (8) D[k,u,u] 0 (9) foreach (u,v) E (10) D[0,u,v] w(u,v) (11) P[0,u,v] u way from u to v, using only nodes 1,..., k. Then we can find the optimal way by backtracking. We could optimize the algorithm to require only n 2 space, since all updates only refer to D[k 1,, ] or P[k 1,, ]. Any earlier values can be thrown away. Note that u belongs to a negative cycle precisely if D[k, u, u] < Johnson s algorithm Floyd-Warshall takes time O(n 3 ), so Dijkstra (O(nm log n)) is faster for sparse graphs, here defined as m < n 2 /log n. Since Dijkstra cannot handle negative weights, we will need to make a trick first. This consist of adding a new node to the graph (see figure??), which is connected to all other nodes. Then, using Bellman-Ford, we will find out the minimum distance to every node from the "fake node". Using this information we can create a new set of non-negative weights for each edge and apply Dijkstra with that. (Obviously this strategy is not working if any negative cycles are involved.) As illustrated in figure?? we can have many paths between a source node s and a target node t, however the manipulations we apply (w (u, v) = w(u, v) + d(u) d(v)) at each step along the way are cancelled out (as indicated in the figure, the

5 Shortest distance, Eulerian cycles, Maxflow 5 Algorithm 4: Floyd-Warshall, complete algorithm. Input: A graph G with weighted edges. Output: The shortest path between each pair of nodes. Floyd-Warshall(G) (1) Init() (2) for k = 1 to n (3) foreach u V (4) if D[k 1,u,k] < (5) foreach v V (6) if D[k 1,u,k] + D[k 1,k,v] D[k 1,u,v] (7) D[k,u,v] D[k 1,u,v] (8) P[k,u,v] P[k 1,u,v] (9) else (10) D[k,u,v] D[k 1,u,k] + D[k 1,k,v] (11) D[k,u,v] P[k 1,k,v] (12) return (D[n,, ],P[n,, ]) Figure 4: Johnson s algorithm, adding a "fake node" Figure 5: Johnson s algorithm, creating new weights

6 6 DD2458 Popup HT 2008 Algorithm 5: Johnson s algorithm Input: A graph G with weighted edges. Output: The shortest path between all pairs of nodes. Johnson(G) (1) G (V {n + 1},E {(n + 1,u) : u V }) (2) (d,p) BellmanFord(G, w( ), n + 1) (3) Let w (u,v) = w(u,v) + d(u) d(v) (4) foreach u V (5) (D[u], P[u]) Dijkstra(G, u, w ) (6) return (D, P) sum of the new weights will be d(s) d(t)), leaving only d(s) and d(t) significant. In effect, this will make sure that a shortest path in the original graph is also shortest in the new graph. 3 Eulerian Path Algorithm 6: Eulerian Path Input: A graph G = (V,E). Output: An Eulerian path. EulerPath(G, u) (1) vis(e) = false for e E. (2) return EulerPathInner(G,u) In a graph (directed or undirected), an Eulerian path is a path in the graph that uses each edge exactly once. An Eulerian cycle is an Eulerian path which ends where it begins. (Observe the difference between an Eulerian cycle and a Hamilton cycle. The latter visits each node exactly once and is NP-hard to find.) Now, let G = (V, E) be an undirected graph. Determine if there is an Eulerian path and find it if there is one. We assume that isolated nodes are not present in the graph as these never have to be visited. Then it follows that: An undirected graph has: an Eulerian path if it s connected and all nodes except at most two have an even number of edges, and an Eulerian cycle if all nodes have an even number of edges. A directed graph has: an Eulerian path if it s connected, all nodes except at most two have as many incoming edges as outgoing edges, and the two special nodes (if any) have exactly one excessive incoming / outgoing edge each (e.g. one more outgoing edge than incoming edges), and

7 Shortest distance, Eulerian cycles, Maxflow 7 an Eulerian cycle if additionally all nodes have as many incoming as outgoing edges. Algorithm?? works by finding cycles in the graph until all nodes have been visited, and then connecting them as is illustrated in figure??. Figure 6: Eulerian cycle 4 Max flow Definition: A flow in a directed, weighted graph G = (V, E) from s V to t V is a function f : E R so that 0 f(e) w(e) for e E, and e In(u) f(e) = e Out(u) f(e) for each u s, t. The flow in a graph from one vertex (often called the source) to another vertex (the sink) can be understood as viewing the edges in a graph as pipes that we want to fill with as much water as possible. The weights of the edges then represents the capacity of the pipes. Using this analogy, finding the max flow means that we want to know how much water we can pump out from the source without overflowing the network. One consequence of this analogy is that the net flow in each vertex is always 0 (except for the source and sink, where the flow out of the source is equal to the flow into the sink), i.e. the flow out from a vertex is always as large as the flow into it. To be able to undo pushing flow through an edge, we add an edge in the opposite direction for every edge already in the graph. This starts with capacity 0 when the flow through the original edge is 0, and increases as the flow in the original edge increases. Going through this residue edge represents lowering the flow in the original edge. An algorithm for finding the maximum flow through a graph is Ford-Fulkerson. It works by finding a path from the source to sink where there still exists some unused capacity (easily done with BFS). Then we find the smallest capacity available along this path, and then fill each edge with this flow. This is then repeated until there are no paths to the sink with more capacity available (including residue

8 8 DD2458 Popup HT 2008 edges), at which point we know that we have pushed as much flow through the graph as we can. When running this algorithm a path can be found in O(m) time and, at least when dealing with integer capacities, in each step we increase the flow by at least 1. This means that the algorithm has a complexity of O(mf), where f is the maximum flow in the graph.

9 Shortest distance, Eulerian cycles, Maxflow 9 EulerPathInner(G, u) (1) if vis(u,v) = true for all (u,v) E(u) (2) return (3) else (4) Take (u,v) E with vis(u,v) = false. (5) vis(u, v) true. (6) x EulerPathInner(G,v) (7) t Conc(((u,v)),x) (8) p (9) foreach (u,v) E with vis(u,v) = false (10) x EulerPathInner(G,u) (11) p Conc(p,x) (12) return Conc(p,t) FlowFinder() (1) Let p = (e 1,...,e k ) with e 1 Out(s),e k In(t) and w(e i ) f(e i ) > 0 if possible, otherwise p =. (2) return p Algorithm 7: Ford-Fulkerson Input: A graph G = (V,E). Output: A maximum flow f. FordFulkerson(G) (1) f(e) 0 for e E. (2) while true (3) p FlowFinder() (4) if p = (5) return f (6) else (7) Expand (e 1,...,e k ) from p. (8) d min i {w(e i ) f(e i )} (9) for i = 1 to k (10) f(e i ) f(e i ) + d (11) f(e 1 i ) f(e 1 i ) d

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

Lecture 5: Graph algorithms 1

Lecture 5: Graph algorithms 1 DD2458, Problem Solving and Programming Under Pressure Lecture 5: Graph algorithms 1 Date: 2008-10-01 Scribe(s): Mikael Auno and Magnus Andermo Lecturer: Douglas Wikström This lecture presents some common

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

CSE 417 Network Flows (pt 4) Min Cost Flows

CSE 417 Network Flows (pt 4) Min Cost Flows CSE 417 Network Flows (pt 4) Min Cost Flows Reminders > HW6 is due Monday Review of last three lectures > Defined the maximum flow problem find the feasible flow of maximum value flow is feasible if it

More information

Lecture 3: Graphs and flows

Lecture 3: Graphs and flows Chapter 3 Lecture 3: Graphs and flows Graphs: a useful combinatorial structure. Definitions: graph, directed and undirected graph, edge as ordered pair, path, cycle, connected graph, strongly connected

More information

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14 CS200: Graphs Rosen Ch. 9.1-9.4, 9.6, 10.4-10.5 Walls and Mirrors Ch. 14 Trees as Graphs Tree: an undirected connected graph that has no cycles. A B C D E F G H I J K L M N O P Rooted Trees A rooted tree

More information

1 Dynamic Programming

1 Dynamic Programming CS161 Lecture 13 Dynamic Programming and Greedy Algorithms Scribe by: Eric Huang Date: May 13, 2015 1 Dynamic Programming The idea of dynamic programming is to have a table of solutions of subproblems

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

Weighted Graph Algorithms Presented by Jason Yuan

Weighted Graph Algorithms Presented by Jason Yuan Weighted Graph Algorithms Presented by Jason Yuan Slides: Zachary Friggstad Programming Club Meeting Weighted Graphs struct Edge { int u, v ; int w e i g h t ; // can be a double } ; Edge ( int uu = 0,

More information

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014

Lecture #7. 1 Introduction. 2 Dijkstra s Correctness. COMPSCI 330: Design and Analysis of Algorithms 9/16/2014 COMPSCI 330: Design and Analysis of Algorithms 9/16/2014 Lecturer: Debmalya Panigrahi Lecture #7 Scribe: Nat Kell 1 Introduction In this lecture, we will further examine shortest path algorithms. We will

More information

1 More on the Bellman-Ford Algorithm

1 More on the Bellman-Ford Algorithm CS161 Lecture 12 Shortest Path and Dynamic Programming Algorithms Scribe by: Eric Huang (2015), Anthony Kim (2016), M. Wootters (2017) Date: May 15, 2017 1 More on the Bellman-Ford Algorithm We didn t

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

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

Routing Protocols and the IP Layer

Routing Protocols and the IP Layer Routing Protocols and the IP Layer CS244A Review Session 2/0/08 Ben Nham Derived from slides by: Paul Tarjan Martin Casado Ari Greenberg Functions of a router Forwarding Determine the correct egress port

More information

Solving problems on graph algorithms

Solving problems on graph algorithms Solving problems on graph algorithms Workshop Organized by: ACM Unit, Indian Statistical Institute, Kolkata. Tutorial-3 Date: 06.07.2017 Let G = (V, E) be an undirected graph. For a vertex v V, G {v} is

More information

Last week: Breadth-First Search

Last week: Breadth-First Search 1 Last week: Breadth-First Search Set L i = [] for i=1,,n L 0 = {w}, where w is the start node For i = 0,, n-1: For u in L i : For each v which is a neighbor of u: If v isn t yet visited: - mark v as visited,

More information

Jessica Su (some parts copied from CLRS / last quarter s notes)

Jessica Su (some parts copied from CLRS / last quarter s notes) 1 Max flow Consider a directed graph G with positive edge weights c that define the capacity of each edge. We can identify two special nodes in G: the source node s and the sink node t. We want to find

More information

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016 CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016 1 Depth first search 1.1 The Algorithm Besides breadth first search, which we saw in class in relation to Dijkstra s algorithm, there

More information

Algorithm Circle Extra Lecture: Solving the Assignment Problem with Network Flow

Algorithm Circle Extra Lecture: Solving the Assignment Problem with Network Flow The Network Flow Minimum-Cost Maximum Flow Shameless Plug Algorithm Circle Extra Lecture: Solving the with Network Flow 17 September 2012 The Network Flow Minimum-Cost Maximum Flow Shameless Plug Outline

More information

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley Single Source Shortest Path: The Bellman-Ford Algorithm Slides based on Kevin Wayne / Pearson-Addison Wesley Single Source Shortest Path Problem Shortest path network. Directed graph G = (V, E, w). Weight

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

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

CS261: Problem Set #1

CS261: Problem Set #1 CS261: Problem Set #1 Due by 11:59 PM on Tuesday, April 21, 2015 Instructions: (1) Form a group of 1-3 students. You should turn in only one write-up for your entire group. (2) Turn in your solutions by

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

Dynamic Programming II

Dynamic Programming II Lecture 11 Dynamic Programming II 11.1 Overview In this lecture we continue our discussion of dynamic programming, focusing on using it for a variety of path-finding problems in graphs. Topics in this

More information

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15 600.363 Introduction to Algorithms / 600.463 Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15 14.1 Introduction Today we re going to talk about algorithms for computing shortest

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

Communication Networks I December 4, 2001 Agenda Graph theory notation Trees Shortest path algorithms Distributed, asynchronous algorithms Page 1

Communication Networks I December 4, 2001 Agenda Graph theory notation Trees Shortest path algorithms Distributed, asynchronous algorithms Page 1 Communication Networks I December, Agenda Graph theory notation Trees Shortest path algorithms Distributed, asynchronous algorithms Page Communication Networks I December, Notation G = (V,E) denotes a

More information

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4. 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.) to produce an ordering of the items that satisfies the given

More information

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College Laura Toma Algorithms (csci2200), Bowdoin College Topics Weighted graphs each edge (u, v) has a weight denoted w(u, v) or w uv stored in the adjacency list or adjacency matrix The weight of a path p =

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

Preflow-Push Algorithm

Preflow-Push Algorithm Preflow-Push Algorithm An s-t preflow obeys the following constraints: Capacity constraints: 0 f(e) c(e) e f (v) = f (u,v) f (v,w) 0 u V w V A labeling h assigns nonnegative integers to vertices. h(t)

More information

Datenstrukturen und Algorithmen

Datenstrukturen und Algorithmen 1 Datenstrukturen und Algorithmen Exercise 9 FS 2018 Program of today 2 1 Feedback of last exercise 2 Repetition theory 3 Programming Task 1. Feedback of last exercise 3 Topological Sorting 4 A B Graph

More information

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems Chapter 24. Shortest path problems We are given a directed graph G = (V,E) with each directed edge (u,v) E having a weight, also called a length, w(u,v) that may or may not be negative. A shortest path

More information

1 Dijkstra s Algorithm

1 Dijkstra s Algorithm Lecture 11 Dijkstra s Algorithm Scribes: Himanshu Bhandoh (2015), Virginia Williams, and Date: November 1, 2017 Anthony Kim (2016), G. Valiant (2017), M. Wootters (2017) (Adapted from Virginia Williams

More information

Minimum Cost Edge Disjoint Paths

Minimum Cost Edge Disjoint Paths Minimum Cost Edge Disjoint Paths Theodor Mader 15.4.2008 1 Introduction Finding paths in networks and graphs constitutes an area of theoretical computer science which has been highly researched during

More information

Source. Sink. Chapter 10: Iterative Programming Maximum Flow Problem. CmSc250 Intro to Algorithms

Source. Sink. Chapter 10: Iterative Programming Maximum Flow Problem. CmSc250 Intro to Algorithms Chapter 10: Iterative Programming Maximum Flow Problem CmSc20 Intro to Algorithms A flow network is a model of a system where some material is produced at its source, travels through the system, and is

More information

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M. Lecture 9 Graphs Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M. Wootters (2017) 1 Graphs A graph is a set of vertices

More information

CS2210 Data Structures and Algorithms

CS2210 Data Structures and Algorithms S1 ata Structures and Algorithms Lecture 1 : Shortest Paths A 4 1 5 5 3 4 Goodrich, Tamassia Outline Weighted Graphs Shortest Paths Algorithm (ijkstra s) Weighted Graphs ach edge has an associated numerical

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

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer Science & Engineering 423/823 Design and Analysis of Algorithms Computer Science & Engineering 423/823 Design and Analysis of Algorithms Lecture 07 Single-Source Shortest Paths (Chapter 24) Stephen Scott and Vinodchandran N. Variyam sscott@cse.unl.edu 1/36 Introduction

More information

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs Shortest paths in graphs Remarks from previous lectures: Path length in unweighted graph equals to edge count on the path Oriented distance (δ(u, v)) between vertices u, v equals to the length of the shortest

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

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

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

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

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

CMPSCI 311: Introduction to Algorithms Practice Final Exam

CMPSCI 311: Introduction to Algorithms Practice Final Exam CMPSCI 311: Introduction to Algorithms Practice Final Exam Name: ID: Instructions: Answer the questions directly on the exam pages. Show all your work for each question. Providing more detail including

More information

Addis Ababa University, Amist Kilo July 20, 2011 Algorithms and Programming for High Schoolers. Lecture 12

Addis Ababa University, Amist Kilo July 20, 2011 Algorithms and Programming for High Schoolers. Lecture 12 Addis Ababa University, Amist Kilo July 20, 2011 Algorithms and Programming for High Schoolers Single-source shortest paths: Lecture 12 We saw in Lab 10 that breadth-first search can be used to find the

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

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

Lecture and notes by: Sarah Fletcher and Michael Xu November 3rd, Multicommodity Flow

Lecture and notes by: Sarah Fletcher and Michael Xu November 3rd, Multicommodity Flow Multicommodity Flow 1 Introduction Suppose we have a company with a factory s and a warehouse t. The quantity of goods that they can ship from the factory to the warehouse in a given time period is limited

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

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar..

Algorithms on Graphs: Part III. Shortest Path Problems. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar.. .. Cal Poly CSC 349: Design and Analyis of Algorithms Alexander Dekhtyar.. Shortest Path Problems Algorithms on Graphs: Part III Path in a graph. Let G = V,E be a graph. A path p = e 1,...,e k, e i E,

More information

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs). Lecture 13 Graph Algorithms I 13.1 Overview This is the first of several lectures on graph algorithms. We will see how simple algorithms like depth-first-search can be used in clever ways (for a problem

More information

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler Lecture 17: Weighted Graphs, Shortest Paths: Dijkstra s Algorithm A 4 B 7 C 1 D 3 5 3 9 5 E Courtesy of Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Weighted Graphs In a weighted graph, each

More information

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456 5.4 Shortest Paths CH08-320201: Algorithms and Data Structures 456 Definitions: Path Consider a directed graph G=(V,E), where each edge e є E is assigned a non-negative weight w: E -> R +. A path is a

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

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

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a

More information

Shortest Paths: Basics. Algorithms and Networks 2016/2017 Johan M. M. van Rooij Hans L. Bodlaender

Shortest Paths: Basics. Algorithms and Networks 2016/2017 Johan M. M. van Rooij Hans L. Bodlaender Shortest Paths: Basics Algorithms and Networks 2016/2017 Johan M. M. van Rooij Hans L. Bodlaender 1 Shortest path problem(s) Undirected single-pair shortest path problem Given a graph G=(V,E) and a length

More information

Detecting negative cycles with Tarjan s breadth-first scanning algorithm

Detecting negative cycles with Tarjan s breadth-first scanning algorithm Detecting negative cycles with Tarjan s breadth-first scanning algorithm Tibor Ásványi asvanyi@inf.elte.hu ELTE Eötvös Loránd University Faculty of Informatics Budapest, Hungary Abstract The Bellman-Ford

More information

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14 1 Depth first search 1.1 The Algorithm Besides breadth first search, which we saw in class in relation to Dijkstra s algorithm, there is one

More information

Some Extra Information on Graph Search

Some Extra Information on Graph Search Some Extra Information on Graph Search David Kempe December 5, 203 We have given basic definitions of graphs earlier in class, and talked about how to implement them. We had also already mentioned paths,

More information

Minimum Spanning Tree

Minimum Spanning Tree Minimum Spanning Tree 1 Minimum Spanning Tree G=(V,E) is an undirected graph, where V is a set of nodes and E is a set of possible interconnections between pairs of nodes. For each edge (u,v) in E, we

More information

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1 CS200: Graphs Prichard Ch. 14 Rosen Ch. 10 CS200 - Graphs 1 Graphs A collection of nodes and edges What can this represent? n A computer network n Abstraction of a map n Social network CS200 - Graphs 2

More information

COMP 251 Winter 2017 Online quizzes with answers

COMP 251 Winter 2017 Online quizzes with answers COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots

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

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

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

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

CHAPTER 13 GRAPH ALGORITHMS

CHAPTER 13 GRAPH ALGORITHMS CHAPTER 13 GRAPH ALGORITHMS SFO LAX ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 00) AND SLIDES FROM NANCY

More information

Shortest Paths: Algorithms for standard variants. Algorithms and Networks 2017/2018 Johan M. M. van Rooij Hans L. Bodlaender

Shortest Paths: Algorithms for standard variants. Algorithms and Networks 2017/2018 Johan M. M. van Rooij Hans L. Bodlaender Shortest Paths: Algorithms for standard variants Algorithms and Networks 2017/2018 Johan M. M. van Rooij Hans L. Bodlaender 1 Shortest path problem(s) Undirected single-pair shortest path problem Given

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

PATH FINDING AND GRAPH TRAVERSAL

PATH FINDING AND GRAPH TRAVERSAL GRAPH TRAVERSAL PATH FINDING AND GRAPH TRAVERSAL Path finding refers to determining the shortest path between two vertices in a graph. We discussed the Floyd Warshall algorithm previously, but you may

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

Optimization Methods in Management Science

Optimization Methods in Management Science Optimization Methods in Management Science MIT 15.053 Recitation 9 TAs: Giacomo Nannicini, Ebrahim Nasrabadi Problem 1 We apply Dijkstra s algorithm to the network given below to find the shortest path

More information

Graphs & Digraphs Tuesday, November 06, 2007

Graphs & Digraphs Tuesday, November 06, 2007 Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements

More information

Graphs II: Trailblazing

Graphs II: Trailblazing Graphs II: Trailblazing Paths In an undirected graph, a path of length n from u to v, where n is a positive integer, is a sequence of edges e 1,, e n of the graph such that f(e 1 )={x 0,x 1 }, f(e 2 )={x

More information

1 Introduction. 2 The Generic Push-Relabel Algorithm. Improvements on the Push-Relabel Method: Excess Scaling. 2.1 Definitions CMSC 29700

1 Introduction. 2 The Generic Push-Relabel Algorithm. Improvements on the Push-Relabel Method: Excess Scaling. 2.1 Definitions CMSC 29700 CMSC 9700 Supervised Reading & Research Week 6: May 17, 013 Improvements on the Push-Relabel Method: Excess Scaling Rahul Mehta 1 Introduction We now know about both the methods based on the idea of Ford

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

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

CS 125 Section #10 Midterm 2 Review 11/5/14

CS 125 Section #10 Midterm 2 Review 11/5/14 CS 125 Section #10 Midterm 2 Review 11/5/14 1 Topics Covered This midterm covers up through NP-completeness; countability, decidability, and recognizability will not appear on this midterm. Disclaimer:

More information

Algorithm Design (8) Graph Algorithms 1/2

Algorithm Design (8) Graph Algorithms 1/2 Graph Algorithm Design (8) Graph Algorithms / Graph:, : A finite set of vertices (or nodes) : A finite set of edges (or arcs or branches) each of which connect two vertices Takashi Chikayama School of

More information

Computational Methods in IS Research Fall Graph Algorithms Network Flow Problems

Computational Methods in IS Research Fall Graph Algorithms Network Flow Problems Computational Methods in IS Research Fall 2017 Graph Algorithms Network Flow Problems Nirmalya Roy Department of Information Systems University of Maryland Baltimore County www.umbc.edu Network Flow Problems

More information

Section authors: Hamilton Clower, David Scott, Ian Dundore.

Section authors: Hamilton Clower, David Scott, Ian Dundore. 2.6.1 Dijkstra s Algorithm Section authors: Hamilton Clower, David Scott, Ian Dundore. Strategy Specialized 1.4 Greedy 1.7 Edge Comparison Based 3.2 Single-Source Shortest-Paths 3.7 Dijkstras Algorithm

More information

Solutions for the Exam 6 January 2014

Solutions for the Exam 6 January 2014 Mastermath and LNMB Course: Discrete Optimization Solutions for the Exam 6 January 2014 Utrecht University, Educatorium, 13:30 16:30 The examination lasts 3 hours. Grading will be done before January 20,

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Graphs: Introduction and First Algorithms Ulf Leser This Course Introduction 2 Abstract Data Types 1 Complexity analysis 1 Styles of algorithms 1 Lists, stacks, queues 2

More information

Shortest Path Algorithms

Shortest Path Algorithms Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch] 1 Single Source Shortest Path Given: a directed or undirected graph G = (V,E) a source node s in V a weight function w: E

More information

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo CSE 431/531: Analysis of Algorithms Greedy Algorithms Lecturer: Shi Li Department of Computer Science and Engineering University at Buffalo Main Goal of Algorithm Design Design fast algorithms to solve

More information

The Shortest Path Problem

The Shortest Path Problem The Shortest Path Problem 1 Shortest-Path Algorithms Find the shortest path from point A to point B Shortest in time, distance, cost, Numerous applications Map navigation Flight itineraries Circuit wiring

More information

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory Dustin Tseng Mike Li Wednesday January 16, 2006 Dustin Tseng Mike Li: CS490: Problem Solving in Computer Science, Lecture

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

Solutions. Benelux Algorithm Programming Contest October 24, Universiteit Leiden. Solutions BAPC 2015 October 24, / 14

Solutions. Benelux Algorithm Programming Contest October 24, Universiteit Leiden. Solutions BAPC 2015 October 24, / 14 Solutions Benelux Algorithm Programming Contest 2015 Universiteit Leiden October 24, 2015 Solutions BAPC 2015 October 24, 2015 1 / 14 A: Freight Train Do a binary search for the minimum length of the longest

More information

Introduction to Algorithms. Lecture 11

Introduction to Algorithms. Lecture 11 Introduction to Algorithms Lecture 11 Last Time Optimization Problems Greedy Algorithms Graph Representation & Algorithms Minimum Spanning Tree Prim s Algorithm Kruskal s Algorithm 2 Today s Topics Shortest

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

CSE 417 Network Flows (pt 3) Modeling with Min Cuts

CSE 417 Network Flows (pt 3) Modeling with Min Cuts CSE 417 Network Flows (pt 3) Modeling with Min Cuts Reminders > HW6 is due on Friday start early bug fixed on line 33 of OptimalLineup.java: > change true to false Review of last two lectures > Defined

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

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Graphs: Introduction Ulf Leser This Course Introduction 2 Abstract Data Types 1 Complexity analysis 1 Styles of algorithms 1 Lists, stacks, queues 2 Sorting (lists) 3 Searching

More information

CS261: Problem Set #2

CS261: Problem Set #2 CS261: Problem Set #2 Due by 11:59 PM on Tuesday, February 9, 2016 Instructions: (1) Form a group of 1-3 students. You should turn in only one write-up for your entire group. (2) Submission instructions:

More information