Multithreaded Sliding Window Approach to Improve Exact Pattern Matching Algorithms

In this paper an efficient pattern matching approach, based on a multithreading sliding window technique, is proposed to improve the efficiency of the common sequential exact pattern matching algorithms including: (i) Brute Force, (ii) Knuth-Morris-Pratt and (iii) Boyer-Moore. The idea is to divide the text under-search into blocks, each block is allocated one or two threads running concurrently. Reported experimental results indicated that the proposed approach improves the performance of the well-known pattern matching algorithms, in terms of search time, especially when the searched patterns are located at the middle or at the end of the text. Keywords—pattern matching; multithreading; sliding window; Brute Force; Knuth-Morris-Pratt; Boyer-Moore


I. INTRODUCTION
In the world of Internet and the increasing availability of huge data, text remains the main form to exchange knowledge.Searching text for a "string matching", also referred to as "pattern matching" , is an active research area with respect to text processing domain.Pattern matching algorithms are the basic components used in the implementation of practical software applications existing under most operating systems.This area of research is expected to grow widely due to the increasing demand of speed associated with many applications related to pattern matching [1].Examples of pattern matching applications include: text editors, database queries, computational molecular biology, network intrusion detection system, information retrieval, natural language processing, web search engines, language syntax checker, digital libraries, two dimensional mesh, ms word spell checker and many other applications [2].The quantity of available data in these fields increases enormously.This is the reason why algorithms should be efficient even if the speed and capacity of storage of computers increase regularly.Pattern matching consists of finding one, or more generally, all the occurrences of a pattern in a text.More formally, the input to the pattern matching problem are: (i) the pattern P and (ii) the text T .The pattern is denoted by P = T [0 ... m-1], where m is the length of the pattern.The text is denoted by T = T [0 ... n-1], where n is the length of the text.Both strings are built over a finite set of characters called an alphabet and denoted by [3].Many sequential algorithms exist for pattern matching and are widely used in practice.The most well-known ones are : 1) Brute-force exact pattern matching algorithm.
The sliding window mechanism [4] is always utilised to implement the previous sequential pattern matching algorithms.
Where the text is scaned with the help of a window whose size is generally equal to m (the same as pattern size).The search process starts with aligning the left ends of the window and the text and then compare the characters of the window with the characters of the pattern p.After a complete match of the pattern or after a mismatch is reported the window will be shifted to the right.The process is repeated again until the right end of the window goes beyond the right end of the text.The main issue associated with sequential exact pattern matching algorithms is the efficiency.More specifically, as the text size increases the efficiency tends to degrade.The work presented in this paper utilises multithreading programming technique to execute the pattern matching process simultaneously in a timesharing manner.The text is divided into text blocks and assigned to threads.More specifically, each block is assigned to: (i) forward thread and (ii) backward thread.The forward thread runs in a forward direction starting from the top of the block "top-down".While backward thread starts the search process from the bottom of the block "bottom-up".Each thread scans and searches the text at different places as the string may occur anywhere within the text.The conjecture advantage is to improve the performance of the search process.
The rest of this paper is organised as follows.Section II gives a review of related work on pattern matching algorithms along with some background on sliding window technique and multithreading motivation.Section III describes the proposed multithreaded sliding window technique for pattern matching algorithms.Section IV presents an evaluation of the proposed approach as applied to a range of different data collections.Section V summarises the work and indicates some future research directions.

II. LITERATURE REVIEW
This section provides a review of sequential pattern matching algorithms, sliding window mechanism and multithreading.The section is organised as follows: Section II-A presents Brute-Force exact pattern matching algorithm, Section II-B provides an overview of Knuth-Morris-Pratt exact pattern matching algorithm, While II-C presents Boyer-Moore exact pattern matching algorithm.An overview of sliding window mechanism is presented in Section II-D.Section II-E provides an overview of the related work on multithreading programming technique as a solution to the efficiency problem associated with sequential pattern matching algorithms.

A. Brute-Force Exact Pattern Matching Algorithm
This section presents an overview of Brute-Force (BF) exact pattern matching algorithm.The BF algorithm is a naive algorithm that compares each character in the pattern with its corresponding character in the input text.In a case of a complete match or a mismatch of the pattern it shifts one position to the right [1].It is worth to note that the BF algorithm has no preprocessing phase; it has only a searching phase.During the searching phase, each position in the text T is checked to see if the pattern P starts in that position.With respect to the time complexity of the searching phase, in the worst case, the time complexity is O(mn) where m is the pattern length and n is the text length [5].

