Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Similar documents
Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 580: Algorithm Design and Analysis

CS781 Lecture 3 January 27, 2011

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design and Analysis

CSE 417. Chapter 4: Greedy Algorithms. Many Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Algorithm Design and Analysis

Algorithms: Lecture 12. Chalmers University of Technology

Algorithm Design and Analysis

CS 580: Algorithm Design and Analysis

CSE 421 Greedy Algorithms / Interval Scheduling

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Single Source Shortest Paths

6. DYNAMIC PROGRAMMING I

CSE 202 Dynamic Programming II

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 9

Knapsack and Scheduling Problems. The Greedy Method

Greedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:

Shortest Path Algorithms

Matching Residents to Hospitals

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Single Source Shortest Paths

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

12. LOCAL SEARCH. gradient descent Metropolis algorithm Hopfield neural networks maximum cut Nash equilibria

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Copyright 2000, Kevin Wayne 1

CS 580: Algorithm Design and Analysis. Jeremiah Blocki Purdue University Spring 2018

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

Introduction to Algorithms

CS 374: Algorithms & Models of Computation, Spring 2017 Greedy Algorithms Lecture 19 April 4, 2017 Chandra Chekuri (UIUC) CS374 1 Spring / 1

Introduction to Algorithms

Introduction to Algorithms

Introduction to Algorithms

CS 580: Algorithm Design and Analysis

Algorithm Design and Analysis

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

6. DYNAMIC PROGRAMMING II

6. DYNAMIC PROGRAMMING I

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

1. REPRESENTATIVE PROBLEMS

Dynamic Programming: Interval Scheduling and Knapsack

1. REPRESENTATIVE PROBLEMS

Linear Time Selection

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Chapter 11. Approximation Algorithms. Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.

Chapter 6. Dynamic Programming. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

1. REPRESENTATIVE PROBLEMS

2. ALGORITHM ANALYSIS

Algorithm Design Strategies V

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

CSE 421 Dynamic Programming

Copyright 2000, Kevin Wayne 1

2.1 Computational Tractability. Chapter 2. Basics of Algorithm Analysis. Computational Tractability. Polynomial-Time

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

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

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Dynamic Programming. Weighted Interval Scheduling. Algorithmic Paradigms. Dynamic Programming

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Scheduling with AND/OR Precedence Constraints

Design and Analysis of Algorithms

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Copyright 2000, Kevin Wayne 1

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

Chapter 8. NP and Computational Intractability

5. DIVIDE AND CONQUER I

! Break up problem into several parts. ! Solve each part recursively. ! Combine solutions to sub-problems into overall solution.

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

The Greedy Method. Design and analysis of algorithms Cs The Greedy Method

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Chapter 6. Weighted Interval Scheduling. Dynamic Programming. Algorithmic Paradigms. Dynamic Programming Applications

CS583 Lecture 11. Many slides here are based on D. Luebke slides. Review: Dynamic Programming

4/30/14. Chapter Sequencing Problems. NP and Computational Intractability. Hamiltonian Cycle

5. DIVIDE AND CONQUER I

Greedy Algorithms My T. UF

8.5 Sequencing Problems. Chapter 8. NP and Computational Intractability. Hamiltonian Cycle. Hamiltonian Cycle

12. LOCAL SEARCH. gradient descent Metropolis algorithm Hopfield neural networks maximum cut Nash equilibria

Chapter 7. Network Flow. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CS 580: Algorithm Design and Analysis

CS 161: Design and Analysis of Algorithms

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Analysis of Algorithms - Midterm (Solutions)

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Minimizing the Number of Tardy Jobs

Dynamic Programming. Cormen et. al. IV 15

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

6. DYNAMIC PROGRAMMING I

Partha Sarathi Mandal

Transcription:

Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright Pearson-Addison Wesley. All rights reserved. 4

4.1 Interval Scheduling

Interval Scheduling Interval scheduling. Job j starts at s j and finishes at f j. Two jobs compatible if they don't overlap. Goal: find maximum subset of mutually compatible jobs. a b c d e f g 1 3 4 7 8 1 11 h Time

Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. [Earliest start time] Consider jobs in ascending order of start time s j. [Earliest finish time] Consider jobs in ascending order of finish time f j. [Shortest interval] Consider jobs in ascending order of interval length f j - s j. [Fewest conflicts] For each job, count the number of conflicting jobs c j. Schedule in ascending order of conflicts c j. 7

Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some order. Take each job provided it's compatible with the ones already taken. breaks earliest start time breaks shortest interval breaks fewest conflicts 8

