-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1729 from pratheekv39/feature/Maximal
#1690 Added Maximal Shift Algorithm
- Loading branch information
Showing
2 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,67 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
// Function to compute the maximal shift array | ||
void computeMaximalShiftArray(char* pattern, int m, int* shift) { | ||
int i = 1, j = 0; | ||
shift[0] = 0; // Shift[0] is always 0 | ||
|
||
// Preprocess the pattern to create the maximal shift array | ||
while (i < m) { | ||
if (pattern[i] == pattern[j]) { | ||
j++; | ||
shift[i] = j; | ||
i++; | ||
} else { | ||
if (j != 0) { | ||
j = shift[j - 1]; | ||
} else { | ||
shift[i] = 0; | ||
i++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Function to implement the Maximal Shift Algorithm | ||
void maximalShiftSearch(char* text, char* pattern) { | ||
int n = strlen(text); // Length of text | ||
int m = strlen(pattern); // Length of pattern | ||
|
||
// Create the maximal shift array for the pattern | ||
int shift[m]; | ||
computeMaximalShiftArray(pattern, m, shift); | ||
|
||
int i = 0; // Index for text | ||
int j = 0; // Index for pattern | ||
|
||
while (i < n) { | ||
if (pattern[j] == text[i]) { | ||
j++; | ||
i++; | ||
} | ||
|
||
if (j == m) { | ||
printf("Pattern found at index %d\n", i - j); | ||
j = shift[j - 1]; // Get the next shift position | ||
} else if (i < n && pattern[j] != text[i]) { | ||
if (j != 0) { | ||
j = shift[j - 1]; // Move j back to a possible match position | ||
} else { | ||
i++; // Move to the next character in text | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
char text[] = "maximalshiftalgorithmstringmatching"; | ||
char pattern[] = "algorithm"; | ||
|
||
printf("Text: %s\n", text); | ||
printf("Pattern: %s\n", pattern); | ||
|
||
maximalShiftSearch(text, pattern); | ||
|
||
return 0; | ||
} |
This file contains 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,94 @@ | ||
# **Maximal Shift Algorithm** | ||
|
||
## **Description** | ||
|
||
The **Maximal Shift Algorithm** is a string matching algorithm used to efficiently find all occurrences of a pattern in a given text. This algorithm leverages a preprocessing step where it computes a **shift array**, allowing the search to skip over sections of the text during mismatches. It is similar to the Knuth-Morris-Pratt (KMP) algorithm but is optimized for cases where large shifts are required. The **Maximal Shift Algorithm** runs in linear time \( O(n + m) \), where \( n \) is the length of the text and \( m \) is the length of the pattern. | ||
|
||
### **Problem Definition** | ||
|
||
Given: | ||
- A **text** string and a **pattern** string. | ||
|
||
Objective: | ||
- Efficiently find all occurrences of the **pattern** within the **text** using the **Maximal Shift Algorithm**. | ||
|
||
### **Algorithm Overview** | ||
|
||
1. **Compute the Maximal Shift Array**: | ||
- Precompute a shift array for the pattern, where each position indicates how far the algorithm can safely shift the pattern without rechecking previously matched characters. | ||
|
||
2. **Pattern Search**: | ||
- Traverse through the text and compare it with the pattern. | ||
- If a mismatch occurs, use the precomputed **shift array** to skip over unnecessary comparisons, effectively shifting the pattern forward. | ||
|
||
3. **Optimal Shifting**: | ||
- The algorithm ensures that no character in the text is checked more than once, leading to an optimal \( O(n + m) \) time complexity. | ||
|
||
### **Time Complexity** | ||
|
||
- **Time Complexity**: \( O(n + m) \), where: | ||
- \( n \) is the length of the **text**. | ||
- \( m \) is the length of the **pattern**. | ||
|
||
- **Space Complexity**: \( O(m) \) for storing the shift array. | ||
|
||
## **How to Run the Program** | ||
|
||
### **Prerequisites** | ||
|
||
- A C compiler such as GCC. | ||
|
||
### **Steps to Compile and Execute** | ||
|
||
1. **Save the Program**: | ||
Save the provided C code as `Program.c`. | ||
|
||
2. **Compile the Program**: | ||
Open your terminal and compile the program using the following command: | ||
```bash | ||
gcc Program.c -o maximal_shift | ||
``` | ||
|
||
3. **Run the Program**: | ||
After compilation, execute the program using: | ||
```bash | ||
./maximal_shift | ||
``` | ||
|
||
4. **Example Output**: | ||
Given the input: | ||
```plaintext | ||
Text: "maximalshiftalgorithmstringmatching" | ||
Pattern: "algorithm" | ||
``` | ||
The output will be: | ||
```plaintext | ||
Pattern found at index 12 | ||
``` | ||
|
||
## **Usage** | ||
|
||
The **Maximal Shift Algorithm** is suitable for applications where pattern matching is required, such as: | ||
|
||
- **Text search engines** | ||
- **Pattern detection in large datasets** | ||
- **Plagiarism detection** | ||
- **Bioinformatics** (e.g., DNA sequence alignment) | ||
|
||
## **Code Overview** | ||
|
||
### **Functions**: | ||
|
||
- **`computeMaximalShiftArray()`**: | ||
Computes the **Maximal Shift Array** for the pattern, allowing the algorithm to efficiently skip sections of the text during mismatches. | ||
|
||
- **`maximalShiftSearch()`**: | ||
Implements the search process using the **Maximal Shift Algorithm**, comparing the pattern against the text. | ||
|
||
### **Input and Output**: | ||
|
||
- **Input**: | ||
A **text** string and a **pattern** string to search for. | ||
|
||
- **Output**: | ||
Prints the starting index of each occurrence of the pattern in the text. |