版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 7 Space and Time TradeoffsCopyright 2007 Pearson Addison-Wesley. All rights reserved.Thing which matter most must never be at the mercy of things which matter less.- Johann Wolfgang von Goethe (1749-1832)Space and time tradeoffs in algorithm design are a well-known issue for both theoreticia
2、ns and practitioners of computing.Space-for-time tradeoffsTwo varieties of space-for-time algorithms: input enhancement preprocess the input (or its part) to store some info to be used later in solving the problem counting sortsstring searching algorithmsprestructuring preprocess the input to make a
3、ccessing its elements easierhashingindexing schemes (e.g., B-trees)447.1 Sorting by Counting5We discuss its application to the sorting problem.One rather obvious idea is to count, for each element of a list to be sorted, the total number of elements smaller than this element and record the results i
4、n a table.Thus, comparison counting sort algorithm be able to sort the list by simply copying its elements to their appropriate positions in a new, sorted list.Sorting by Counting7Array A0,5 InitiallyAfter pass i = 0 i = 1 i = 2 i = 3 i = 4 Final stateArray S05 62318496194719314762849600000030110012
5、201430150102314502 But the counting idea does work productively in a situation in which elements to be sorted belong to a known small set of values. Assume, for example, that we have to sort a list whose value can be either 1 or 2. Rather than applying a general sorting algorithm, we should be able
6、to take advantage of this additional information about values to be sorted.9Example. Consider sorting the arraywhose values are known to come from the set 11,12,13 and should not be overwritten in the process of sorting.The frequency and distribution arrays are as follows: 131112131212Array values 1
7、1 12 13Frequencies 1 3 2Distribution values 1 4 610Note that the distribution values indicate theproper positions for the last occurrences oftheir elements in the final sorted array.If we index array positions from 0 to n-1, thedistribution values must be reduced by 1 toget corresponding element pos
8、itions.It is more convenient to process the inputarray right to left. 11The last element is 12, and, since its distribution value is 4, we place this 12 in position 4-1 = 3 of the array S that hold the sorted list. Then we decrease the 12s distribution value by 1 and proceed to the next element in t
9、he given array.ExampleA5= 12A4= 12A5= 13A4= 12A5= 11A4= 1314613612612511501512121312111313Assuming that the range of array values isfixed, this is obviously a linear algorithmbecause it makes just two consecutive passesthrough its input array A.This is a better time-efficiency class than thatof the
10、most efficient sorting algorithms mergesort, quicksort, and heapsort we haveencountered.14147.2 Input Enhancement in String MatchingReview: String searching by brute forcepattern: a string of m characters to search fortext: a (long) string of n characters to search inBrute force algorithmStep 1Align
11、 pattern at beginning of textStep 2Moving from left to right, compare each character of pattern to the corresponding character in text until either all characters are found to match (successful search) or a mismatch is detectedStep 3 While a mismatch is detected and the text is not yet exhausted, re
12、align pattern one position to the right and repeat Step 2String searching by preprocessingSeveral string searching algorithms are based on the input enhancement idea of preprocessing the pattern Knuth-Morris-Pratt (KMP) algorithm preprocesses pattern left to right to get useful information for later
13、 searchingBoyer -Moore algorithm preprocesses pattern right to left and store information into two tablesHorspools algorithm simplifies the Boyer-Moore algorithm by using just one tableHorspools AlgorithmA simplified version of Boyer-Moore algorithm:preprocesses pattern to generate a shift table tha
14、t determines how much to shift the pattern when a mismatch occurs always makes a shift based on the texts character c aligned with the last character in the pattern according to the shift tables entry for cHow far to shift?Look at first (rightmost) character in text that was compared: The character
15、is not in the pattern .c. (c not in pattern) BAOBABThe character is in the pattern (but not the rightmost) .O. (O occurs once in pattern) BAOBAB .A. (A occurs twice in pattern) BAOBABThe rightmost characters do match .B. BAOBAB Shift tableShift sizes can be precomputed by the formula distance from c
16、s rightmost ocurrence in pattern among its first m-1 characters to its right end t(c) = patterns length m, otherwise by scanning pattern before search begins and stored in a table called shift tableShift table is indexed by text and pattern alphabet Eg, for BAOBAB:A B C D E F G H I J K L M N O P Q R
17、 S T U V W X Y Z1 2 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6 6 6Example of Horspools alg. applicationBARD LOVED BANANASBAOBAB BAOBAB BAOBAB BAOBAB (unsuccessful search)A B C D E F G H I J K L M N O P Q R S T U V W X Y Z1 2 6 6 6 6 6 6 6 6 6 6 6 6 3 6 6 6 6 6 6 6 6 6 6 6_6Horspools algorithmStep 1
18、. For a given pattern of length m and the alphabet used in both the pattern and text, construct the shift table as describe above.Step 2. Align the pattern against the beginning of the text.Step 3. Repeat the following until either a matching substring is found or the pattern reaches beyond the last
19、 character or the text. 22Starting with the last character in the pattern, compare the corresponding characters in the pattern and text until either all m characters are matched (then stop) or a mismatching pair is encountered. In the latter case, retrieve the entry t(c) from the cs column of the sh
20、ift table where c is the text character currently aligned against the last character of the pattern, and shift the pattern by t(c) characters to the right along the text.24Boyer-Moore algorithmBased on same two ideas:comparing pattern characters to text from right to leftprecomputing shift sizes in
21、two tablesbad-symbol table indicates how much to shift based on texts character causing a mismatchgood-suffix table indicates how much to shift based on matched part (suffix) of the patternBad-symbol shift in Boyer-Moore algorithmIf the rightmost character of the pattern doesnt match, BM algorithm a
22、cts as HorspoolsIf the rightmost character of the pattern does match, BM compares preceding characters right to left until either all patterns characters match or a mismatch on texts character c is encountered after k 0 matches text pattern bad-symbol shift d1 = maxt1(c ) - k, 1 ck matchesGood-suffi
23、x shift in Boyer-Moore algorithmGood-suffix shift d2 is applied after 0 k m last characters were matchedd2(k) = the distance between matched suffix of size k and its rightmost occurrence in the pattern that is not preceded by the same character as the suffixExample: CABABA d2(1) = 4 If there is no s
24、uch occurrence, match the longest part of the k-character suffix with corresponding prefix; if there are no such suffix-prefix matches, d2 (k) = mExample: WOWWOW d2(2) = 3, d2(3) = 3, d2(4) = 3, d2(5) = 3 Boyer-Moore AlgorithmAfter matching successfully 0 k m characters, the algorithm shifts the pat
25、tern right by d = max d1, d2where d1 = maxt1(c) - k, 1 is bad-symbol shift d2(k) is good-suffix shiftExample: Find pattern AT_THAT inWHICH_FINALLY_HALTS. _ _ AT_THAT Boyer-Moore Algorithm (cont.)Step 1 Fill in the bad-symbol shift tableStep 2 Fill in the good-suffix shift tableStep 3 Align the patte
26、rn against the beginning of the textStep 4 Repeat until a matching substring is found or text ends: Compare the corresponding characters right to left. If no characters match, retrieve entry t1(c) from the bad-symbol table for the texts character c causing the mismatch and shift the pattern to the r
27、ight by t1(c).If 0 k mClosed hashing (Open addressing)Keys are stored inside a hash table.AAFOOLAANDFOOLAANDFOOLHISAANDMONEYFOOLHISAANDMONEYFOOLHISAREAANDMONEYFOOLHISARESOONPARTEDAANDMONEYFOOLHISARESOONKeyAFOOLANDHISMONEYARESOON PARTEDh(K)1961071111120123456789101112Closed hashing (cont.)Does not wo
28、rk if n mAvoids pointersDeletions are not straightforwardNumber of probes to find/insert/delete a key depends on load factor = n/m (hash table density) and collision resolution strategy. For linear probing: S = () (1+ 1/(1- ) and U = () (1+ 1/(1- )As the table gets filled ( approaches 1), number of
29、probes in linear probing increases dramatically: 44447.4 B-TreesB-TreesThe idea of using extra space to facilitate faster access to a given data set is particularly important if the data set in question contains a very large number of records that need to be stored on a disk.A principal device in organizing such data sets in an index, which provides some information about the location of records with indicated key values.45For data sets of structured records, the most
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年违约借款合同违约责任追究办法3篇
- 2025年度个人房屋买卖价格调整及支付合同4篇
- 2025年度企业应收账款债权转让与风险控制协议书3篇
- 2025年度房地产样板间设计与施工合同范本4篇
- 2025年度电子商务个人劳务派遣合作协议书4篇
- 工厂租地合同(2篇)
- 二零二五年度民政局离婚协议书模板法律咨询附加服务合同4篇
- 2025年度销售顾问市场调研聘用合同2篇
- 2024西部县域经济百强研究
- STEM教育实践讲解模板
- 2025年山东浪潮集团限公司招聘25人高频重点提升(共500题)附带答案详解
- 2024年财政部会计法律法规答题活动题目及答案一
- 2025年江西省港口集团招聘笔试参考题库含答案解析
- (2024年)中国传统文化介绍课件
- 液化气安全检查及整改方案
- 《冠心病》课件(完整版)
- 2024年云网安全应知应会考试题库
- 公园保洁服务投标方案
- 光伏电站项目合作开发合同协议书三方版
- 高中物理答题卡模板
- 芳香植物与芳香疗法讲解课件
评论
0/150
提交评论