B. Knuth-Morris-Pratt (KMP) Exact Pattern Matching Algorithm
In this section an overview of Knuth-Morris-Pratt (KMP) is provided.KMP algorithm was proposed by Knuth, Morris and Pratt in 1977 [6] to improve and speed up the algorithm that proposed earlier by Morris and Pratt [7].The KMP algorithm performs a character comparison from left to right of the pattern and it avoids comparisons with the input text that has previously been involved in comparison with some element of the input pattern.This is done by using information of the previous character that has already been tested in order to decide the next sliding position.Unlike the BF algorithm, the KMP algorithm has preprocessing phase, in addition to the searching phase.Within the preprocessing phase the pattern is preprocessed to find matches of prefixes of the pattern with the pattern itself.The information obtained from the preprocessing phase is used to avoid naive BF useless shifts of the pattern, so backtracking on the string never occurs.More specifically, KMP algorithm consists of the following two functions: 1) P ¯refix Function.This function is used to compute the number of shifts that the pattern can be moved to avoid wasteful comparisons.

2) K
¯MP Matcher This function takes the text, the pattern and the prefix function as inputs.The target is to find the occurrence of the searched pattern within the text.
The time complexity of the preprocessing and searching phases are Θ(m) and Θ(n) respectively, where m is pattern length and n is the text length [3].

C. Boyer-Moore Pattern Matching Algorithm
This section presents Boyer-Moore pattern matching algorithm.Boyer-Moore algorithm searches from left to right and performs character comparisons within its sliding window from right to left.The BM algorithm performs preprocessing on the pattern by using two heuristics: (i) bad-character shift and (ii) good-suffix shift.In bad-character heuristic, the input pattern is shifted to align the mismatched character with the rightmost position, where the mismatched character is placed in the input pattern.In the good-suffix, the mismatch occurs in the middle of the search string.Therefore the input pattern is shifted to the next occurrence of the suffix in the string [8].The time and space complexity of the preprocessing phase is O(m+| |).While the running time of the searching phase is O(nm + | |) in the worst case.Note that m is the pattern length and n is the text length [1].

D. Sliding Window Mechanism
In this section an overview of sliding window mechanism is presented.Most pattern matching algorithms scan the text with the help of a window, whose size is generally equal to the pattern size.This mechanism is referred to as "sliding window" mechanism [4].The general procedure is as follows.At the beginning of the search, the left end of the window is aligned with the left end of the text.Then the occurrence of the pattern is checked, this process is referred to as an "attempt".The check is generally carried out by comparing the characters of the window with the characters of the pattern [9].After finding a match of the pattern, or after a mismatch is detected, the window is shifted to the right by a finite number of positions according to the shift strategy.The same process is repeated again until the right end of the window goes beyond the right end of the text.Recently some researchers suggested the use of multiple windows to scan the text simultaneously, in order to improve the efficiency of the search process [10], [11], [12].

E. Threads and Multithreading
This section presents an overview of: (i) threads and multithreading and (ii) utilising multithreading techniques to reduce the computation time of pattern matching approaches.With respect to threads and multithreading, the thread is the basic unit of execution [13].In single threaded applications, all operations are executed sequentially by a single thread.More specifically, an operation must complete before the other operations can begin, also there is only one thread running at a time [14].While in multithreading applications, multiple threads run simultaneously in a timesharing manner [15].This allows many parts of the same program to run concurrently on a single processor system [14].Java makes concurrency available to application programs running on a Java machine.Java programs can have multiple threads of execution, where each thread is responsible for a portion of the program that may execute concurrently with other threads while sharing with them application resources such as memory [14].Every Java thread has a priority which helps the operating system to determine the order in which threads are scheduled.A thread with a higher priority is allocated a processor time before a lower priority one [14].Recently, multithreading techniques have been utilised to decrease the computation time of pattern matching approaches [16], [17].Kofahi and Abusalama [16] suggested a framework that uses data distribution and multithreading techniques for string matching.The main idea is based on applying a multithreading technique which concurrently searches the text at different positions.The framework combines two techniques of concurrency: (i) a multithread technique that searches the target text simultaneously in a time sharing manner, in which each thread starts searching the target text at different positions, and (ii) a technique that distributes the search load among multiple servers and implements the multithreading approach on small sub text.A novel multithreaded string matching approach was proposed by Nirmala and Rajagopalan for exact occurrences of string in DNA sequences [17].In this approach, the DNA sequence is divided into parts depending on string size, and then multiple search threads search the string simultaneously in a timesharing manner.The proposed technique depends on the pre-processing phase, which retrieves the index.Rasool et al. [18] also utilised multithreading techniques to improve the CPU utilization and increase the time efficiency for a hybrid string matching algorithm that combines Knuth-Morris-Pratt (KMP) and Boyer-Moore.The work presented in this paper utilises multithreading techniques to execute the pattern matching process simultaneously in a timesharing manner.The main idea is to split the text into text blocks with associated threads running on the blocks.Each thread scans and searches the text in different places as the string may occur anywhere within the text.

