




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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第一学期小学升学指导计划
- 北师大版八年级数学下册教学目标分解计划
- 六年级户外体育体验活动计划
- 人教版六年级数学上册拓展练习设计计划
- 2025年中国智慧教室设备行业运行态势及未来发展趋势预测报告
- 2023-2029年中国纯铝箔餐具行业市场发展监测及市场深度研究报告
- 2025-2030年中国紫铜中大异径三通接头项目投资可行性研究分析报告
- 2021-2026年中国燕麦行业市场供需格局及行业前景展望报告
- 2025年中国胶环盖板行业市场发展前景及发展趋势与投资战略研究报告
- 江苏省淮安市淮安中学2025届数学高一下期末教学质量检测试题含解析2
- 《取水许可核验报告编制导则(试行)(征求意见稿)》
- 【中国信科-中信科移动】2023星地融合通信白皮书
- 厨师中暑防范知识讲座
- 水质检测员年终总结
- 公司期货交易及风险控制管理制度
- 娃哈哈私域代运营方案规划
- 阻塞性睡眠呼吸暂停低通气综合征的护理查房
- 氯化钾外渗护理不良事件
- 老年消防知识讲座
- Filemaker数据库使用指南知识分享
评论
0/150
提交评论