Z Algorithm for Pattern Searching in Data Structures - Scaler Topics (2024)

Overview

Z algorithm is a linear time string matching algorithm which runs in complexity. It is used to find all occurrence of a pattern in a string , which is common string searching problem.

Scope

  • This article tells about the working of Z algorithm.
  • Implementation of Z algorithm.
  • Complexity of Z algorithm.
  • Complexity of Z algorithm
    • Time complexity - O(m+n)

Abstract

Z algorithm is an algorithm for searching a given pattern in a string. It is an efficient algorithm as it has linear time complexity. It has a time complexity of O(m+n), where m is the length of the string and n is the length of the pattern to be searched.

Introduction to Z Algorithm

Ever wondered how to match a pattern in a given string, that too efficiently. For example, if you need to match a DNA sequence with a DNA pattern. DNA sequences have an average length of about 150 billion. Using a brute force string matching algorithm in such a case would lead to very high processing times as it has a quadratic time complexity, making it impractical to be used.

Well, the solution to this problem could be the Z algorithm as it is an efficient string matching algorithm. Read along to know more.

Z algorithm is a pattern searching algorithm. It means that it is used to search for occurrences of a given pattern in a string. It is an efficient algorithm as it has linear time complexity.

What is Z algorithm?

For applying Z algorithm, we require a string and a pattern that is to be searched.

As stated in the previous sections, Z algorithm is an algorithm used for finding occurrences of a pattern in a string. Now let's see how it works.

Z algorithm works by maintaining an auxiliary array called the Z array. This Z array stores the length of the longest substring, starting from the current index, that also it's prefix. This means that each index stores the number of characters, matching the starting characters, starting from this index. This implies that if Z array has a value k for any index, it means that k characters after this index match the first k characters of the string. This is the fundamental part of Z algorithm.

For using Z algorithm, we first concatenate the search pattern and the string to be searched together, adding another character in between, which is not present in any of the strings. Then we generate the Z array for this newly created string.

Then we scan the Z array and at each index where its value is equal to the length of the pattern to be searched, we say that the pattern has been found.

newString=pattern+'$'+string

where $ can be any character not present in either the string or the pattern.

Analysis of Z Algorithm

It's now time to analyse why and how the above-mentioned technique works. Let us first understand the use of the Z array. The Z array, at each index, stores the length of the longest substring starting from it, matching the prefix(starting characters) of the string. Now when we concatenate the pattern to our original string with an additional character, basically we are matching the prefix of the newly created string that is, our pattern, with the rest of the string, that is, our string to be searched.

As we have added an additional character not present in any of our strings, we are sure that a string longer than our pattern will never be found.

Let us understand it with the help of the below example.

Z Algorithm for Pattern Searching in Data Structures - Scaler Topics (1)

As we can see in the example, we have concatenated the pattern and the string with an additional character in between, not present in either of the strings. Now when we generate the Z array, we are basically searching for the first 4 characters of our string, which is also our pattern. As we have added an additional character, the largest pattern that we can find will be the pattern we need to search for. Now, let's analyse the Z array.

The first value of the Z array is explicitly set to zero. As we can see in the array at index 1, as 'a' matches with the character at index 0('a'), the value is set to 1. Then as none of the values are matching, they are set to zero. Now let's see the value at index 10. It is set to 4, as 4 characters starting from this index, match the prefix of the string(starting 4 characters).

Now, to find the occurrence of the pattern, we scan the Z array and we can say that the pattern is found wherever the Z value is equal to the length of the pattern. This is how Z algorithm works.

Working of Z Algorithm

Now let us understand the working of Z algorithm.

First let us understand why Z algorithm uses a window. Z algorithm speeds up its execution by using previous values from the Z array, and these values are used according to the current window. We check that is it possible to have a string greater than the current length inside the current window, and if it is not possible, we skip the calculation for the remaining values inside the window.

This speeds up the algorithm to a great extent as many elements are skipped. If this technique is not used, the algorithm would perform computations for all the elements, and thus get reduced to a quadratic [O(n2n^2n2)] algorithm, equivalent to naive pattern searching.

Now let us understand how Z algorithm uses a window to speed up its execution. Z algorithm maintains a window between two variables, left and right. While creating the Z array, work is done only inside this window and the window keeps updating. Now while we are inside the window, we find the current elements position inside the window, let it be k.

