 1596746160

# Rabin-Karp algorithm for Pattern Searching in Matrix

Given matrices txt[][] of dimensions m1 x m2 and pattern pat[][] of dimensions n1 x n2, the task is to check whether a pattern exists in the matrix or not and if yes then print the top mot indices od the pat[][] in txt[][]. It is assumed that m1, m2 ≥ n1, n2

Examples:

``````Input:
txt[][] = {{G, H, I, P}
{J, K, L, Q}
{R, G, H, I}
{S, J, K, L}
}
pat[][] = {{G, H, I},
{J, K, L}
}
Output:
Pattern found at ( 0, 0 )
Pattern found at ( 2, 1 )
Explanation:

``````

Input:
txt[][] = { {A, B, C},
{D, E, F},
{G, H, I}
}
pat[][] = { {E, F},
{H, I}
}
Output:
Pattern found at (1, 1)

``````
**Approach:** In order to find a pattern in a 2-D array using [Rabin-Karp algorithm](https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/), consider an input matrix **txt[m1][m2]** and a pattern **pat[n1][n2]**. The idea is to find the [hash](https://www.geeksforgeeks.org/hashing-data-structure/) of each columns of **mat[][]** and **pat[][]** and compare the hash values. For any column if hash values are equals than check for the corresponding rows values. Below are the steps:

1.  Find the hash values of each column for the first **N1** rows in both **txt[][]** and **pat[][]** matrix.
2.  Apply Rabin-Karp Algorithm by finding hash values for the column hashes found in step 1.
3.  If a match is found compare **txt[][]** and **pat[][]** matrices for the specific rows and columns.
4.  Else slide down the column hashes by 1 row in the txt matrix using a [rolling hash](https://www.geeksforgeeks.org/string-hashing-using-polynomial-rolling-hash-function/).
5.  Repeat steps 2 to 4 for all the hash values and if we found any **pat[][]** match in **txt[][]** then print the indices of top most cell in the **txt[][]**.

**To find the hash value:** In order to find the hash value of a substring of size **N** in a text using rolling hash follow below steps:

1.  Remove the first character from the string: **hash(txt[s:s+n-1])-(radix**(n-1)*txt[s])%prime_number**

#greedy #hash #mathematical #matrix #searching #algorithms

## Buddha Community  1596737760

# Statement

We have to search for a value **x **in a sorted matrix M. If **exists, then return its coordinates **(i, j), else return (-1, -1). Let us consider the above matrix as an example. In this example, we are going to search for the value 12. Since 12 is present in the matrix, the algorithm should return its coordinates (2, 1)

# Simple Approach

A simple approach is to traverse all the values in the matrix and check if it is equal to 12. The worst case time complexity of the above algorithm will be

_O(n x m) = O(n²) __when _n = m

# Efficient Approach

The above algorithm behaves worse for large values of n and m. Let us look into the efficient algorithm now.

## Algorithm

``````1\. Start from Top Right position (0, m - 1) in the matrix M
2\. If the value is equal to x return (0, m - 1)
3\. Move one row down if the current value is less than x
4\. Move one column left if the current value is greater than x
``````

Let us apply this algorithm into our matrix M. We are going to search for the value 12 in M

#data-structures #search-sorted-matrix #2d-binary-search #algorithms #matrix-search #algorithms 1596746160

## Rabin-Karp algorithm for Pattern Searching in Matrix

Given matrices txt[][] of dimensions m1 x m2 and pattern pat[][] of dimensions n1 x n2, the task is to check whether a pattern exists in the matrix or not and if yes then print the top mot indices od the pat[][] in txt[][]. It is assumed that m1, m2 ≥ n1, n2

Examples:

``````Input:
txt[][] = {{G, H, I, P}
{J, K, L, Q}
{R, G, H, I}
{S, J, K, L}
}
pat[][] = {{G, H, I},
{J, K, L}
}
Output:
Pattern found at ( 0, 0 )
Pattern found at ( 2, 1 )
Explanation:

``````

Input:
txt[][] = { {A, B, C},
{D, E, F},
{G, H, I}
}
pat[][] = { {E, F},
{H, I}
}
Output:
Pattern found at (1, 1)

``````
**Approach:** In order to find a pattern in a 2-D array using [Rabin-Karp algorithm](https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/), consider an input matrix **txt[m1][m2]** and a pattern **pat[n1][n2]**. The idea is to find the [hash](https://www.geeksforgeeks.org/hashing-data-structure/) of each columns of **mat[][]** and **pat[][]** and compare the hash values. For any column if hash values are equals than check for the corresponding rows values. Below are the steps:

1.  Find the hash values of each column for the first **N1** rows in both **txt[][]** and **pat[][]** matrix.
2.  Apply Rabin-Karp Algorithm by finding hash values for the column hashes found in step 1.
3.  If a match is found compare **txt[][]** and **pat[][]** matrices for the specific rows and columns.
4.  Else slide down the column hashes by 1 row in the txt matrix using a [rolling hash](https://www.geeksforgeeks.org/string-hashing-using-polynomial-rolling-hash-function/).
5.  Repeat steps 2 to 4 for all the hash values and if we found any **pat[][]** match in **txt[][]** then print the indices of top most cell in the **txt[][]**.

**To find the hash value:** In order to find the hash value of a substring of size **N** in a text using rolling hash follow below steps:

1.  Remove the first character from the string: **hash(txt[s:s+n-1])-(radix**(n-1)*txt[s])%prime_number**

#greedy #hash #mathematical #matrix #searching #algorithms 1603767600

## Search Algorithms

Today, let us touch base on some fundamental concepts like search algorithms.

In simple terms, **searching **is a process of looking up a particular data record in the database or in the collection of items. A search typically answers as true or false whether the particular data in which we are referring is found or not and perform the next action accordingly.

Commonly used algorithms for search are:

• Linear search
• Binary search
• Interpolation search

Let us understand them in detail with some example

## Linear Search Algorithm

Linear Search Algorithm is the simplest and easiest form of the search algorithm. In this algorithm, a sequential search is made over all the items one by one to search for the targeted item. Each item is checked in sequence until the match is found. If the match is found, the searched item is returned otherwise the search continues till the end.

To make it easy to understand, let us see understand linear search using a flow diagram Linear Search — Data Flow

### Points to note:

• Does not need sorted array list
• Performs equality comparisons
• The time complexity is O(n)
• Time taken to search elements keeps increasing as the number of elements is increased.

## Binary Search Algorithm

In _Binary search algorithm, _begins with an interval covering the whole array and diving it in half. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

To make it easy to understand, let us see understand binary search using flow diagram and example as below. Binary Search — Data Flow

### Points to note:

• The array needs to be sorted
• Performs ordering comparisons
• Time complexity to O(log n).
• Search is done to either half of the given list, thus cut down your search to half time

#sorting-algorithms #algorithms #data-structures #search-and-sort #searching-algorithm 1597045745

## Searching Algorithms in Java

Searching Algorithms are designed to check for an element or retrieve an element from any data structure where it is stored. Based on the type of search operation, these algorithms are generally classified into two categories:

1. Sequential Search: In this, the list or array is traversed sequentially and every element is checked. For Example: Linear Search.
2. Interval Search: These algorithms are specifically designed for searching in sorted data-structures. These type of searching algorithms are much more efficient than Linear Search as they repeatedly target the center of the search structure and divide the search space in half. For Example: Binary Search.

Linear Search: The idea is to traverse the given array arr[] and find the index at which the element is present. Below are the steps:

• Let the element to be search be x.
• Start from the leftmost element of arr[] and one by one compare x with each element of arr[].
• If x matches with an element then return that index.
• If x doesn’t match with any of elements then return -1.

#java #searching #algorithms-searching #binary search 1598518020

## Let Us Understand Searching Algorithms

When searching for data, the difference between a fast application and a slower one lies in the accurate use of search algorithm. Searching algorithms is a basic, fundamental step in computing done via step-by-step method to locate a specific data among a collection of data.

All search algorithms make use of a search key in order to complete the procedure. And they are expected to return a success or a failure status ( in boolean true or false value).

In computer science, there are various type of search algorithms available and the way they are used decides the performance and efficiency of the data available( the manner in which the data is being used).

What is a Search Algorithm?

According to Wikipedia- Search algorithm is-

Any algorithm which solves the search problem, namely, to retrieve information stored within some data structure, or calculated in the search space of a problem domain, either with discrete or continuous values.

Searching Algorithms are designed to check or retrieve an element from any data structure where it is being stored.

They search for a target (key) in the search space, like-

1. All students in the class.
2. All numbers in a given list.

#data structure #algorithm skills #algorithms #searching algorithms #big data