III. THE PROPOSED EFFICIENT PATTERN MATCHING APPROACH
In this section the suggested Efficient Pattern Matching (EPM) approach is described.As noted in the introduction to this paper the proposed approach adopts: (i) sliding window mechanism and (ii) multithreading techniques.The intuition behind using multithreading techniques is that it could improve the efficiency of the common sequential exact pattern matching algorithms.The EPM approach is applied to three well-known pattern matching algorithms namely: (i) BF, (ii) KMP and (iii) BM.The rest of this section is organized as follows: Section III-A presents the proposed Efficient Pattern Matching Approach, while Section III-B provides a complete working example explaining how it works.

A. Efficient Pattern Matching (EPM) Approach
In this section the Efficient Pattern Matching (EPM) approach is described.As noted above the EPM utilises multithreading techniques to improve the efficiency of the pattern matching algorithms.The main idea is to divide the text document into blocks and assign the blocks to threads to run concurrently.Figure 1 illustrates the process.The text is divided into w blocks, and each block is allocated a "forward" thread that runs in a forward direction starting from the top of the block.In Figure 1 each block is assigned to one forward thread.In the work presented in this paper assigning a block to two independent threads is also considered.More specifically, each block is assigned to: (i) forward thread and (ii) backward thread.Figure 2 illustrates assigning each block to a forward and backward thread.Recall that forward thread runs in a forward direction starting from the top of the block "topdown".While backward thread starts the search process from the bottom of the block "bottom-up".

Backward Thread 2
Backward Thread 1 Backward Thread 3 Backward Thread w Fig. 2: A text divided into w blocks, each block is assigned to two threads: (i) a forward thread and (ii) a backward thread It is interesting to note here that the size of each block specified according to the total number of words in the text as follows: 1) If the number of words in the text is even: Block size = n/w.
Where n is the number of words in the text and w is the number of blocks.2) If the number of words in the text is odd: Block size= n/w.The last block size = block size + the reminder of the division.Again, n is the number of words in the text and w is the number of blocks.
Algorithm Efficient Pattern Matching approach presents the proposed EPM procedure.The input to the algorithm is the text under search text and the number of desired blocks N umOf Blocks.The process commences by dividing the text into blocks according to N umOf Blocks and number of words in text as explained earlier (line 10).Then calculate and allocate the required number of threads, note here that two threads (forward and backward) will be allocated for each block (line 11).After that we loop through the blocks (line 12) and on each iteration: (i) assign block i to a forward thread F T (line 13), (ii) assign block i to a backward thread BT (line 14), (iii) start search process at block i, using the specified pattern matching algorithm, with respect to F T (line 15), and (iv) start search process at block i, using the specified pattern matching algorithm, with respect to BT (line 16).The output of the EPM procedure will be the occurrences of the searched pattern in the text associated with the time consumed to find them using the forward and backward threads.Start search process at block i, using the specified pattern matching algorithm, with respect to F T

18:
Start search process at block i, using the specified pattern matching algorithm, with respect to BT 19: end for 20: END PROCEDURE Ef f icientP atternM atching(text, N umOf Blocks)

B. working example
Section III-A above explained the proposed EPM approach.This section presents a complete example to clarify how the proposed EPM approach works.Figure 3 presents an example of the searching process using EPM approach.In this example, the text size = 275 words and the target pattern is "compression".The text is divided into three blocks (w = 3).The sizes of the first and second blocks were 92 words, while the third block has 91 words.From the figure we can observe that the target pattern has three occurrences and each occurrence is located at different position.The first occurrence is located at the begging of the text (the first word).The second occurrence is located in the middle of the text (the number of words after it almost equal to the number of the words before it).The last occurrence is located at the end of the text (the last word).Each block is assigned to two threads: (i) a forward thread (FT) and (ii) a backward thread (BT).Forward and backward threads are used to search the pattern concurrently in a timesharing manner from different positions.In detail, the FT scans the text from the top to the bottom of the block.While the BT scans the text from the bottom to the top of the block to speed up the process of finding the required pattern.It is interesting to note that each thread uses a sliding window of size 11 characters, which is equal to the pattern size (compression).With respect to the number of shifts needed to move each sliding window, it depends on the utilised pattern matching algorithm (KMP, FB, and BM).During the searching process, the following will occur: 1) In the first block, F T 1 will find the pattern before BT 1. 2) In the second block, the pattern will be found by the fastest between F T 2 and BT 2. 3) In the third block, the pattern will be found first by BT 3.
In the context of evaluation, the execution time, for all threads, was recorded and the minimum time for each pattern occurrence was taken.