Then we check the number of elements after k in the window, and if it is greater than the value in the Z array at the kth index, the Z value at the current index becomes equal to the Z value at the kth index.

This is how the window in Z algorithm is used to speed up execution. The window basically uses the already filled values of the Z array.

Algorithm

Start z_algo(search_string,pattern) concatStr = concatenate pattern + “$” + text patLen = length of pattern n = length of concatStr  left = 0 and right = 0 for i = 1 to n, do if i > right, then left = i and right = i while right < n AND concatStr[right-left]=concatStr[right], do increase right by 1 done ZArray[i] = right – left decrease right by 1 else k = i – left if ZArray[k] < right – i +1, then ZArray[i] = ZArray[k] else left = i while right < n AND concatStr[right-left]=concatStr[right], do increase right by 1 done ZArray[i] = right – left decrease right by 1 for i = 0 to n – 1, do if ZArray[i] = patLen, then print the location i – patLen – 1 End

Z algorithm makes use of the previously scanned part of the string to speed up its execution. We maintain a window [left,right] of matched prefixes and update the window if a mismatch is found.

Steps for maintaining the window are -

  1. If i > right then there is no prefix substring that starts before i and ends after i, so we reset left and right and compute new [left,right] by comparing str[0..] to str[i..] and get ZArray[i] = (right-left+1).
  2. If i <= right then let K = i-left, now ZArray[i] >= min(Z[K], right-i+1) because str[i..] matches with str[K..] for atleast right-i+1 characters (they are in [left,right] interval which we know is a prefix substring).
  3. Now two sub cases arise –a) If ZArray[K] < right-i+1 then there is no prefix substring starting at str[i] (otherwise ZArray[K] would be larger) so ZArray[i] = ZArray[K] and interval [left,right] remains same.b) If ZArray[K] >= right-i+1 then it is possible to extend the [left,right] interval thus we will set left as i and start matching from str[right] onwards and get new right then we will update interval [left,right] and calculate ZArray[i] = (right-left+1).

Dry Run

Z Algorithm for Pattern Searching in Data Structures - Scaler Topics (2)

Lets take a sample String caabxaaab and search for the pattern aab.First thing we will do is to generate a new string by concatenating the old strings. So the new string becomes aab$caabxaaab. Now we will generate the Z array for the this string:

  • The value at index zero in the Z array is of no use so it can be left empty, so we start from index 1. Z_Array[1] will be equal to 1 as only one character a matches with the starting character a and b is a mismatch.
  • Z_Array[2] will be zero as b is a mismatch with the starting character.
  • Z_Array[3] and Z_Array[4] will also be equal to zero as both are mismatches.
  • At Z_Array[5] we find a match, so we compute it explicitly by searching till a mismatch is not found. Z_Array[5] comes out to be 3. So the window becomes [5,7].
  • Z_Array[6] is not calculated explicitly as it is inside the window and number of elements after it is greater than Z_Array[1]. So Z_Array[6] becomes equal to Z_Array[1] i.e. 1. Here we have used the previously calculated value.
  • Z_Array[7] is also not calculated and previous value of Z_Array[2] is used as value for Z_Array[7].
  • Now we come out of the previous window and a new window is created of size 1. Z_Array[8] is computed explicitly and comes out to be 1.
  • At Z_Array[9] we find a match, so we compute it explicitly by searching till a mismatch is not found. Z_Array[9] comes out to be 2. So the window becomes [9,11].
  • Z_Array[10] is inside the window but the Z value at its position in the window i.e. Z[1] is not less than the number of elements left in the window. So Z_Array[10] is calculated explicitly. It comes out to be 3. The window now becomes [10,12].
  • Z_Array[11] is not calculated explicitly as it is inside the window and number of elements after it is greater than Z_Array[1]. So Z_Array[11] becomes equal to Z_Array[1] i.e. 1. Here we have used the previously calculated value.
  • Z_Array[12] is also not calculated and previous value of Z_Array[2] is used as value for Z_Array[12].

Final Z array - _ 1 0 0 0 3 1 0 0 2 3 1 0

This is how the Z array is created. We then search for the index with value equal to length of the search pattern and our element is found at that index.

Time Complexity of Z Algorithm

