Greedy Algorithms Neil Tang 4/8/2010

20
CS223 Advanced Data Structures and Algorithms 1 Greedy Algorithms Greedy Algorithms Neil Tang Neil Tang 4/8/2010 4/8/2010

description

Greedy Algorithms Neil Tang 4/8/2010. Class Overview. Basic idea Examples: “Greed is good” The bin packing problem and the algorithms The path scheduling problem and the algorithms: Greed is no good. Basic Idea. - PowerPoint PPT Presentation

Transcript of Greedy Algorithms Neil Tang 4/8/2010

Page 1: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 1

Greedy AlgorithmsGreedy Algorithms

Neil TangNeil Tang4/8/20104/8/2010

Page 2: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 2

Class OverviewClass Overview

Basic idea

Examples: “Greed is good”

The bin packing problem and the algorithms

The path scheduling problem and the algorithms: Greed is no good.

Page 3: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 3

Basic IdeaBasic Idea

In each phase, it makes the best choice based on the current partial solution and the performance metric.

No future consequences are considered when making decisions.

Usually it will not go back to change the previous choices.

Page 4: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 4

Examples: “Greed is good”Examples: “Greed is good”

Dijkstra’s algorithm

Prim’s algorithm

Kruskal’s algorithm

Page 5: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 5

Bin PackingBin Packing

Bin packing: Given N items with sizes s1, s2,…, sN, where

0 si 1. The bin packing is to pack these items in the fewest bins, given that each bin has unit capacity.

Online bin packing (dynamic case): Each item must be placed in a bin before the size of the next item is given.

Offline bin packing (static case): You cannot make decision until all the input has been read.

Page 6: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 6

An ExampleAn Example

Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8

Page 7: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 7

The Next Fit AlgorithmThe Next Fit Algorithm

For each new item, check to see if it fits in the same bin as the last one. If it does, place it there. Otherwise, create a new bin.

Time complexity: O(N).

Page 8: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 8

The Next Fit AlgorithmThe Next Fit Algorithm

Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8

Page 9: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 9

The Next Fit AlgorithmThe Next Fit Algorithm Theorem: Let M be the optimal solution. The next fit

algorithm never uses more than 2M bins. There exist sequences such that it uses 2M-2 bins.

Page 10: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 10

The First Fit AlgorithmThe First Fit Algorithm

For each new item, scan the existing bins in order and place it in the first bin that can hold it . Create a new bin if none of them can hold it.

Time complexity: O(N2)

Page 11: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 11

The First Fit AlgorithmThe First Fit Algorithm

Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8

Page 12: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 12

The First Fit AlgorithmThe First Fit Algorithm Theorem: Let M be the optimal solution. The first fit

algorithm never uses more than 1.7M bins. There exist sequences such that it uses 1.7(M-1) bins.

Page 13: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 13

The Best Fit AlgorithmThe Best Fit Algorithm

For each new item, scan the existing bins in order and place it in the tightest spot among all bins. Create a new bin if none of them can hold it.

Time complexity: O(N2)

Page 14: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 14

The Best Fit AlgorithmThe Best Fit Algorithm

Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8

Page 15: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 15

The Offline AlgorithmsThe Offline Algorithms

The first/best fit decreasing algorithm: Sort the items in the descending order of their sizes, then use the first/best fit algorithm.

Time complexity: O(N2)

Page 16: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 16

The Offline AlgorithmsThe Offline Algorithms

First fit for 0.8, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1

Page 17: Greedy Algorithms Neil Tang 4/8/2010

CS223 Advanced Data Structures and Algorithms 17

The Offline AlgorithmsThe Offline Algorithms Theorem: Let M be the optimal solution. The first fit

descending algorithm never uses more than 11/9M+4 bins. There exists sequences such that it uses 11/9M bins.

Page 18: Greedy Algorithms Neil Tang 4/8/2010

CS440 Computer Networks 18

The Path Scheduling ProblemThe Path Scheduling ProblemGiven a path, and free timeslots of every nodes in the path, find a collision-free transmission schedule.

J. Tang, G. Xue and C. Chandler, Interference-aware routing and bandwidth allocation for QoS provisioning in multihop wireless networks, Wireless Communications and Mobile Computing (WCMC), Vol. 5, No. 8, 2005, pp. 933-944.

Page 19: Greedy Algorithms Neil Tang 4/8/2010

CS440 Computer Networks 19

The First Fit AlgorithmThe First Fit Algorithm

Page 20: Greedy Algorithms Neil Tang 4/8/2010

CS440 Computer Networks 20

The Optimal AlgorithmThe Optimal Algorithm