Interval Scheduling: Greedy Algorithm Greedy algorithm. Consider jobs in increasing order of finish time. Take each job provided it's compatible with the ones already taken. Sort jobs by finish times so that f 1 f... f n. jobs selected A for j = 1 to n { if (job j compatible with A) A A {j} } return A Implementation. O(n log n). Remember job j* that was added last to A. Job j is compatible with A if s j f j*.

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time 1 3 4 7 8 1 11

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B 1 3 4 7 8 1 11 1

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B C 1 3 4 7 8 1 11

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B A 1 3 4 7 8 1 11 3

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B E 1 3 4 7 8 1 11 4

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B ED 1 3 4 7 8 1 11

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B E F 1 3 4 7 8 1 11

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B E G 1 3 4 7 8 1 11 7

Interval Scheduling B C A E D F 1 3 4 7 8 1 11 G H Time B E H 1 3 4 7 8 1 11 8

4.1 Interval Partitioning

Interval Partitioning Interval partitioning. Lecture j starts at s j and finishes at f j. Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses 4 classrooms to schedule 1 lectures. e j c d g b h a f i :3 1 1:3 11 11:3 1 1:3 1 1:3 :3 3 3:3 4 4:3 Time 7

Interval Partitioning Interval partitioning. Lecture j starts at s j and finishes at f j. Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. Ex: This schedule uses only 3. c d f j b g i a e h :3 1 1:3 11 11:3 1 1:3 1 1:3 :3 3 3:3 4 4:3 Time 71

Interval Partitioning: Lower Bound on Optimal Solution Def. The depth of a set of open intervals is the maximum number that contain any given time. Key observation. Number of classrooms needed depth. Ex: Depth of schedule below = 3 schedule below is optimal. a, b, c all contain :3 Q. Does there always exist a schedule equal to depth of intervals? c d f j b g i a e h :3 1 1:3 11 11:3 1 1:3 1 1:3 :3 3 3:3 4 4:3 Time 7

Interval Partitioning: Greedy Algorithm Greedy algorithm. Consider lectures in increasing order of start time: assign lecture to any compatible classroom. Sort intervals by starting time so that s 1 s... s n. d number of allocated classrooms for j = 1 to n { if (lecture j is compatible with some classroom k) schedule lecture j in classroom k else allocate a new classroom d + 1 schedule lecture j in classroom d + 1 d d + 1 } Implementation. O(n log n). For each classroom k, maintain the finish time of the last job added. Keep the classrooms in a priority queue. 73

Interval Partitioning: Greedy Analysis Observation. Greedy algorithm never schedules two incompatible lectures in the same classroom. Theorem. Greedy algorithm is optimal. Pf. Let d = number of classrooms that the greedy algorithm allocates. Classroom d was allocated because we needed to schedule a job, say j, that is incompatible with all d-1 other classrooms. Since we sorted by start time, all these incompatibilities are caused by lectures that start no later than s j. Thus, we have d lectures overlapping at time s j + ε. Key observation all schedules use d classrooms. 74

Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright Pearson-Addison Wesley. All rights reserved. 7

4.4 Shortest Paths in a Graph shortest path from Princeton CS department to Einstein's house

Shortest Path Problem Shortest path network. Directed graph G = (V, E). Source s, target t. Length l e = length of edge e. Shortest path problem: find shortest directed path from s to t. cost of path = sum of edge costs in path 3 3 s 1 3 18 11 1 4 1 Cost of path s--3--t = + 3 + + 1 =. 7 44 t 77

Dijkstra's Algorithm Dijkstra's algorithm. Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) =. Repeatedly (greedily) choose unexplored node v S which minimizes add v to S, and set d(v) = (v). shortest path to some u in explored part, followed by a single edge (u, v) S d(u) u l e v s 78

Dijkstra's Shortest Path Algorithm Find shortest path from s to t. 4 3 s 18 1 3 11 1 4 1 7 44 t 7

Dijkstra's Shortest Path Algorithm S = { } PQ = { s,, 3, 4,,, 7, t } s 1 3 18 4 11 1 4 3 1 distance label 7 44 8 t

Dijkstra's Shortest Path Algorithm S = { } PQ = { s,, 3, 4,,, 7, t } delmin s 1 3 18 4 11 1 4 3 1 distance label 7 44 81 t

Dijkstra's Shortest Path Algorithm decrease key S = { s } PQ = {, 3, 4,,, 7, t } X s 1 X 3 18 4 11 1 4 3 1 distance label 7 X 1 44 8 t

Dijkstra's Shortest Path Algorithm S = { s } PQ = {, 3, 4,,, 7, t } X delmin s 1 X 3 18 4 11 1 4 3 1 distance label 7 X 1 44 83 t

