 Hello friends, today we will see searching, that is sequential searching. So learning outcome for this session is at the end of this session students will be able to find all the text positions where that pattern occurs using brute force algorithm and KMP algorithm. Now first we will see when to prefer sequential searching. Sequential searching means just scan the text for finding the occurrences of the pattern or the basic query. Now sequential searching is appropriate when the text is not processed or when the text is too small we can afford sequential searching or the text is too volatile that we don't have the time to build index so we can use sequential searching and the more obvious reason is that index space over it is not afforded. So sequential searching means just start scanning the text from the start to end for the given pattern. So the first approach for this is brute force approach. It's a simplest algorithm where we will try all the possible positions in the text. So every position of the pattern will be matched against the text position. Now let us see with example, consider that here is the text that is the long continuous sequence of the characters and the pattern is CAPC. So here I have started the indexing with 1 so you can start with 1 or 0. So first character will be matched of the pattern and text so here T of 1 and P of 1. So obviously the pattern or the character is not matching or mismatch that we need to shift this window of a pattern to right. So again this first character of the pattern will be matched with the second character of the text. This is again not matching so shift the window to right. So this now see that is the third character of the text and the first character of the pattern is matching. So we need to shift, we need to move the pointer to right not the window. So here we are matching now the fourth character of the text and second character of the pattern. So whenever there is match we need to move the pointer of the text and pattern and whenever there is a mismatch we need to shift the window of a pattern. So again B is matched and then C is matched. So we have reached to the end of the pattern it means that the complete pattern is found in the text. So we have found the pattern at the position of 3 or at the third position we have found the pattern or occurrences. Now this is the first occurrences we need to find all the occurrences of the pattern. So at this moment pause the video and try to find out the next occurrence or how you will start finding the next occurrence. So here our pointer is at this position. So where you will initialize your pointers to start for finding the next pattern. So we have found it at third position so we have to again start from the fourth position because there can be overlap in between the text and the pattern to find the next. So here again it is a mismatch so we will start shifting the window. So in that way we will come to the second occurrence of the pattern. So we will move this till the end of the text. So this is a brute force approach. So here assume that the text length is n characters and pattern length is m characters. So o of n positions are examined at o of m position. So worst case complexity of brute force approach is o of mn whereas average case complexity is o of n. It means that we can find the pattern randomly at any position. So next algorithm is Nooth Morris Pratt that is KMP algorithm. Now in this algorithm window will be slide over this text but it does not try all the window positions. Instead what it does in this algorithm it reuses the information from the previous checks that is the number of characters matched. So now you can consider this example here this a, b, a is matched and now this is the mismatch. So these three characters matched which is obvious of prefix can be used for shifting this window. Now how to use this information that we will see. So first we have to find out how many characters are matched as a prefix and also as a suffix. Now what do you mean by this? For example consider this position which is j position. Now at this position 1, 2, 3 are the prefix or the characters in the prefix same as 3, 4, 5 are the characters are of the suffix. So we need to find the longest pattern from 1 to j minus 1 which is same as prefix and suffix. Now if this is not matching the next characters, so two characters of prefix are the same as two characters of suffix. Again if these are not matching we have to see that the first character of prefix is the same as the last character as a suffix. So in this way we can find how many characters of prefix and as well as suffix are matching that we will be using as a prefix value. Now let us say with example, now consider this is our pattern. Now the first character itself does not have anything before that so its prefix value will be always 0. Now consider the second string which is up to ab. Now in this ab, a is a prefix whereas b is suffix. Now these prefix and suffix are not matching so its prefix value will be 0. It means that there is no start with b or no ending with a. Now consider the next string aba. Now the highest prefix is up to the position of 3 is ab and the highest suffix is ba but this ab and ba are not matching. So what we need to do is that delete one character. So what will be the prefix now a and what will be the suffix a? Is this matching? Yes it is matching. So how many characters are matching as prefix and suffix only one. So prefix value for this position will be 1. Now consider abab. Now in this case again first consider the highest prefix which is aba and the suffix is bab this is not matching. Delete one character now this ab and this is also ab so two characters are matching so the prefix value for this position will be 2. What will be for this next position? We can just pause the video and try to calculate. So 3 aba and this is also aba so prefix value will be 3. For this character c now it will be again 0 because the suffix it is ending with c whereas there is no character starting with c. So go on starting with abab and still babac go on deleting the character and see that there is no match. So prefix value for c is 0. Again go for a now it is only the one character which is matching. So these are the prefix value. So in KMT algorithm the first step is to calculate the prefix value of every character. So at this moment pause the video and try to calculate the prefix value for the given pattern. So yes these are the values for g obviously 0, c 0, g 1 because it is again prefix is g so it will be 1. Then gc this is prefix is gc suffix is also gc so it will be 2, t there is no character starting with t so it will be 0 and also a is not present in as in prefix it will be 0. So this we have calculated prefix value. Now we have to use this prefix value in finding the pattern. So second part is of matching. Now how it will be used in the matching process? So assume that this green color shows that it is match characters whereas at this position of j there is a mismatch. Now we have started our text position is at i and pattern is moved up to j. So now this is a mismatch means that we need to move this particular window to write. But how many characters? So i is a starting position from where we want to shift. Now how many match characters that is j minus 1 are match characters but are we going to shift by all the characters? No. How many are what is the prefix value of this j minus 1? It means that how many characters of prefix and suffix are same that we want to find. So this will give you a new window position. Now let us see with the example. Now this is our text and this is our pattern. So i means the pointer for text and j means pointer for pattern. So first character itself is mismatch. So obviously we need to move the window to next image position. Now this is matching the first character and the first character of the pattern and the second character of the text is matching. So we need to move the pointer so j will become 2. See we are not moving the text position we are just moving the pointer of this 2. So i will be as it is we are moving the pointer of the text with the other variable. So j will be 3. Now in this case j will be 4 and now here it is a mismatch. So how many characters are matched? 4 characters are matched and at the fifth position there is a mismatch. So here mismatch position is 5. Number of characters matched that is j minus 1 are 4. Now what is the prefix value of this character? Now we have already calculated it prefix value of the character. So here it is 2. So i plus j minus 1 minus prefix value of j minus 1 which will give you 4. So it means that from 2 we are going to move this window to the fourth position. So this is your fourth position. The same way now first j will be 1 then we will be moving again we will start matching the characters and at that position we have found that this g and t is mismatch. So again same is the position 5. So now i is 4. Now the number of characters matched are also 4 and the prefix value of this character is 2. Next position will be 6. So j will be set to 1 then j will be incremented then again it will be moved to 2, 3, 4, 5, 6. All the characters are matching so j will be 6. So as we have reached to this end of pattern we can say that we have found a pattern. So how we are doing it in KMP algorithm is that we are first calculating the prefix value and that prefix value will be used while shifting the window. So window will not be immediately shifted to right but it will be shifted to the number of positions depending on that prefix value. Thank you.