0 Up votes0 Down votes

0 views8 pagesSpace and time tradeoff of ADA

May 08, 2019

© © All Rights Reserved

ODT, PDF, TXT or read online from Scribd

Space and time tradeoff of ADA

© All Rights Reserved

0 views

Space and time tradeoff of ADA

© All Rights Reserved

- Keral SEM 2
- External Sorting
- Data Structures
- Advanced Programming
- 05987184
- Sorting
- AMCAT 2012 Syllabus
- 350prog1F12
- IT321- Data - Part 1
- DAA FILE
- Hughes
- base sas
- Truss Analyzer Code C
- Cookbook — Pandas 0.23.4 Documentation
- Info Vis 2013
- algorithms and structures
- Arrays
- Study Guide Final
- All_ds_solution.doc
- Cs4811 Perceptron Learning

You are on page 1of 8

and store the additional information obtained to accelerate solving

the problem afterward. We call this approach input enhancement.

1) Counting Method for sorting

2) Boyer-Moore algorithm for string matching and its simplified

version suggested by Horspool.

offs simply uses extra space to facilitate faster and/or more flexible

access to the data. We call this approach prestructuring.

time trade-off: some processing is done before a problem in

question is actually solved but, unlike the input-enhancement

variety, it deals with access

structuring. We illustrate this approach by:

1) Hashing

2) Indexing With B-trees

for-time

trade-off idea: dynamic programming. This strategy is based on

recording solutions to overlapping subproblems of a given

problem in a table from which a solution to the problem in question

is then obtained.

we discuss its application to the sorting problem.

sorted, the total number of elements smaller than this element and

record the results in a table.

These numbers will indicate the positions of the elements in the

sorted list.

Thus, we will be able to sort the list by simply copying its elements

to their

appropriate positions in a new, sorted list. This algorithm is called

comparison counting sort.

Page no.255 In Text-Book PDF(Important Problem Table).

*ALGORITHM:- ComparisonCountingSort(A[0..n − 1])

//Input: An array A[0..n − 1] of orderable elements

//Output: Array S[0..n − 1] of A’s elements sorted in nondecreasing

order

for i ← 0 to n − 1 do Count[i] ← 0

for i ← 0 to n − 2 do

for j ← i + 1 to n − 1 do

if A[i] < A[j ]

Count[j ] ← Count[j ] + 1

else Count[i] ← Count[i] + 1

for i ← 0 to n − 1 do S[Count[i]] ← A[i]

return S

*Efficiency Of Algorithm.

Text book Pdf Page no.255

See only c(n) = part

different pairs of an n-element array. More formally, the number of

times its basic operation, the comparison A[i] < A[j ], is executed

is equal to the sum we have encountered several times already.

selection sort and in addition uses a linear amount of extra space.

On the positive side, the algorithm makes the minimum number of

key moves possible, placing each of them directly in their final

position in a sorted array.

elements to be sorted belong to a known small set of values.

Assume, for example, that we have to sort a list whose values can

be either 1 or 2. Rather than applying a general sorting algorithm,

we should be able to take advantage of this additional information

about values to be sorted.

Indeed, we can scan the list to compute the number of 1’s and the

number of 2’s in it and then, on the second pass, simply make the

appropriate number of the first elements equal to 1 and the

remaining elements equal to 2.

More generally, if element values are integers between some lower

bound l and upper bound u, we can compute the frequency

of each of those values and store them in array F [0..u − l].

Then the first F [0] positions in the sorted list must be filled with l,

the next F [1] positions with l + 1, and so on.

All this can be done, of course, only if we can overwrite the given

elements.

with some other information associated with their keys so that we

cannot overwrite the list’s elements.

Then we can copy elements into a new array S[0..n − 1]to hold the

sorted list as follows.

value l are copied into the first F [0] elements of S,

i.e., positions 0 through F [0] − 1;

the elements of value l + 1 are copied to positions from F [0] to (F

[0] + F [1]) − 1; and so on.

distribution in statistics, the method itself is known as distribution

counting.

//Sorts an array of integers from a limited range by

distribution counting

//Input: An array A[0..n − 1] of integers between l and u (l ≤ u)

//Output: Array S[0..n − 1] of A’s elements sorted in nondecreasing

order

for j ← 0 to u − l do D[j ] ← 0

//initialize frequencies

for i ← 0 to n − 1 do D[A[i] − l] ← D[A[i] − l] + 1 //compute

