Greedy. Greedy. Greedy. Greedy. What is a Greedy Algorithm? Solves an optimization problem: the solution is best in some sense.

Similar documents
Near Optimal Solutions

Approximation Algorithms

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Solutions to Homework 6

Scheduling Shop Scheduling. Tim Nieberg

Full and Complete Binary Trees

CS473 - Algorithms I

Find-The-Number. 1 Find-The-Number With Comps

NOTES ON LINEAR TRANSFORMATIONS

Warshall s Algorithm: Transitive Closure

Closest Pair Problem

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

Single machine parallel batch scheduling with unbounded capacity

Matrix-Chain Multiplication

INDISTINGUISHABILITY OF ABSOLUTELY CONTINUOUS AND SINGULAR DISTRIBUTIONS

How To Find An Optimal Search Protocol For An Oblivious Cell

1 if 1 x 0 1 if 0 x 1

3. Mathematical Induction

Bicolored Shortest Paths in Graphs with Applications to Network Overlay Design

6.3 Conditional Probability and Independence

24. The Branch and Bound Method

1. Prove that the empty set is a subset of every set.

Triangle deletion. Ernie Croot. February 3, 2010

Cpt S 223. School of EECS, WSU

GRAPH THEORY LECTURE 4: TREES

JUST-IN-TIME SCHEDULING WITH PERIODIC TIME SLOTS. Received December May 12, 2003; revised February 5, 2004

Follow links for Class Use and other Permissions. For more information send to:

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Definition Given a graph G on n vertices, we define the following quantities:

Lecture 1: Course overview, circuits, and formulas

Classification - Examples

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

the recursion-tree method

ONLINE DEGREE-BOUNDED STEINER NETWORK DESIGN. Sina Dehghani Saeed Seddighin Ali Shafahi Fall 2015

5 INTEGER LINEAR PROGRAMMING (ILP) E. Amaldi Fondamenti di R.O. Politecnico di Milano 1

5.1 Bipartite Matching

Systems of Linear Equations

Follow the Perturbed Leader

Dynamic Programming. Lecture Overview Introduction

Page 1. CSCE 310J Data Structures & Algorithms. CSCE 310J Data Structures & Algorithms. P, NP, and NP-Complete. Polynomial-Time Algorithms

Lecture 17 : Equivalence and Order Relations DRAFT

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

I. GROUPS: BASIC DEFINITIONS AND EXAMPLES

All trees contain a large induced subgraph having all degrees 1 (mod k)

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

OPTIMAL DESIGN OF DISTRIBUTED SENSOR NETWORKS FOR FIELD RECONSTRUCTION

Even Faster Algorithm for Set Splitting!

Lecture 3: Finding integer solutions to systems of linear equations

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Connected Identifying Codes for Sensor Network Monitoring

Kenken For Teachers. Tom Davis June 27, Abstract

No: Bilkent University. Monotonic Extension. Farhad Husseinov. Discussion Papers. Department of Economics

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Minimizing Probing Cost and Achieving Identifiability in Probe Based Network Link Monitoring

6.852: Distributed Algorithms Fall, Class 2

CHAPTER II THE LIMIT OF A SEQUENCE OF NUMBERS DEFINITION OF THE NUMBER e.

Linear Programming. March 14, 2014

CSC2420 Spring 2015: Lecture 3

Data Structures and Algorithms Written Examination

Large induced subgraphs with all degrees odd

E3: PROBABILITY AND STATISTICS lecture notes

Sensitivity Analysis 3.1 AN EXAMPLE FOR ANALYSIS

Analysis of Algorithms I: Optimal Binary Search Trees

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

International Journal of Advanced Research in Computer Science and Software Engineering

Scheduling a sequence of tasks with general completion costs

CMPSCI611: Approximating MAX-CUT Lecture 20

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

Mathematical Induction. Lecture 10-11

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Lecture 22: November 10

Practical Guide to the Simplex Method of Linear Programming

Row Echelon Form and Reduced Row Echelon Form

Distributed Computing over Communication Networks: Maximal Independent Set

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

CSCE 310J Data Structures & Algorithms. Dynamic programming 0-1 Knapsack problem. Dynamic programming. Dynamic Programming. Knapsack problem (Review)

8 Divisibility and prime numbers

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

1.2 Solving a System of Linear Equations

Chapter 3. Social Surplus and Tractability

Tree-representation of set families and applications to combinatorial decompositions

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

On the k-path cover problem for cacti

SCORE SETS IN ORIENTED GRAPHS

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Arithmetic Coding: Introduction

Dynamic Programming Problem Set Partial Solution CMPSC 465

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Distributed Load Balancing for Machines Fully Heterogeneous

Towards Optimal Firewall Rule Ordering Utilizing Directed Acyclical Graphs

Matrix Representations of Linear Transformations and Changes of Coordinates