IV. EXPERIMENTS AND EVALUATION
In this section we present an overview of the adopted experimental set up and the evaluation results obtained.This section is organised as follows: sub-Section IV-A provides an overview of the data collections used to evaluate the proposed Efficient Pattern Matching (EPM) approach and a generic overview of the adopted evaluation measure.While sub-Section IV-B presents and discusses the obtained results.

A. Evaluation Data Collections and Criteria
The suggested Efficient Pattern Matching (EPM) approach was evaluated using three different data collections obtained from "textfiles"1 .The general characteristics of the data collections are provided in Table I.From the table it can be observed that the evaluation data collections are varied in context of size and topic.In order to evaluate the efficiency of the proposed EPM approach, run time measure was used.Run time refers to the total time an algorithm needs to find each occurrence of the pattern including any preprocessing time.Note that the running times reported in this paper are all in nano second.Since the running time can be affected by many factors such as memory usage and CPU of the system, each test was repeated ten times and all preprocessing times were included.The results were very stable across different runs.All experiments were conducted on Microsoft Windows 7 Professional, 32-bit Operating system with a 3.40 GHz Intel Core i7 processor, and 8 GB memory.Because the performance Fig. 3: An example of the searching process using our proposed EPM approach  14,1993 of pattern matching process affected by: (i) text length, (ii) pattern length and (iii) pattern occurrence frequency [19], the conducted experiments were designated to take the previous factors into consideration, more specifically: 1) The proposed approach evaluated against three deferent text length (See Table I).
2) The proposed approach evaluated against different pattern length, range from 2 to 26 characters as shown in Table II.
3) The proposed approach evaluated against different pattern occurrence positions: (i) beginning, (ii) middle and (iii) end of the text.Patterns were induced into evaluation texts at these three different positions.Each experiment, reported later in this paper, shows the searching results for all these three positions.
For comparison purposes the three sequential pattern matching algorithms were also applied to the data collections, namely: 1) Brute-Force (BF) sequential exact pattern matching algorithm.2) Knuth-Morris-Pratt (KMP) sequential exact pattern matching algorithm.3) Boyer-Moore sequential exact pattern matching algorithm.

B. Results and Discussion
In this section, we compare the performance of each sequential algorithm (BF, KMP and Boyer-Moore) with its corresponding multithreading pair (EPM approach) with respect to: (i) different text length, (ii) different pattern length and (iii) different pattern occurrence position.With respect to the EPM approach the text was divided into three blocks (w = 3) and each block was assigned two threads running in both directions (i.e forward and backward).Commencing with BF algorithm, Table III presents the obtained results with respect to Abyssal evaluation data collection, Table IV presents the obtained results with respect to Ejournal evaluation data collection and Table V presents the obtained results with respect to Mail-list evaluation data collection.
The tables presents the running time of the forward thread FT, backward thread BT and the minimum time was taken as the fastest result.From the tables it can be observed that the average time taken by the proposed multithreading approach (EPM) is lower than the sequential techniques especially when the pattern is located at the middle or the end of the text.The results are reasonable since the multithreading approaches search the text from multiple sides while the sequential approaches search the text from one side only.This can be justified by the following two advantages of the proposed approach.First, this approach is based on dividing the text into blocks before the search phase.Second, each block is allocated two search threads that scan the block concurrently to match the pattern, with the use of sliding window technique.It is interesting to note that when the pattern occurs at the beginning of the text sequential pattern matching outperforms EPM approach for some cases, the reason behind this is that assigning tasks to threads consumes time in addition to the search time.
Regarding KMP algorithm, Table VI presents the obtained results with respect to Abyssal evaluation data collection, Table VII presents the obtained results with respect to Ejournal evaluation data collection and Table VIII presents the obtained results with respect to Mail-list evaluation data collection.The same as the case of BM algorithm, the multithreading approach outperforms sequential approach in terms of average search time especially when the pattern is located at the middle or the end of the text regardless the pattern length.
With respect to BF algorithm, Table IX presents the obtained results with respect to Abyssal evaluation data collection, Table X presents the obtained results with respect to Ejournal evaluation data collection and Table XI presents the obtained results with respect to Mail-list evaluation data collection.Again, the multithreading approach outperforms sequential approach in terms of average search time especially when the pattern is located at the middle or the end of the text regardless the pattern length.The previous results are summarized and plotted in the next three figures to compare two approaches: (i) sequential and (ii) multithreading (EPM) with respect to all the three used pattern matching algorithms.Figure 4 presents a comparison between all sequential and multithreading approaches with respect to Abyssal data collection.From the figure it can be observed that: (i) as noted earlier, multithreading approaches clearly outperform sequential approaches in terms of average case time and (ii) the minimum average case time obtained from multithreading BM and multithreading KPM.presents a comparison between all sequential and multithreading approaches with respect to Ejournal data collection.From the figure it can be observed that, and the same as Abyssal data collection, multithreading approaches clearly outperform sequential approaches and the minimum average case time obtained from multithreading BM and multithreading KPM. Figure 6 presents a comparison between all sequential and multithreading approaches with respect to E-mail data collection (the largest data collection).From the figure it can be observed that, and the same as Abyssal and Ejournal data collection, multithreading approaches clearly outperform sequential approaches.However, the minimum average case time obtained from multithreading BM.In this paper, Efficient Pattern Matching (EPM) approach based on multithreading techniques has been proposed to improve the efficiency of sequential pattern matching algorithms.Three common pattern matching algorithm have been considered: (i) Brute Force, (ii) Knuth-Morris-Pratt and (iii) Boyer-Moore.The central idea was to divide the text into blocks and assign each block to two threads, forward thread and backward thread, to conduct the search process concurrently.The proposed approach was evaluated using different text size and various pattern lengths.From the reported experimental results, presented in this paper, it was demonstrated that the proposed multithreading approach shows remarkable performance gain compared with the traditional sequential approach, especially when the lookup patterns were located at the middle and the end of the text regardless the text length or the pattern length.With respect to future work the authors intend to investigate the effect of multithreading approach on Arabic data collections.