frequencies

for j ← 1 to u − l do D[j ] ← D[j − 1] + D[j ]

//reuse for distribution

for i ← n − 1 downto 0 do

j ← A[i] − l

S[D[j ] − 1] ← A[i]

D[j ] ← D[j ] − 1

return S

linear algorithm because it makes just two consecutive passes

through its input array A.

sorting algorithms—mergesort, quicksort, and heapsort—we have

encountered.

obtained by exploiting the specific nature of inputs for which sorting

by distribution counting works, in addition to trading space for time.

The problem of string matching requires finding an occurrence of a

given string of m characters called the pattern in a longer string of

n characters called the text.

The brute-force algorithm for this problem simply matches

corresponding pairs of characters in the pattern and the text left to

right and, if a mismatch occurs, shifts the pattern one position to

the right for the next trial.

Since the maximum number of such trials is n − m + 1 and, in the

worst case, m comparisons need to be made on each of them, the

worst-case efficiency of the brute-force algorithm is in the O(nm)

class.

On average, however, we should expect just a few comparisons

before a pattern’s shift, and for random natural-language texts, the

average-case

efficiency indeed turns out to be in O(n + m).

exploit the

input-enhancement idea: preprocess the pattern to get some

information about it, store this information in a table, and then use

this information during an actual search for the pattern in a given

text.

The principal difference between these two algorithms lies in the

way they

compare characters of a pattern with their counterparts in a text:

the Knuth-Morris-Pratt algorithm does it left to right,

whereas the Boyer-Moore algorithm does it right to left.

Since the latter idea leads to simpler algorithms, it is the only one

that we will pursue here.

simple, its

actual implementation in a working method is less so.

*Horspool’s Algorithm :-

text:

s0 ... c ...s n−1

BARBER

Starting with the last R of the pattern and moving right to left, we

compare the corresponding pairs of characters in the pattern and

the text. If all the pattern’s characters match successfully, a

matching substring is found. Then the search can be either stopped

altogether or continued if another occurrence of the same pattern is

desired.

Clearly, we

would like to make as large a shift as possible without risking the

possibility of missing a matching substring in the text.

at the character c of the text that is aligned against the last

character of the pattern.

example

we can safely shift the pattern by its entire length.

S 0 .... S ... s n−1

BARBER

BARBER

Case 2 :- If there are occurrences of character c in the pattern but

it is not the last one there—e.g., c is letter B in our example—the

shift should align the rightmost occurrence of c in the pattern with

the c in the text

S 0 .... B ... s n−1

BARBER

BARBER

Case 3 :- If c happens to be the last character in the pattern but

there are no c’s among its other m − 1 characters—e.g., c is letter R

in our example—the situation is similar to that of Case 1 and the

pattern should be shifted by the entire pattern’s length m:

S 0 .... MER ... s n−1

LEADER

LEADER

Case 4 :- Finally, if c happens to be the last character in the pattern

and there are other c’s among its first m − 1 characters—e.g., c is

letter R in our example the situation is similar to that of Case 2 and

the rightmost occurrence of c among the first m − 1 characters in

the pattern should be aligned with the text’s c:

S 0 .... AR ... s n−1

REORDER

REORDER

comparisons can lead to farther shifts of the pattern than the shifts

by only one position always made by the brute-force algorithm.

pattern on every trial, it would lose much of this superiority.

Fortunately, the idea of input enhancement makes repetitive

comparisons unnecessary.

We can precompute shift sizes and store them in a table. The table

will be indexed by all possible characters that can be encountered

in a text, including, for natural language texts, the space,

punctuation symbols, and

other special characters.

The table’s entries will indicate the shift sizes computed by the

formula.

characters of the pattern;

t (c) =

2.the distance from the rightmost c among the first m − 1

characters

of the pattern to its last character, otherwise.

*Horspool’s algorithm :-

Step 1 For a given pattern of length m and the alphabet used in

both the

pattern and text, construct the shift table as described 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 character of the text.

Starting 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 c’s column of the shift table where c

is the text’s character currently aligned against the last character of

the pattern, and shift the pattern by t (c) characters to the right

along the text.

//Implements Horspool’s algorithm for string matching

//Input: Pattern P [0..m − 1] and text T [0..n − 1]

//Output: The index of the left end of the first matching substring

//

or −1 if there are no matches

ShiftTable(P [0..m − 1])