The time complexity of Z algorithm is O(m+n), where n is the length of the string that is searched and m is the length of the pattern that is to be searched for.

It can be calculated as follows:Length of our newly created string is m+n.Traversing the string takes linear time that is = O(m+n)

Whenever a while loop is encountered, lets assume that k number of operations are performed but the next k iterations of the outer loops are skipped as the upper bound is increased by k every time.

So the total time taken for creating Z array remains O(m+n).

Time taken for searching m in the Z array also takes O(m+n) time.

So the total time complexity is:-

  • Total time taken for creating Z array remains O(m+n) + Time taken for searching m in the Z array also takes O(m+n) time

=O(m+n)+O(m+n)

=O(m+n)

Implementation of Z Algorithm

  1. Z algorithm in C/C++

#include<iostream>using namespace std; void create_Zarr(string str, int Z[]){ int n = str.length(); int left, right, k;  // [left,right] make a window which matches with prefix of str left = right = 0; for (int i = 1; i < n; ++i) { // if i>right nothing matches so we will calculate. // Z[i] using naive way. if (i > right) { left = right = i;  // right-left = 0 in starting, so it will start // checking from 0'th index.  while (right<n && str[right-left] == str[right]) right++; Z[i] = right-left; right--; } else { // k = i-left so k corresponds to number which // matches in [left,right] interval. k = i-left;  // if Z[k] is less than remaining interval // then Z[i] will be equal to Z[k].  if (Z[k] < right-i+1) Z[i] = Z[k];  else { // else start from right and check manually left = i; while (right<n && str[right-left] == str[right]) right++; Z[i] = right-left; right--; } } }}void find(string text, string pattern){ // Create concatenated string "P$T with additional character" string concat = pattern + "$" + text; int l = concat.length();  // Constructing Z array int Z[l]; create_Zarr(concat, Z);  // now looping through Z array for matching condition for (int i = 0; i < l; ++i) { // if Z[i] (matched region) is equal to pattern // length we got the pattern if (Z[i] == pattern.length()) cout << "Pattern found at index " << i - pattern.length() -1 << endl; }}// Driver programint main(){ string text = "faabbcdeffghiaaabbcdfgaabf"; string pattern = "aabb"; find(text, pattern); return 0;}

Output

Pattern found at index 1Pattern found at index 14

Explanation Of C++ code

The main function consists of our string and the pattern. Then the find is called with string and pattern as parameters. Now lets see what happens inside find function.

First thing the find function does is to create the string concat by concatenating pattern with string with an additional character $ in between. Now we pass this string concat to the create_Zarr function to create the Z array for this string. Now lets see how the Z array is created.

In the createZarr function, the first thing we do is create two variables called left and right to maintain the search window for creating the array.

Left and right are both initialized to zero. Now we start traversing the string and filling the Z array. The first value of the Z array is never used and can be left unassigned. Now we check if our current pointer i.

If it is greater than right that means we are currently out of the window, so we calculate the Z value at this index using the naive string matching that is by matching each character one by one until a mismatch is found. This value becomes our Z value for the current index.

Now if our current index is currently inside the window i.e., less than right, we calculate k which specifies our current index's position inside the window. If the Z value at kth index is less than right-i+1 i.e., the number of elements inside the window after k, the Z value at the current index becomes equal to the Z value at the kth index and the window will remain the same.

If the Z value at the kth index is greater than or equal to right-i+1, the current window can be extended. So we will set left=i and start matching the string again as done before. After matching the string, our Z value for the current index Z[i] will become equal to right-left.

This is how the Z array will be created for the concat string. Now to find the occurrence of pattern in string we search the Z array for a value equal to the length of pattern and if it is found at index i we can output that the pattern is found at index i-pattern_length-1(removing the extra length added to original string to create Z array).

This is how Z algorithm works to find all occurrences of a pattern in a string.

2. Z algorithm in JAVA

