200 likes | 208 Views
Learn advanced data structures and algorithms like bin packing with greedy techniques. Understand Next Fit, First Fit, and Best Fit algorithms. Practice with online and offline scenarios for optimal solutions.
E N D
Greedy AlgorithmsNeil Tang4/8/2010 CS223 Advanced Data Structures and Algorithms
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
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
Examples: “Greed is good” • Dijkstra’s algorithm • Prim’s algorithm • Kruskal’s algorithm CS223 Advanced Data Structures and Algorithms
Bin Packing • Bin packing: Given N items with sizes s1, s2,…, sN, where0 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. CS223 Advanced Data Structures and Algorithms
An Example • Pack: 0.2,0.5,0.4,0.7,0.1,0.3,0.8 CS223 Advanced Data Structures and Algorithms
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
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
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
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(N2) CS223 Advanced Data Structures and Algorithms
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
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
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(N2) CS223 Advanced Data Structures and Algorithms
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
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(N2) CS223 Advanced Data Structures and Algorithms
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
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. CS223 Advanced Data Structures and Algorithms
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. 933-944. CS440 Computer Networks
The First Fit Algorithm CS440 Computer Networks
The Optimal Algorithm CS440 Computer Networks