CS223 Advanced Data Structures and Algorithms 1 Greedy Algorithms Neil Tang 4/8/2010
CS223 Advanced Data Structures and Algorithms 2 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.
CS223 Advanced Data Structures and Algorithms 3 Basic 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.
CS223 Advanced Data Structures and Algorithms 4 Examples: “Greed is good” Dijkstra’s algorithm Prim’s algorithm Kruskal’s algorithm
CS223 Advanced Data Structures and Algorithms 5 Bin Packing Bin packing: Given N items with sizes s 1, s 2,…, s N, where 0 s i 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.
CS223 Advanced Data Structures and Algorithms 6 An Example Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8
CS223 Advanced Data Structures and Algorithms 7 The 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).
CS223 Advanced Data Structures and Algorithms 8 The Next Fit Algorithm Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8
CS223 Advanced Data Structures and Algorithms 9 The 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.
CS223 Advanced Data Structures and Algorithms 10 The 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(N 2 )
CS223 Advanced Data Structures and Algorithms 11 The First Fit Algorithm Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8
CS223 Advanced Data Structures and Algorithms 12 The 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.
CS223 Advanced Data Structures and Algorithms 13 The 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(N 2 )
CS223 Advanced Data Structures and Algorithms 14 The Best Fit Algorithm Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8
CS223 Advanced Data Structures and Algorithms 15 The 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(N 2 )
CS223 Advanced Data Structures and Algorithms 16 The Offline Algorithms First fit for 0.8, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1
CS223 Advanced Data Structures and Algorithms 17 The 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.
CS440 Computer Networks 18 The Path Scheduling Problem Given 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
CS440 Computer Networks 19 The First Fit Algorithm
CS440 Computer Networks 20 The Optimal Algorithm