//generate Table of shifts

i←m−1

//position of the pattern’s right end

while i ≤ n − 1 do

k←0

//number of matched characters

while k ≤ m − 1 and P [m − 1 − k] = T [i − k] do

k←k+1

if k = m

return i − m + 1

else i ← i + Table[T [i]]

return −1

Hor-

spool’s algorithm is in O(nm) (Problem 4 in this section’s exercises).

But for

random texts, it is in #(n), and, although in the same efficiency

class, Horspool’s algorithm is obviously faster on average than the

brute-force algorithm.

**Boyer-Moore Algorithm:-

- Keral SEM 2Uploaded bySuresh Kumar
- External SortingUploaded byKumar Ashish
- Data StructuresUploaded byayjnihhh
- Advanced ProgrammingUploaded byJames Pawlak
- 05987184Uploaded bySahilYadav
- SortingUploaded bychanderbal
- AMCAT 2012 SyllabusUploaded byRishi Pall
- 350prog1F12Uploaded byStephen Frankie
- DAA FILEUploaded byDaljit
- IT321- Data - Part 1Uploaded byrayden22
- HughesUploaded byPankaj Kumar
- base sasUploaded byAlok Yadav
- Cookbook — Pandas 0.23.4 DocumentationUploaded bytariqueahmad
- Truss Analyzer Code CUploaded byJulius Rey Baniqued
- Info Vis 2013Uploaded byMécia Sá
- algorithms and structuresUploaded byLølø
- ArraysUploaded bySafder Khan Babar
- Study Guide FinalUploaded byshivambarca
- All_ds_solution.docUploaded bymukul_2526
- Cs4811 Perceptron LearningUploaded byBolaji Awokiyesi
- JFJFUploaded byjskldf boboloko
- Lecturer Computer Science Mcqs Paper 2011Uploaded bySami Ullah
- gcse-7Uploaded byPrashantBhattacharji
- gcse-3Uploaded byPrashantBhattacharji
- CS102-DATA-STRUCT.pdfUploaded byBenvindo DreamsSTS
- Lecture_3Uploaded byMUNESH SINGH
- Dec 2016 Data StrcuturesUploaded bysaif shaikh
- Binary Search sddUploaded byd
- tcs questionUploaded byShraddha
- Mobile Robotics A3Uploaded byAbhishek pullela

- Albert Einstein Was Born at Ulm- IllaaUploaded byTilka Annur Chairil
- Eetop.cn Lab3Uploaded byThomas George
- Building Drifts in ETABS GoodUploaded bykaosar alam
- Design and Construction of a Water TunnelUploaded bywdtalampas
- AJS Twin Manual 1949 to 1955Uploaded byMatchles
- Gdjp Leson PlanUploaded byBillie Kent
- Cardan and CryptographyUploaded byAshish Yadav
- Hazard and Safety Investigations for LNG Tanks.pdfUploaded bycisco
- stx15fUploaded byGrzegorz Miąsko
- InformaciónUploaded byErik Daza
- Ads 1248Uploaded byneko1212121515123001
- Imports EmguUploaded byq_man2512
- Sample.final.examUploaded byCOCO12432345
- Reynolds NumberUploaded bypaoloasequia
- schaums pdf.pdfUploaded byConnor Bell
- NRRS 2055First Revision 2069 DoLIDAR Reduced for WebUploaded byDeepak Kr Gupta
- paperUploaded byHermann Muriel
- LPG Properties (ENGINEERING)Uploaded byGiftObionochie
- ASA PBRUploaded bypiyushhote
- MPLS Technology on IP Backbone NetworkUploaded bytranceforge
- Non-Newtonian FluidUploaded byRandhir Kumar Gupta
- Math53 3rd LE with Answer Key +++Uploaded byJustin Richmond
- PowerVM Processor Virtualization Concepts and ConfigurationUploaded byvishaltyaggi1981
- B2Bi 2.0.2 Basic Training Day 5Uploaded bySidharth Mazumdar
- spmu272Uploaded byMsc Maia
- Final Report S Mtetwa_2Uploaded byblem_0075
- JSTLUploaded bySanjeeb Pradhan
- Thermodynamic Flow Simulation Model in Ducted Axial Fan Using SimulinkUploaded byIAEME Publication
- Zelio Timer Relays_RE11RAMUUploaded byani mariani
- Kathrein 742265V02Uploaded byandahv

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.