Dijkstra's Shortest Path Algorithm S = { s, } PQ = { 3, 4,,, 7, t } X s 1 X 3 18 4 11 1 4 3 1 7 X 1 44 84 t

Dijkstra's Shortest Path Algorithm S = { s, } PQ = { 3, 4,,, 7, t } decrease key X X 33 s 1 X 3 18 4 11 1 4 3 1 7 X 1 44 8 t

Dijkstra's Shortest Path Algorithm S = { s, } PQ = { 3, 4,,, 7, t } X X 33 s 1 X delmin 3 18 4 11 1 4 3 1 7 X 1 44 8 t

Dijkstra's Shortest Path Algorithm S = { s,, } PQ = { 3, 4,, 7, t } 3 X X 33 X s 1 X 3 44 X 18 4 11 1 4 3 1 7 X 1 44 87 t

Dijkstra's Shortest Path Algorithm S = { s,, } PQ = { 3, 4,, 7, t } 3 X X 33X s 1 X 3 44 X 18 4 11 1 4 3 1 7 X 1 delmin 44 88 t

Dijkstra's Shortest Path Algorithm S = { s,,, 7 } PQ = { 3, 4,, t } 3 X X 33X s 1 X 3 44 X X 3 18 4 11 1 4 3 1 7 X 1 44 t X 8

Dijkstra's Shortest Path Algorithm S = { s,,, 7 } PQ = { 3, 4,, t } delmin 3 X X 33X s 1 X 3 44 X X 3 18 4 11 1 4 3 1 7 X 1 44 t X

Dijkstra's Shortest Path Algorithm S = { s,, 3,, 7 } PQ = { 4,, t } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 3 1 7 X 1 44 1 t X X 1

Dijkstra's Shortest Path Algorithm S = { s,, 3,, 7 } PQ = { 4,, t } 3 X X 33X s 1 X 3 4 18 44 X 3 X 34 X delmin 11 1 4 3 1 7 X 1 44 1 t X X

Dijkstra's Shortest Path Algorithm S = { s,, 3,,, 7 } PQ = { 4, t } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 X 4 3 1 7 X 1 44 1 X t X X 3

Dijkstra's Shortest Path Algorithm S = { s,, 3,,, 7 } PQ = { 4, t } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 X 4 delmin 3 1 7 X 1 44 1 X t X X 4

Dijkstra's Shortest Path Algorithm S = { s,, 3, 4,,, 7 } PQ = { t } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 X 4 3 1 7 X 1 44 1 X t X X

Dijkstra's Shortest Path Algorithm S = { s,, 3, 4,,, 7 } PQ = { t } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 X 4 3 1 7 X 1 44 delmin 1 X t X X

Dijkstra's Shortest Path Algorithm S = { s,, 3, 4,,, 7, t } PQ = { } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 X 4 3 1 7 X 1 44 1 X t X X 7

Dijkstra's Shortest Path Algorithm S = { s,, 3, 4,,, 7, t } PQ = { } 3 X X 33X s 1 X 3 44 X X 4 18 3 X 34 11 1 4 X 4 3 1 7 X 1 44 1 X t X X 8

Dijkstra's Algorithm: Proof of Correctness Invariant. For each node u S, d(u) is the length of the shortest s-u path. Pf. (by induction on S ) Base case: S = 1 is trivial. Inductive hypothesis: Assume true for S = k 1. Let v be next node added to S, and let (u,v) be the chosen edge. The shortest s-u path plus (u, v) is an s-v path of length (v). Consider any s-v path P. We'll see that it's no shorter than (v). P Let x-y be the first edge in P that leaves S, and let P' be the subpath to x. P' x P is already too long as soon as it s leaves S. S u l (P) l (P') + l (x,y) d(x) + l (x, y) (y) (v) v y nonnegative weights inductive hypothesis defn of π(y) Dijkstra chose v instead of y

Dijkstra's Algorithm: Implementation For each unexplored node v S, explicitly maintain Next node to explore = node v S with minimum (v). When exploring v, for each incident edge e = (v, w), w S, update Efficient implementation. Maintain a priority queue of unexplored nodes, prioritized by (v). PQ Operation Insert ExtractMin ChangeKey n n 1 log n log n log n Priority Queue IsEmpty n 1 1 1 1 Total (w) = min { (w), (v)+ l e }. Dijkstra n n m Array Binary heap d-way Heap Fib heap n m log n d log d n d log d n log d n m log m/n n 1 log n 1 m + n log n Individual ops are amortized bounds 1

Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright Pearson-Addison Wesley. All rights reserved. 11