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

Similar documents
1 Summation Formulae, Induction and Bounding. 2 How to compare functions: o, ω, O, Ω, Θ. 3 How to count the running time of algorithms

Minimum Spanning Trees

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 16. Graph Search Algorithms. Recall BFS

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 17. Depth-First Search. DFS (Initialize and Go) Last Time Depth-First Search

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Introduction to Algorithms. Lecture 11

Minimum Spanning Trees Ch 23 Traversing graphs

Graph Representation

Unit 2: Algorithmic Graph Theory

Graph representation

Chapter 22. Elementary Graph Algorithms

Single Source Shortest Path (SSSP) Problem

Representations of Graphs

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

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

Unit 5F: Layout Compaction

Unit 3: Layout Compaction

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Elementary Graph Algorithms

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Design and Analysis of Algorithms

DFS & STRONGLY CONNECTED COMPONENTS

Shortest Path Problem

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Basic Graph Algorithms

Lecture 6 Basic Graph Algorithms

Graph: representation and traversal

Shortest path problems

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

Graph Algorithms. Definition

Lecture Notes for Chapter 23: Minimum Spanning Trees

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

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

Lecture 11: Analysis of Algorithms (CS ) 1

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

CSI 604 Elementary Graph Algorithms

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Sample Solutions to Homework #4

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

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Graph Algorithms: Chapters Part 1: Introductory graph concepts

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Graph Algorithms (part 3 of CSC 282),

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

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

6.006 Introduction to Algorithms Spring 2008

Graphs. Graphs. Representation of Graphs. CSE 680 Prof. Roger Crawfis. Two standard ways. Graph G = (V, E)» V = set of vertices

Single Source Shortest Path

Quiz 2 Practice Problems

COMP251: Single source shortest paths

Minimum Spanning Tree (undirected graph)

CS473-Algorithms I. Lecture 14-A. Graph Searching: Breadth-First Search. Cevdet Aykanat - Bilkent University Computer Engineering Department

Lecture 3: Graphs and flows

CS521 \ Notes for the Final Exam

Proof: if not f[u] < d[v], then u still grey while v is being visited. DFS visit(v) will then terminate before DFS visit(u).

Shortest Paths. Nishant Mehta Lectures 10 and 11

Lecture 10: Analysis of Algorithms (CS ) 1

Graph: representation and traversal

CS 270 Algorithms. Oliver Kullmann. Breadth-first search. Analysing BFS. Depth-first. search. Analysing DFS. Dags and topological sorting.

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Shortest Paths. Nishant Mehta Lectures 10 and 11

CHAPTER 23: ELEMENTARY GRAPH ALGORITHMS Representations of graphs

Minimum Spanning Trees

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Minimum Spanning Trees

Solutions to relevant spring 2000 exam problems

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

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

CS Elementary Graph Algorithms

CS Elementary Graph Algorithms

Single Source Shortest Paths

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

COT 6405 Introduction to Theory of Algorithms

Algorithm Design and Analysis

Graph Algorithms (part 3 of CSC 282),

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

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

Graphs. Part I: Basic algorithms. Laura Toma Algorithms (csci2200), Bowdoin College

Outline. Graphs. Divide and Conquer.

Graph implementations :

Parallel Graph Algorithms

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.

Week 12: Minimum Spanning trees and Shortest Paths

Minimum Spanning Tree

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Minimum Spanning Trees

Minimum Spanning Trees

TIE Graph algorithms

Graph Search. Adnan Aziz

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

W4231: Analysis of Algorithms

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

Design and Analysis of Algorithms