The Minimum Consistent Subset Cover Problem and its Applications in Data Mining

A Non-Linear Schema Theorem for Genetic Algorithms

3. Linear Programming and Polyhedral Combinatorics

MINIMAL BOOKS OF RATIONALES

Analysis of Algorithms, I

Scheduling Single Machine Scheduling. Tim Nieberg

THE DIMENSION OF A VECTOR SPACE

TREE BASIC TERMINOLOGIES

Transcription:

A Comparison Divide & Conquer Dynamic Programming Algorithm View problem as collection of Recursive nature Independent overlapping typically sequential dependence Number of depends on partitioning factors typically small Preprocessing typically sort Characteristic running time Primarily for optimization problems Optimal substructure: optimal solution to problem contains within it optimal solutions to choice property: locally optimal produces globally optimal Heuristic version useful for bounding optimal value typically log function of n depends on number and difficulty of often dominated by nlogn sort 9/7/10 1 What is a Algorithm? Solves an optimization problem: the solution is best in some sense. Strategy: At each decision point, do what looks best locally Choice does not depend on evaluating potential future choices or solving Top-down algorithmic structure With each step, reduce problem to a smaller problem Optimal Substructure: optimal solution contains in it optimal solutions to Choice Property: locally best = globally best 9/7/10 What is a Algorithm? Solves an optimization problem: the solution is best in some sense. Strategy: At each decision point, do what looks best locally Choice does not depend on evaluating potential future choices or solving Top-down algorithmic structure With each step, reduce problem to a smaller problem Optimal Substructure: optimal solution contains in it optimal solutions to Choice Property: locally best = globally best 9/7/10 3 See also: http://en.wikipedia.org/wiki/_algorithm With a greedy algorithm for the N-queens problem. 9/7/10 1

Examples: Minimum Spanning Tree Minimum Spanning Forest Dijkstra Shortest Path Huffman Codes Fractional Knapsack Activity Selection Minimum Spanning Tree Invariant: Minimum weight spanning forest Becomes single tree at end Invariant: Minimum weight tree Spans all vertices at end A B 3 G 5 1 E 1 7 8 F D C for Undirected, Connected, Weighted Graph G=(V,E) 9/7/10 5 9/7/10 Single Source Shortest Paths: Dijkstra s Algorithm Huffman Codes A B 3 G 5 1 E 1 7 8 F D C 9/7/10 7 9/7/10 8