class z_algo {  // prints all occurrences of pattern in text using // Z algo public static void find(String text, String pattern) {  // Create concatenated string "P$T" String concat = pattern + "$" + text;  int l = concat.length();  int Z[] = new int[l];  // Construct Z array create_Zarr(concat, Z);  // now looping through Z array for matching condition for(int i = 0; i < l; ++i){  // if Z[i] (matched region) is equal to pattern // length we got the pattern  if(Z[i] == pattern.length()){ System.out.println("Pattern found at index " + (i - pattern.length() - 1)); } } }  // Fills Z array for given string str[] private static void create_Zarr(String str, int[] Z) {  int n = str.length();  // [left,right] make a window which matches with // prefix of s int left = 0, right = 0;  for(int i = 1; i < n; ++i) {  // if i>right nothing matches so we will calculate. // Z[i] using naive way. if(i > right){ left = right = i;  while(right < n && str.charAt(right - left) == str.charAt(right)) right++; Z[i] = right - left; right--; } else{  // k = i-left so k corresponds to number which // matches in [left,right] interval. int k = i - left;  if(Z[k] < right - i + 1) Z[i] = Z[k];  else{  // else start from right and check manually left = i; while(right < n && str.charAt(right - left) == str.charAt(right)) right++; Z[i] = right - left; right--; } } } }  // Driver program public static void main(String[] args) { String text = "faabbcdeffghiaaabbcdfgaabf"; String pattern = "aabb"; find(text, pattern); }}

Output

Pattern found at index 1Pattern found at index 14

3. Z Algorithm in Python

# Fills Z array for given string str[]def create_Zarr(string, z): n = len(string)  # [L,R] make a window which matches # with prefix of s left, right, k = 0, 0, 0 for i in range(1, n):  # if i>R nothing matches so we will calculate. # Z[i] using naive way. if i > right: left, right = i, i  # R-L = 0 in starting, so it will start # checking from 0'th index. while right < n and string[right - left] == string[right]: right += 1 z[i] = right - left right -= 1 else:  # k = i-L so k corresponds to number which # matches in [L,R] interval. k = i - left  # if Z[k] is less than remaining interval # then Z[i] will be equal to Z[k].  if z[k] < right - i + 1: z[i] = z[k]  else:  # else start from R and check manually left = i while right < n and string[right - left] == string[right]: right += 1 z[i] = right - left right -= 1 # prints all occurrences of pattern# in text using Z algodef find(text, pattern):  # Create concatenated string "P$T" concat = pattern + "$" + text left = len(concat)  # Construct Z array z = [0] * left create_Zarr(concat, z)  # now looping through Z array for matching condition for i in range(left):  # if Z[i] (matched region) is equal to pattern # length we got the pattern if z[i] == len(pattern): print("Pattern found at index", i - len(pattern) - 1) # Driver Codeif __name__ == "__main__": text = "faabbcdeffghiaaabbcdfgaabf" pattern = "aabb" find(text, pattern)

Output

Pattern found at index 1Pattern found at index 14

Examples of Z Algorithm

Z algorithm can be used to find any pattern in a string.

Let's look at some examples.

1. DNA sequence

Z algorithm can be used to find a DNA pattern in a DNA sequence. This application is important as DNA sequences are very large strings and thus an efficient algorithm is required for them.

In this example, we search a DNA sequence of length 100 for the pattern atgc.

Input

String=cgactgttatgggttcagtctcgttagtaaataatacaaaatgcccgttcacagctaaggttcatccgtgccgcggtaagtcccgttttcggcagcttcaPattern=atgc

Code

#include<iostream>using namespace std; void create_Zarr(string str, int Z[]){ int n = str.length(); int left, right, k;  // [left,right] make a window which matches with prefix of str left = right = 0; for (int i = 1; i < n; ++i) { // if i>right nothing matches so we will calculate. // Z[i] using naive way. if (i > right) { left = right = i;  // right-left = 0 in starting, so it will start // checking from 0'th index. For example, // for "ababab" and i = 1, the value of right // remains 0 and Z[i] becomes 0. For string // "aaaaaa" and i = 1, Z[i] and right become 5 while (right<n && str[right-left] == str[right]) right++; Z[i] = right-left; right--; } else { // k = i-left so k corresponds to number which // matches in [left,right] interval. k = i-left;  // if Z[k] is less than remaining interval // then Z[i] will be equal to Z[k]. // For example, str = "ababab", i = 3, right = 5 // and left = 2 if (Z[k] < right-i+1) Z[i] = Z[k];  // For example str = "aaaaaa" and i = 2, right is 5, // left is 0 else { // else start from right and check manually left = i; while (right<n && str[right-left] == str[right]) right++; Z[i] = right-left; right--; } } }}void find(string text, string pattern){ // Create concatenated string "P$T with additional character" string concat = pattern + "$" + text; int l = concat.length();  // Constructing Z array int Z[l]; create_Zarr(concat, Z);  // now looping through Z array for matching condition for (int i = 0; i < l; ++i) { // if Z[i] (matched region) is equal to pattern // length we got the pattern if (Z[i] == pattern.length()) cout << "Pattern found at index " << i - pattern.length() -1 << endl; }} // Fills Z array for given string str[] // Driver programint main(){ string text = "cgactgttatgggttcagtctcgttagtaaataatacaaaatgcccgttcacagctaaggttcatccgtgccgcggtaagtcccgttttcggcagcttca"; string pattern = "atgc"; find(text, pattern); return 0;}

Output

Pattern found at index 40

2. Word Search

Z Algorithm can also be used to find occurrences of a word in a sentence. In this example, we have found the occurrences of the word the in the sentence.

Input

String=the occurrence of the in this sentence can be found using the Z algoPattern=the

Code

#include<iostream>using namespace std; void create_Zarr(string str, int Z[]){ int n = str.length(); int left, right, k;  // [left,right] make a window which matches with prefix of str left = right = 0; for (int i = 1; i < n; ++i) { // if i>right nothing matches so we will calculate. // Z[i] using naive way. if (i > right) { left = right = i;  // right-left = 0 in starting, so it will start // checking from 0'th index. For example, // for "ababab" and i = 1, the value of right // remains 0 and Z[i] becomes 0. For string // "aaaaaa" and i = 1, Z[i] and right become 5 while (right<n && str[right-left] == str[right]) right++; Z[i] = right-left; right--; } else { // k = i-left so k corresponds to number which // matches in [left,right] interval. k = i-left;  // if Z[k] is less than remaining interval // then Z[i] will be equal to Z[k]. // For example, str = "ababab", i = 3, right = 5 // and left = 2 if (Z[k] < right-i+1) Z[i] = Z[k];  // For example str = "aaaaaa" and i = 2, right is 5, // left is 0 else { // else start from right and check manually left = i; while (right<n && str[right-left] == str[right]) right++; Z[i] = right-left; right--; } } }}void find(string text, string pattern){ // Create concatenated string "P$T with additional character" string concat = pattern + "$" + text; int l = concat.length();  // Constructing Z array int Z[l]; create_Zarr(concat, Z);  // now looping through Z array for matching condition for (int i = 0; i < l; ++i) { // if Z[i] (matched region) is equal to pattern // length we got the pattern if (Z[i] == pattern.length()) cout << "Pattern found at index " << i - pattern.length() -1 << endl; }} // Fills Z array for given string str[] // Driver programint main(){ string text = "the occurence of the in this sentence can be found using the Z algo"; string pattern = "the"; find(text, pattern); return 0;}

Output

Pattern found at index 0Pattern found at index 17Pattern found at index 57

Applications of Z Algorithm