Fig. 1 :
Fig.1: A text divided into w blocks,each block is assigned to onel forward thread

4 : 8 :
text The text under search 5: N umOf Blocks The number of desired blocks 6: OUTPUT 7: The occurrences of the searched pattern in the text associated with the time consumed to find them using the forward and backward threads block A segment of text 9: F T Thread starts searching from the top of the block 10: BT Thread starts searching from the bottom of the block 11: START PROCEDURE Ef f icientP atternM atchingl(text, N umOf Blocks) 12: Divide text into Blocks 13: Allocate (2* NumOfBlocks) threads (two threads for each block) 14: for i = 0 to i = N umOf Blocks do 15: Assign block i to F T 16:Assign block i to BT 17:

Fig. 4 :
Fig.4: Comparison between all sequential and multithreading approaches with respect to Abyssal data collection

Fig. 5 :
Fig. 5: Comparison between all sequential and multithreading approaches with respect to Ejournal data collection

Fig. 6 :
Fig.6: Comparison between all sequential and multithreading approaches with respect to Mail-list data collection

TABLE I .
EVALUATION DATA COLLECTIONS

TABLE II .
AN EXAMPLE EVALUATION PATTERN WITH VARIABLE LENGTH

TABLE III .
AVERAGE TIME IN NANO SECONDS FOR SEQUENTIAL BM AND EPM APPROACH FOR BM ALGORITHM (MULTITHREADING BM) WITH RESPECT TO ABYSSAL DATA COLLECTION

TABLE IV .
AVERAGE TIME IN NANO SECONDS FOR SEQUENTIAL BM AND EPM APPROACH FOR BM ALGORITHM (MULTITHREADING BM) WITH RESPECT TO EJOURNAL DATA COLLECTION

TABLE VI .
AVERAGE TIME IN NANO SECONDS FOR SEQUENTIAL KMP AND EPM APPROACH FOR KMP ALGORITHM (MULTITHREADING KMP) WITH RESPECT TO ABYSSAL DATA COLLECTION

TABLE VII .
AVERAGE TIME IN NANO SECONDS FOR SEQUENTIAL KMP AND EPM APPROACH FOR KMP ALGORITHM (MULTITHREADING KMP) WITH RESPECT TO EJOURNAL DATA COLLECTION

TABLE IX .
AVERAGE TIME IN NANO SECONDS FOR SEQUENTIAL BF AND EPM APPROACH FOR BF ALGORITHM (MULTITHREADING BF) WITH RESPECT TO ABYSSAL DATA COLLECTION

TABLE X .
AVERAGE TIME IN NANO SECONDS FOR SEQUENTIAL BF AND EPM APPROACH FOR BF ALGORITHM (MULTITHREADING BF) WITH RESPECT TO EJOURNAL DATA COLLECTION