Konigsberg Bridge Problem

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Transcription:

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 (Lehigh University) IE170:Lecture 26 Lecture Notes 1 / 16 Stuff We Learned Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 2 / 16 Dynamic Programming Dynamic Programming (15.[1,3]) Greedy Algorithms (16.[1,2]) Graphs and Search (22.*) Spanning Trees (23.*) (Single Source) Shortest Paths (24.[1,2,3]) (All Pairs) Shortest Paths (25.[1,2]) Max Flow (26.[1,2,3]) Dynamic Programming in a Nutshell 1 Characterize the structure of an optimal solution 2 Recursively define the value of an optimal solution 3 Compute the value of an optimal solution from the bottum up 4 Construct optimal solution (if required) Examples Assembly Line Balancing Lot Sizing Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 3 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 4 / 16

Assembly Line Balancing Let f i (j) be the fastest time to get through S i = 1, 2 j = 1, 2,... n Lot Sizing f = min(f 1 (n) + x 1, f 2 (n) + x 2 ) f 1 (1) = e 1 + a 11 f 2 (1) = e 2 + a 21 f 1 (j) = min(f 1 (j 1) + a 1j, f 2 (j 1) + t 2,j 1 + a 1j ) f 2 (j) = min(f 2 (j 1) + a 2j, f 1 (j 1) + t 1,j 1 + a 2j ) Let f t (s): be the minimum cost of meeting demands from t, t + 1,... T (t until the end) if s units are in inventory at the beginning of period t f t (s) = min {c t(x) + h t (s + x d t ) + f t+1 (s + x d t )}. x 0,1,2,... Greedy Greedy is not always optimal! But it sometimes works: Activity Selection Let S A be the set of activities that start after activity i needs to finish and before activity j needs to start: S def = {k S f i s k, f k s j } Let s assume that we have sorted the activities such that Schedule jobs in S 0,n+1 f 1 f 2 f n Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 5 / 16 c be the size of a maximum-sized subset of mutually compatible jobs in S. If S =, then c = 0 If S, then c = c ik + 1 + c kj for some k S. We pick the k S that maximizes the number of jobs: { c = Note we need only check i < k < j To Solve S 0 if S = max k S c ik + c kj + 1 if S 1 Choose m S with the earliest finish time. The Greedy Choice Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 6 / 16 Graphs! Adjacency List, Adjacency Matrix Breadth First Search Depth First Search BFS Input: Graph G = (V, E), source node s V Output: d(v), distance (smallest # of edges) from s to v v V Output: π(v), predecessor of v on the shortest path from s to v 2 Then solve problem on jobs S mj Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 7 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 8 / 16

BFS BFS(V, E, s) 1 for each u in V \ {s} 2 do d(u) 3 π(u) nil 4 d[s] 0 5 Q 6 add(q, s) 7 while Q 8 do u poll(q) 9 for each v in Adj[u] 10 do if d[v] = 11 then d[v] d[u] + 1 12 π[v] = u 13 add(q, v) DFS DFS Input: Graph G = (V, E) Output: Two timestamps for each node d(v), f(v), Output: π(v), predecessor of v not on shortest path necessarily dfs(v, E) 1 for each u in V 2 do color(u) green 3 π(u) nil 4 time 0 5 for each u in V 6 do if color[u] = green 7 then dfs-visit(u) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 9 / 16 DFS (Visit Node Recursive) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 10 / 16 Classifying Edges in the DFS Tree dfs-visit(u) 1 color(u) yellow 2 d[u] time ++ 3 for each v in Adj[u] 4 do if color[v] = green 5 then π[v] u 6 dfs-visit(v) 7 8 color(u) red 9 f[u] = time ++ Given a DFS Tree G π, there are four type of edges (u, v) 1 Tree Edges: Edges in E π. These are found by exploring (u, v) in the DFS procedure 2 Back Edges: Connect u to an ancestor v in a DFS tree 3 Forward Edges: Connect u to a descendent v in a DFS tree 4 Cross Edges: All other edges. They can be edges in the same DFS tree, or can cross trees in the DFS forest G π Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 11 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 12 / 16

Modifying DFS to Classify Edges Stuff You Can Do with DFS DFS can be modified to classify edges as it encounters them... Classify e = (u, v) based on the color of v when e is first explored... green: Indicates Tree Edge yellow: Indicates Back Edge red: Indicates Forward or Cross Edge Topological Sort: The Whole Algorithm 1 DFS search the graph 2 List vertices in order of decreasing finishing time Strongly Connected Components 1 Call DFS(G) to topologically sort G 2 Compute G T 3 Call DFS(G T ) but consider vertices in topologically sorteded order (from G) 4 Vertices in each tree of depth-first forest for SCC Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 13 / 16 Spanning Tree Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 14 / 16 Kruskal s Algorithm Kruskal s Algorithm 1 Start with each vertex being its own component 2 Merge two components into one by choosing the light edge that connects them 3 Scans the set of edges in increasing order of weight Prim s Algorithm Builds one tree, so A is always a tree Let V A be the set of vertices on which A is incident Start from an arbitrary root r At each step find a light edge crossing the cut (V A, V \ V A ) kruskal(v, E, w) 1 A 2 for each v in V 3 do make-set(v) 4 sort(e, w) 5 for each (u, v) in (sorted) E 6 do if Find-Set(u) Find-Set(v) 7 then A A {(u, v)} 8 Union(u, v)return A Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 15 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 16 / 16

Pseudocode for Prim Shortest Paths Prim(V, E, w, r) 1 Q 2 for each u V 3 do key[u] 4 π[u] nilinsert(q, u) 5 key[r] = 0 6 while Q 7 do u Extract-Min(Q) 8 for each v Adj[u] 9 do if v Q and w uv < key[v] 10 then π[v] u 11 key[v] = w uv (Single Source) shortest-path algorithms produce a label: d[v] = δ(s, v). Initially d[v] =, reduces as the algorithm goes, so always d[v] δ(s, v) Also produce labels π[v], predecessor of v on a shortest path from s. Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 17 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 18 / 16 Lemma, Lemma, Lemma Relax! The algorithms work by improving (lowering) the shortest path estimate d[v] This operation is called relaxing an edge (u, v) Can we improve the shortest-path estimate for v by going through u and taking (u, v)? Path Relaxation Property Let P = {v 0, v 1,... v k } be a shortest path from s = v 0 to v k. If the edges (v 0, v 1 ), (v 1, v 2 ), (v k 1, v k ) are relaxed in that order, (there can be other relaxations in-between), then d[v k ] = δ(s, v k ) Relax(u, v, w) 1 if d[v] > d[u] + w uv 2 then d[v] d[u] + w uv 3 π[v] u Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 19 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 20 / 16

Bellman-Ford Algorithm Works with Negative-Weight Edges Returns true is there are no negative-weight cycles reachable from s, false otherwise Bellman-Ford(V, E, w, s) 1 Init-Single-Source(V, s) 2 for i 1 to V 1 3 do for each (u, v) in E 4 do Relax(u, v, w) 5 for each (u, v) in E 6 do if d[v] > d[u] + w uv 7 then return F alse 8 9 return T rue SSSP Dag DAG-Shortest-Paths(V, E, s, w) 1 Init-Single-Source(V, s) 2 topologically sort the vertices 3 for each u in topologically sortedv 4 do for each v Adj[u] 5 do RELAX(u, v, w) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 21 / 16 Dkstra Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 22 / 16 All Pairs Shortest Paths Dkstra(V, E, w, s) 1 Init-Single-Source(V, s) 2 S 3 Q V 4 while Q 5 do u Extract-Min(Q) 6 S S {u} 7 for each v Adj[u] 8 do Relax(u, v, w) Dkstra s Algorithm Runs in O(E lg V ), with a binary heap implementation. The output of an all pairs shortest path algorithm is a matrix D = (d), where d = δ(i, j) DP: l (m) be the shortest path from i V to j V that uses m edges l (m) = min 1 k n (l(m 1) ik + w kj ) Extend(L, W ) 1 create (n n) matrix L 2 for i 1 to n 3 do for j 1 to n 4 do l 5 for k 1 to n 6 do l min(l, l ik + w kj ) This is just like matrix multiplication. We can speed this up. Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 23 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 24 / 16

Faster All-Pairs-Shortest-Paths APSP2(W ) 1 L (1) = W 2 m 1 3 while m n 1 4 do L (2m) = Extend(L m, L m ) 5 m 2m 6 return L (m) OK to overshoot n 1, since shortest path labels don t change after m = n 1 (since no negative cycles) Repeated squaring is a technique used to improve the efficiency of lots of other algorithms : Floyd Warshall Floyd-Warshall Labels: Let d (k) be the shortest path from i to j such that all intermediate vertices are in the set {1, 2,..., k}. This simple obervation, immediately suggests a DP recursion { d (k) w k = 0 = min(d k 1, d k 1 ik + d k 1 kj ) k 1 We look for D (n) = (d) (n) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 25 / 16 Floyd-Warshall Floyd-Warshall(W ) 1 D (0) = W 2 for k 1 to n 3 do for i 1 to n 4 do for j 1 to n 5 do d (k) 6 return D (n) min(d k 1, d (k 1) ik + d (k 1) kj ) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 26 / 16 A net flow is a function f : V V R V V that satisfies three conditions: 1 Capacity Constraints: 2 Skew Symmetry: 0 f(u, v) c(u, v) f(u, v) = f(v, u) u V, v V 3 Flow Conservation: f(u, v) = 0 u V \ {s, t} v V The Maximum Flow Problem Given G = (V, E). source node s V, sink node t V, edge capacities c. Find a flow whose value is maximum. Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 27 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 28 / 16

Phlow Phacts For any cut (S, T ), f(s, T ) = f Resdiual capacity of arcs given flow: c f (u, v) def = c(u, v) f(u, v) 0. Give flow f, we can create a residual network from the flow. G f = (V, E f ), with E f def = {(u, v) V V c f (u, v) > 0}, so that each edge in the residual network can admit a positive flow. Max-Flow Min-Cut Theorem The following statements are equivalent 1 f is a maximum flow 2 f admits no augmenting path. (No (s, t) path in residual network) 3 f = c(s, T ) for some cut (S, T ) Ford-Fulkerson(V, E, c, s, t) 1 for i 1 to n 2 do f[u, v] f[v, u] 0 3 while augmenting path P in G f 4 do augment f by c f (P ) of this? Do better algorithms exist? Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 29 / 16 Maximum Bipartite Matching Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 30 / 16 Stuff To Know: EVERYTHING! DP and Greedy A graph G = (V, E) is bipartite if we can partition the vertices into V = L R such that all edges in E go between L and R A matching is a subset of edges M E such that for all v V, 1 edge of M is incident upon it. Maximum Bipartite Matching Given (undirected) bipartite graph G = (L R, E), find a matching M of G that contains the most edges Develop (and potentially solve small) problems via DP Activity Selection (or related problems): Greedy Works Graphs BFS, DFS, and. Classifying edges in directed and undirected graphs Topological Sorting Finding Strongly Connected Components Spanning Trees Kruskal s Algorithm (and analysis) Prim s Algorithm (and analysis) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 31 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 32 / 16

More Stuff To Know... Single Source Shortest Paths Distance Labels and Relax Path Relaxation Property Bellman-Ford Algorithm How to do it When (Why?) it works SSSP Dag How to do it When (Why?) it works Dkstra s Algorithm How to do it When (Why?) it works Even More Stuff To Know... All Pairs Shortest Paths Analogue to Matrix Multiplication Floyd-Warshall How to do it? When (Why?) it works? What is a flow? What is a cut? What is MFMC Theorem? How to create residual graph G f? How to do Augmenting Paths algorithm (Ford Fulkerson/Edmonds Karp) Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 33 / 16 Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 34 / 16 Next Time Quiz! April 4 No Class: Friday April 6. Have a nice holiday! We start numerical methods on Monday Jeff Linderoth (Lehigh University) IE170:Lecture 26 Lecture Notes 35 / 16