浙江大学DS05_Ch10a_A高级数据结构课件.ppt_第1页
浙江大学DS05_Ch10a_A高级数据结构课件.ppt_第2页
浙江大学DS05_Ch10a_A高级数据结构课件.ppt_第3页
浙江大学DS05_Ch10a_A高级数据结构课件.ppt_第4页
浙江大学DS05_Ch10a_A高级数据结构课件.ppt_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、CHAPTER 10 ALGORITHM DESIGN TECHNIQUES,1 Greedy Algorithms, Optimization Problems: Given a set of constrains and an optimization function. Solutions that satisfy the constrains are called feasible solutions. A feasible solution for which the optimization function has the best possible value is calle

2、d an optimal solution., The Greedy Method: Make the best decision at each stage, under some greedy criterion. A decision made in one stage is not changed in a later stage, so each decision should assure feasibility.,1/12,1 Greedy Algorithms,2/12,Note: Greedy algorithm works only if the local optimum

3、 is equal to the global optimum. Greedy algorithm does not guarantee optimal solutions. However, it generally produces solutions that are very close in value (heuristics) to the optimal, and hence is intuitively appealing when finding the optimal solution takes too much time.,1 Greedy Algorithms,1.

4、Huffman Codes for file compression,Example Suppose our text is a string of length 1000 that comprises the characters a, u, x, and z. Then it will take ? bits to store the string as 1000 one-byte characters.,8000,Notice that we have only 4 distinct characters in that string. Hence we need only 2 bits

5、 to identify them.,We may encode the symbols as a = 00, u = 01, x = 10, z = 11. For example, aaaxuaxz is encoded as 0000001001001011. Then the space taken by the string with length 1000 will be 2000 bits + space for code table. /* log C bits are needed in a standard encoding where C is the size of t

6、he character set */, frequency := number of occurrences of a symbol.,In string aaaxuaxz , f(a) = 4, f(u) = 1, f(x) = 2, f(z) = 1.,The size of the coded string can be reduced using variable-length codes, for example, a = 0, u = 110, x = 10, z = 111.,3/12,Discussion 9: In what case that there are not

7、likely to be any savings even using Huffman codes?,1 Greedy Algorithms,Representation of the original code in a binary tree /* trie */, If character Ci is at depth di and occurs fi times, then the cost of the code = di fi .,Cost ( aaaxuaxz 0000001001001011 ) = 24 + 21 + 22 + 21 = 16,Representation o

8、f the optimal code in a binary tree,Cost ( aaaxuaxz 00010110010111 ) = 14 + 31 + 22 + 31 = 14,The answer is aaaxuaxz (with a = 0, u = 110, x = 10, z = 111). What makes this decoding method work?,The trick is: No code is a prefix of another.,Now, with a = 0, u = 110, x = 10, z = 111 and the string 00

9、010110010111, can you decode it?,4/12,Discussion 10: What must the tree look like if we are to decode unambiguously?,1 Greedy Algorithms, Huffmans Algorithm (1952),void Huffman ( PriorityQueue heap , int C ) consider the C characters as C single node binary trees, and initialize them into a min heap

10、; for ( i = 1; i C; i+ ) create a new node; /* be greedy here */ delete root from min heap and attach it to left_child of node; delete root from min heap and attach it to right_child of node; weight of node = sum of weights of its children; /* weight of a tree = sum of the frequencies of its leaves

11、*/ insert node into min heap; ,T = O( ? ),C log C,5/12,1 Greedy Algorithms,Cost = 310 + 215 + 212 + 53 + 44 + 213 + 51 = 146,6/12,Research Project 4 Huffman Codes (30),As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem: the Huffman codes are NOT unique.

12、 The students are submitting all kinds of codes, and I need a computer program to help me determine which ones are correct and which ones are not. Detailed requirements can be downloaded from ,7/12,2. A Simple Scheduling Problem,Given N jobs j1 , j2 , , jN , their running times t1 , t2 , , tN , and

13、one processor.,Schedule jobs to minimize the average completion time. /* assume nonpreemptive scheduling */, The Single Processor Case,1 Greedy Algorithms,8/12,1 Greedy Algorithms,Example,Schedule 1,Tavg = ( 15 + 23 + 26 + 36 ) / 4 = 25,Schedule 2,Tavg = ( 3 + 11 + 21 + 36 ) / 4 = 17.75,In general:,

14、9/12,Best scheduling is to make tik nondecreasing.,1 Greedy Algorithms, The Multiprocessor Case N jobs on P processors,An Optimal Solution, Minimizing the Final Completion Time,NP Hard,10/12,1 Greedy Algorithms, Flow Shop Scheduling a simple case with 2 processors,Consider a machine shop that has 2

15、identical processors P1 and P2 . We have N jobs J1, . , JN that need to be processed. Each job Ji can be broken into 2 tasks ji1 and ji2 .,A schedule is an assignment of jobs to time intervals on machines such that, jij must be processed by Pj and the processing time is tij ., No machine processes m

16、ore than one job at any time., ji2 may not be started before ji1 is finished.,Construct a minimum-completion-time 2 machine schedule for a given set of N jobs.,Let ai = ti1 0, and bi = ti2 .,Example Given N = 4,T1234 = ?,40,11/12,Discussion 11: What if ai = 0?,1 Greedy Algorithms,【Proposition】 An optimal schedule exists if min bi , aj min bj , ai for any pair of adjacent jobs Ji and Jj . All the schedules with th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论