Fractional Knapsack : Example Activity Selection: Problem Instance Set S = {1,,..., n} of n activities Each activity i has: start time: s i finish time: f i si f i Activities i, j are compatible iff nonoverlapping: [s i, f i ) \Å [s j, f j ) =. Objective: select a maximum-sized set of mutually compatible activities 9/7/10 9 9/7/10 10 Algorithm Why does this all work? Algorithm: S = presort activities in S by nondecreasing finish time and renumber GREEDY-ACTIVITY-SELECTOR(S, f) n = S.length A = {1} j = 1 for i = to n return A» if s i f i» A = A [ {i}» j = i Why does it provide an optimal (maximal) solution? It is clear that it will provide a solution (a set of nonoverlapping activities), but there is no obvious reason to believe that it will provide a maximal set of activities. We start with a restatement of the problem in such a way that a dynamic programming solution can be constructed. This solution will be shown to be maximal. It will then be modified into a greedy solution in such a way that maximality will be preserved. 9/7/10 11 9/7/10 1 3

Consider the set S of activities, sorted in monotonically increasing order of finishing time: i s i f i 1 3 5 7 8 9 10 11 1 3 0 5 3 5 8 8 1 5 7 8 9 10 11 1 13 1 Where the activity a i corresponds to the time interval [s i, f i ). The subset {a 3, a 9, a 11 } consists of mutually compatible activities, but is not maximal. The subsets {a 1, a, a 8, a 11 }, and {a, a, a 9, a 11 } are also compatible and are both maximal. 9/7/10 13 First Step: find an optimal substructure: an optimal solution to a subproblem that can be extended to a full solution. Define an appropriate space of : S ij = {a k S: f i s k < f k s j }, the set of all those activities compatible with a i and a j and with those that finish no later than a i and start no earlier than a j. Add two activities: a 0 :[, 0); a n+1 :[, ); which come before and after, respectively, all activities in S = S 0,n+1. Assume the activities are sorted in non-decreasing order of finish: f 0 f 1 f n < f n+1. Then S ij is empty whenever i j. 9/7/10 1 We define the suproblems: find a maximum size subset of mutually compatible activities from S ij, 0 i < j n+1. What is the substructure? Substructure: suppose a solution to S ij contains some activity a k, f i s k < f k s j. a k generates two, S ik and S kj. The solution to S ij is the union of a solution to S ik, the singleton activity {a k }, and a solution to S kj. Any solution to a larger problem can be obtained by patching together solutions for smaller problems. Cardinality (=number of activities) is also additive. Optimal Substructure: assume A ij is an optimal solution to S ij, containing an activity a k. Then A ij contains a solution to S ik (the activities that end before the beginning of a k ) and a solution to S kj (the activities that begin after the end of a k ). If these solutions were not already maximal, then one could obtain a maximal solution for, say, S ik, and splice it with {a k } and the solution to S kj to obtain a solution for S ij of greater cardinality, thus violating the optimality condition assumed for A ij. 9/7/10 15 9/7/10 1

Use Optimal Substructure to construct an optimal solution: Any solution to a nonempty problem S ij includes some activity a k, and any optimal solution to S ij must contain optimal solutions to S ik and S kj. For each a k in S 0,n+1, find (recursively) optimal solutions of S 0k and S k,n+1, say A 0k and A k,n+1. Splice them together, along with a k, to form solutions A k. Take a maximal A k as an optimal solution A 0,n+1. We need to look at the recursion in more detail (cost??) Second Step: the recursive algorithm. Let c[i,j] denote the maximum number of compatible activities in S ij. It is easy to see that c[i,j] = 0, whenever i j - and S ij is empty. If, in a non-empty set of activities S ij, the activity a k occurs as part of a maximal compatible subset, this generates two, S ik and S kj, and the equation c[i,j] = c[i,k] + 1 + c[k,j], which tells us how the cardinalities are related. The problem is that k is not known a priori. Solution: c[i,j] = max i<k<j (c[i,k] + 1 + c[k,j]), if S ij. And one can write an easy bottom up (dynamic programming) algorithm to compute a maximal solution. 9/7/10 17 9/7/10 18 A better mousetrap. Recall S ij = {a k S: f i s k < f k s j }. Theorem 1.1. Consider any nonempty subproblem S ij, and let a m be the activity in S ij with the earliest finish time: f m = min{f k : a k S ij }. Then: 1. a m is used in some maximum size subset of mutually compatible activities of S ij (e.g., A ij = A im U {a m } U A mj ).. The subproblem S im is empty, so that choosing a m leaves the subproblem S mj as the only one that may be nonempty. Proof: ) if S im is nonempty then S ij must contain an activity (finishing) prior to a m. Contradiction. 1) Suppose A ij is a maximal subset. Either a m is in A ij, and we are done, or it is not. If not, let a k be the earliest finishing activity in A ij. It can be replaced by a m (since it finishes no earlier than a m ) thus giving a maximal subset containing a m. 9/7/10 19 Why is this a better mousetrap? The dynamic programming solution requires solving j - i 1 to solve S ij. Total Running Time??? Theorem 1.1 gives us conditions under which solving S ij requires solving ONE subproblem only, since the other implied by the dynamic programming recurrence relation are empty, or irrelevant (they might give us other maximal solutions, but we need just one) Lots less computation Another benefit comes from the observation that the problem can be solved in a top-down fashion: take the earliest finishing activity, a m, and you are left with the problem S m,n+1. It is easy to see that each activity needs to be looked at only once: linear time (after sorting). 9/7/10 0 5

The Algorithm: R_A_S(s, f, k, n) 1. m = k + 1. while m n and s[m] < f[k] // find first activity in S k to finish 3. do m = m + 1. if m n 5. then return {a m } + R_A_S(s, f, m, n). else return. The Recursive Activity Selector Algorithm: Cormen, Leiserson, Rivest, Stein: Fig. 1.1 The time is - fairly obviously Θ(n). 9/7/10 1 9/7/10 A Slightly Different Perspective. Rather than start from a dynamic programming approach, moving to a greedy strategy, start with identifying the characteristics of the greedy approach. Choice Property. Choice that looks best in the current problem will work: no need to look ahead to its implications for the solution. 1. Make a choice and leave a subproblem to solve.. Prove that the greedy choice is always safe - there is an optimal solution starting from a greedy choice. 3. Prove that, having made a greedy choice, the solution of the remaining subproblem can be added to the greedy choice providing a solution to the original problem. This is important because it reduces the amount of computational complexity we have to deal with. Can t expect this to hold all the time: maximization of a function with multiple relative maxima on an interval - the gradient method would lead to a greedy choice, but may well lead to a relative maximum that is far from the actual maximum. 9/7/10 3 9/7/10

Optimal Substructure. While the greedy choice property tells us that we should be able to solve the problem with little computation, we need to know that the solution can be properly reconstructed: an optimal solution contains optimal sub-solutions to the. Note: An induction can then be used to turn the construction around (bottom-up). In the case of a problem possessing only the Optimal Substructure property there is little chance that we will be able to find methods more efficient than dynamic programming (with memoization). Upper Bound Lower Bound 9/7/10 5 9/7/10 7