  1. Z Algorithm has an important application in finding DNA patterns in a DNA sequence. It is used in this case because Z algorithm works in linear time and as DNA sequences are very large, Z algorithm works efficiently.
  2. Z algorithm can also be used to find all occurrences of a word in a sentence.
  3. Z algorithm can be used anywhere to find a pattern in a string.

Z Algortihm vs Others

String matching algorithms comparable to Z algorithm are Rabin-Karp algorithm, KMP algorithm, naive string matching.

Naive String searching algorithm matches the patterns checking it at each and every index whereas Rabin Karp follows a similar approach but it uses a hash function to match the pattern.

KMP algorithm follows a similar approach to Z algorithm but it uses an auxiliary array that stores the longest length of the proper prefix of the pattern that is also a suffix of the pattern.

Time Complexities of String Searching Algorithms

No.AlgorithmBest CaseAverage CaseWorst Case
1Naive String SearchingO(n)O((n-m+1)*m)O(n*m)
2Rabin KarpO(n+m)O(n+m)O(n*m)
3KMPO(n+m)O(n+m)O(n+m)
4Z AlgorithmO(n+m)O(n+m)O(n+m)

Z Algorithm for Pattern Searching in Data Structures - Scaler Topics (3)

Here n is the length of the String in which the pattern is to be searched and m is the length of the pattern that is to be searched.

In the above tables, we can see that naive string matching and Rabin Karp algorithms have very high time complexities and thus their use should be avoided for big inputs.

KMP algorithm and Z algorithm have similar time complexities and can be used interchangeably but the use of Z algorithm should be preferred as it is easier to code and understand and even debugging the Z array is easier than debugging the auxiliary array in KMP.

Conclusion

