-
-
Notifications
You must be signed in to change notification settings - Fork 156
pattern search algorithm added #2003 #2044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c78486f
pattern search algorithm added
dhairyagothi 3a3b0d4
Merge branch 'main' into main
ajay-dhangar 7c480cc
Update patternsearchalgorithm.md
dhairyagothi 72a1db2
Merge branch 'main' of https://github.com/dhairyagothi/codeharborhub.…
dhairyagothi 25b5f78
Merge branch 'main' into main
ajay-dhangar b73e993
Merge branch 'main' into main
ajay-dhangar 723019e
solved issue
ajay-dhangar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
id: patternsearching-in-dsa | ||
title: Pattern Searching Algorithm in Data Structures and Algorithms | ||
sidebar_label: Pattern Searching Algorithm | ||
sidebar_position: 1 | ||
description: "" | ||
tags: | ||
dhairyagothi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[dsa,data-algorithms , pattern-searching | ||
] | ||
--- | ||
|
||
|
||
The Pattern Searching Algorithm is a powerful tool used to find occurrences of a pattern within a larger text or string. It is commonly employed in string matching problems, where the goal is to search for a specific pattern or substring within a given text. | ||
|
||
 | ||
|
||
This algorithm compares the pattern with each possible substring of the text, starting from the leftmost position. When a match is found, the algorithm reports the index or position of the match. There are several variations of the Pattern Searching Algorithm, including the Naive Pattern Searching Algorithm, Knuth-Morris-Pratt (KMP) Algorithm, Rabin-Karp Algorithm, and Boyer-Moore Algorithm, each with its own advantages and disadvantages. | ||
|
||
### Advantages of the Pattern Searching Algorithm: | ||
|
||
1. Versatility: The algorithm can be applied to various string matching problems, making it a valuable tool in many applications. | ||
2. Flexibility: Different variations of the algorithm allow for different trade-offs in terms of time complexity, space complexity, and implementation complexity. | ||
3. Efficiency: Depending on the specific variation used, the Pattern Searching Algorithm can achieve efficient search times, particularly in large texts. | ||
|
||
### Disadvantages of the Pattern Searching Algorithm: | ||
|
||
1. Complexity: Some variations of the algorithm can be complex to understand and implement, especially for beginners. | ||
2. Performance: The performance of the algorithm can vary depending on the specific variation used and the characteristics of the input text and pattern. In some cases, the algorithm may not be the most efficient solution. | ||
|
||
Here's an example code problem to illustrate the usage of the Pattern Searching Algorithm: | ||
|
||
```python | ||
```python | ||
def pattern_search(text, pattern): | ||
result = [] | ||
text_length = len(text) | ||
pattern_length = len(pattern) | ||
|
||
for i in range(text_length - pattern_length + 1): | ||
j = 0 | ||
while j < pattern_length: | ||
if text[i + j] != pattern[j]: | ||
break | ||
j += 1 | ||
if j == pattern_length: | ||
result.append(i) | ||
|
||
return result | ||
|
||
text = "ABABDABACDABABCABAB" | ||
pattern = "ABABC" | ||
matches = pattern_search(text, pattern) | ||
print("Pattern found at positions:", matches) | ||
``` | ||
|
||
In this example, we have a text string "ABABDABACDABABCABAB" and we want to search for the pattern "ABABC" within it. The `patternSearch` function implements the Naive Pattern Searching Algorithm. It iterates through the text, comparing each substring of the same length as the pattern with the pattern itself. If a match is found, the starting position of the match is added to the result array. Finally, the function returns the array of positions where the pattern was found. | ||
|
||
When we run this code, it will output: "Pattern found at positions: [0, 10]". This means that the pattern "ABABC" was found at positions 0 and 10 within the text string. | ||
|
||
|
||
The pattern search algorithm, also known as the Naive Pattern Searching Algorithm, is a simple and straightforward approach to finding occurrences of a pattern within a text string. It works by iterating through the text and comparing each substring of the same length as the pattern with the pattern itself. | ||
|
||
The algorithm starts by initializing an empty array to store the positions where the pattern is found. Then, it iterates through the text string, comparing each substring of the same length as the pattern with the pattern itself. If a match is found, the starting position of the match is added to the result array. | ||
|
||
Once the algorithm has finished iterating through the entire text, it returns the array of positions where the pattern was found. This allows us to determine the locations of all occurrences of the pattern within the text. | ||
|
||
The Naive Pattern Searching Algorithm is relatively simple to implement and understand, making it a good starting point for pattern searching tasks. However, it has a time complexity of O((n-m+1)*m), where n is the length of the text and m is the length of the pattern. This means that the algorithm's performance can degrade for large texts or patterns. | ||
|
||
There are more advanced pattern searching algorithms, such as the Knuth-Morris-Pratt (KMP) algorithm and the Boyer-Moore algorithm, which offer better time complexity and improved performance in certain scenarios. However, the Naive Pattern Searching Algorithm serves as a foundation for understanding pattern searching concepts and can be a useful starting point for simple cases. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.