1596746160

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**
2. Add the next character to the string: **hash(txt[s:s+n-1])*radix + txt[n]**
```

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

1596737760

We have to search for a value ***x* **in a sorted matrix ** M**. If

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)**

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

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

```
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

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

1596746160

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**
2. Add the next character to the string: **hash(txt[s:s+n-1])*radix + txt[n]**
```

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

1603767600

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* 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

- 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.

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

- 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 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:

**Sequential Search:**In this, the list or array is traversed sequentially and every element is checked. For Example: Linear Search.**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

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-

- All students in the class.
- All numbers in a given list.

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