  • In this article, first, we have seen the basic working of Z algorithm.
  • After that we came to an example to better understand the working of Z algorithm.
  • Then we have also studied how to write code for Z algorithm with the help of the pseudocode/algorithm.
  • That is followed by a time complexity analysis of Z algorithm.
  • After that we have also implemented Z algorithm in C++ with explanation and have also implemented Z algorithm in JAVA and Python.
  • Finally discussed applications and examples of Z algorithm and also compared Z algorithm to algorithms similar to it.
Z Algorithm for Pattern Searching in Data Structures - Scaler Topics (2024)

FAQs

Which algorithm is best for pattern searching? ›

The so-called naive or brute force algorithm is the most intuitive approach to the string pattern-matching problem. This algorithm attempts simply to match the pattern in the target at successive positions from left to right.

What is Z algorithm? ›

Z algorithm is a linear time string matching algorithm which runs in complexity. It is used to find all occurrence of a pattern in a string , which is common string searching problem.

What are the topics in DSA? ›

Syllabus:
  • Basic Data Structures: Arrays, Strings, Stacks, Queues.
  • Asymptotic analysis (Big-O notation)
  • Basic math operations (addition, subtraction, multiplication, division, exponentiation)
  • Sqrt(n) primality testing.
  • Euclid's GCD Algorithm.
  • Basic Recursion.
  • Greedy Algorithms.
  • Basic Dynamic Programming.

What is called pattern searching? ›

Pattern Searching algorithms are used to find a pattern or substring from another bigger string. There are different algorithms. The main goal to design these type of algorithms to reduce the time complexity. The traditional approach may take lots of time to complete the pattern searching task for a longer text.

What is the time complexity of Z algorithm for pattern searching? ›

Explanation: Z algorithm is an efficient pattern searching algorithm as it searches the pattern in linear time. It has a time complexity of O(m + n) where m is the length of text and n is the length of the pattern.

What is pattern matching algorithm in data structure? ›

Pattern matching algorithms are the algorithms that are used to figure out whether a specific string pattern occurs in a string text. Two of the most widely used pattern matching algorithms are the Naive Algorithm for pattern matching and the pattern matching algorithm using finite automata.

What are the 4 types of algorithm? ›

Introduction To Types of Algorithms

Brute Force algorithm. Greedy algorithm. Recursive algorithm. Backtracking algorithm.

Can I learn DSA in 4 months? ›

DSA requires a significant investment of time and effort. It can take you anywhere from 4-8 months to truly master it.

What are the 4 data structures? ›

Popular linear data structures are:
  • Array Data Structure. In an array, elements in memory are arranged in continuous memory. ...
  • Stack Data Structure. In stack data structure, elements are stored in the LIFO principle. ...
  • Queue Data Structure. ...
  • Linked List Data Structure.

What are the 2 main types of data structures? ›

Linear Data Structures. Non-Linear Data Structures.

What is the most important topic in data structure? ›

What are the important topics in data structures and algorithms? Basic Data Structures: Arrays, Strings, Stacks, Queues.

What are the 3 main concepts of structures programming? ›

Surprisingly, it can often be broken down into three simple programming structures called sequences, selections, and loops. These come together to form the most basic instructions and algorithms for all types of software.

What are the 3 components of the pattern recognition? ›

There are three main types of pattern recognition, dependent on the mechanism used for classifying the input data. Those types are: statistical, structural (or syntactic), and neural.

What is the main importance of the patterns? ›

Patterns provide a sense of order in what might otherwise appear chaotic. Researchers have found that understanding and being able to identify recurring patterns allow us to make educated guesses, assumptions, and hypothesis; it helps us develop important skills of critical thinking and logic.

What is the main purpose of pattern? ›

Pattern is an underlying structure that organizes surfaces or structures in a consistent, regular manner. Pattern can be described as a repeating unit of shape or form, but it can also be thought of as the "skeleton" that organizes the parts of a composition.

What are the types of searching? ›

It is commonly accepted that there are three different types of search queries: Navigational search queries. Informational search queries. Transactional search queries.

What are different types of searching technique? ›

Searching Algorithms :
  • Linear Search.
  • Binary Search.
  • Jump Search.
  • Interpolation Search.
  • Exponential Search.
  • Sublist Search (Search a linked list in another list)
  • Fibonacci Search.
  • The Ubiquitous Binary Search.
22 Nov 2022

Which operator is used to search patterned data? ›

LIKE operator is used for pattern matching, and it can be used as -. % – It matches zero or more characters.

How do you find Z example? ›

The Z Score Formula: One Sample

The test has a mean (μ) of 150 and a standard deviation (σ) of 25. Assuming a normal distribution, your z score would be: z = (x – μ) / σ = (190 – 150) / 25 = 1.6.

How do you find Z probability? ›

The Z-score formula is z = x − μ σ .

Which searching algorithm has best time complexity? ›

The Time Complexity of Binary Search: The Time Complexity of Binary Search has the best case defined by Ω(1) and the worst case defined by O(log n). Binary Search is the faster of the two searching algorithms.

What is the complexity of sieve? ›

Algorithmic complexity

The sieve of Eratosthenes is a popular way to benchmark computer performance. The time complexity of calculating all primes below n in the random access machine model is O(n log log n) operations, a direct consequence of the fact that the prime harmonic series asymptotically approaches log log n.

Which algorithm has O n time complexity? ›

Time Complexities of all Sorting Algorithms
AlgorithmTime Complexity
BestWorst
Insertion SortΩ(n)O(n^2)
Heap SortΩ(n log(n))O(n log(n))
Quick Sort
2 more rows
22 Sept 2022

What are the 2 main characters used for matching the pattern? ›

Answer. Answer: In SQL, the LIKE keyword is used to search for patterns. Pattern matching employs wildcard characters to match different combinations of characters.

What is pattern algorithm? ›

Pattern recognition algorithms generally aim to provide a reasonable answer for all possible inputs and to perform "most likely" matching of the inputs, taking into account their statistical variation. This is opposed to pattern matching algorithms, which look for exact matches in the input with pre-existing patterns.

What is pattern matching explain with example? ›

What Does Pattern Matching Mean? Pattern matching in computer science is the checking and locating of specific sequences of data of some pattern among raw data or a sequence of tokens. Unlike pattern recognition, the match has to be exact in the case of pattern matching.

How many algorithms does Z have? ›

There are 169 algorithms for ZZ-B if you do phasing intuitively and 227 if you do it algorithmically. Can be used as a transition to full ZBLL. Using algorithms for phasing is called ZZ-B+. ZZ-R- This is where you phase edges during last slot, then do OCLL/PLL.

What are 5 examples of algorithms? ›

Examples of Algorithms in Everyday Life
  • Tying Your Shoes.
  • Following a Recipe.
  • Classifying Objects.
  • Bedtime Routines.
  • Finding a Library Book in the Library.
  • Driving to or from Somewhere.
  • Deciding What to Eat.
18 Aug 2022

What are 3 examples of algorithms? ›

Here are some more algorithms we can explore on our own to further our knowledge.
  • Quicksort.
  • Traverse a binary search tree.
  • Minimum spanning tree.
  • Heapsort.
  • Reverse a string in place.

In which language DSA is easy? ›

Python – Python is extremely popular among programmers and data scientists due to its ease of use and adaptability. Python is a beginner-friendly language with a simple learning curve and English-like syntax.

Is OOP required for DSA? ›

No,OOP concepts are not required for competitive programming.

Which is the most difficult topic in DSA? ›

The hardest part is to map a “new” problem to a known data structure or algorithm that you thought you clearly understood.
...
Data structures:
  • Array.
  • Linked list.
  • Stack.
  • Queue.
  • Binary search tree (not necessarily balanced)
  • Balanced binary search tree.
  • Hash table.

Which data structure is best? ›

An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays.

What are basics of data structure? ›

A data structure is a specialized format for organizing, processing, retrieving and storing data. There are several basic and advanced types of data structures, all designed to arrange data to suit a specific purpose. Data structures make it easy for users to access and work with the data they need in appropriate ways.

What are the 6 operations on data structures? ›

➢ The possible operations on the linear data structure are: Traversal, Insertion, Deletion, Searching, Sorting and Merging.

What are the 3 characteristics of data structure? ›

Characteristics of a data structure

There are three main characteristics: Correctness. Time complexity. Space complexity.

What are the four applications of data structure? ›

Data structures, including linked lists for memory allocation, file directory management and file structure trees, as well as process scheduling queues, are used to allow core operating system (OS) resources and functions.

Which algorithm is the best? ›

Top 10 Machine Learning Algorithms in 2022
  1. Linear regression. ...
  2. Logistic regression. ...
  3. Decision trees. ...
  4. Support vector machines (SVMs) ...
  5. Naive Bayes algorithm. ...
  6. KNN classification algorithm. ...
  7. K-Means. ...
  8. Random forest algorithm.
30 May 2022

How many algorithms are there in data structure? ›

7 algorithms and data structures every programmer must know.

How many algorithms are there? ›

There are seven different types of programming algorithms: Sort algorithms. Search algorithms. Hashing.

What are the features of algorithm? ›

Characteristics of an Algorithm
  • Input: An algorithm requires some input values. ...
  • Output: At the end of an algorithm, you will have one or more outcomes.
  • Unambiguity: A perfect algorithm is defined as unambiguous, which means that its instructions should be clear and straightforward.
18 Nov 2022

Which searching method is the most efficient? ›

Binary search method is considered as the best searching algorithms. There are other search algorithms such as the depth-first search algorithm, breadth-first algorithm, etc. The efficiency of a search algorithm is measured by the number of times a comparison of the search key is done in the worst case.

Which of the following algorithms are used for searching and pattern matching problems? ›

Rabin Karp Algorithm: It matches the hash value of the pattern with the hash value of current substring of text, and if the hash values match then only it starts matching individual characters.

Which searching algorithm is faster? ›

According to a simulation conducted by researchers, it is known that Binary search is commonly the fastest searching algorithm. A binary search is performed for the ordered list. This idea makes everything make sense that we can compare each element in a list systematically.

Which pattern designing method is fastest and most efficient? ›

If you want to develop a standard pattern, flat pattern making is the fastest and the most efficient method. The previously developed patterns is what this method is dependant on. Flat pattern making manipulates patterns using slash.

Which search is faster and why? ›

Binary search is widely used and one of the fastest search algorithms. It works based on the divide and search principle. The data set must be sorted in increasing or decreasing order before providing it as input to the binary search algorithm.

How many types of searching are there? ›

In searching, there are two types: sequential search and interval search. Almost every search algorithm falls into one of these two categories. Linear and binary searches are two simple and easy-to-implement algorithms, with binary algorithms performing faster than linear algorithms.

What is best algorithm? ›

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.

What are the 2 types of searching algorithms? ›

There are many different types of searching algorithms. Two of them are serial search and binary search.

Which query is used for pattern matching? ›

LIKE clause is used to perform the pattern matching task in SQL. A WHERE clause is generally preceded by a LIKE clause in an SQL query. LIKE clause searches for a match between the patterns in a query with the pattern in the values present in an SQL table.

Which operators is used for pattern matching? ›

LIKE operator is used for pattern matching, and it can be used as -. % – It matches zero or more characters.

Which shortest algorithm is fastest? ›

The Shortest Path Faster Algorithm (SPFA) is an improvement of the Bellman–Ford algorithm which computes single-source shortest paths in a weighted directed graph. The algorithm is believed to work well on random sparse graphs and is particularly suitable for graphs that contain negative-weight edges.

Who invented searching algorithm? ›

Incidentally, Bharat was the person behind Google News. 8- Singhal famously re-wrote the original Google algorithm that was created earlier by Larry Page. He reportedly changed it completely to suit the existing challenges.

What are searching techniques? ›

The search techniques introduced on this webpage are: Boolean operators, phrase searching, truncation/wildcards, and nesting.

What is 2 types of pattern making? ›

Methods of Pattern Making
  • Drafting.
  • Draping.
  • Flat paper patternmaking.

What are the 4 main types of pattern used in design? ›

The 4 types of pattern repeats are:

Full drop. Half drop. Mirror. Continuous.

Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6184

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.