Skip to content
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

#1308 Added Colussi Algorithm in String Algorithms #1371

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions String Algorithms/Colussi Algorithm/Program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <string.h>

#define MAX_PATTERN_LENGTH 100

// Function to compute the prefix function for the pattern
void computePrefixFunction(char* pattern, int m, int* pi) {
int i = 1, j = 0;
pi[0] = 0;

while (i < m) {
if (pattern[i] == pattern[j]) {
pi[i] = j + 1;
i++;
j++;
}
else if (j > 0) {
j = pi[j - 1];
}
else {
pi[i] = 0;
i++;
}
}
}

// Function to perform string matching using Colussi algorithm
void colussi(char* text, char* pattern) {
int n = strlen(text);
int m = strlen(pattern);
int pi[MAX_PATTERN_LENGTH];

// Compute the prefix function for the pattern
computePrefixFunction(pattern, m, pi);

int i = 0, j = 0;
while (i < n) {
if (text[i] == pattern[j]) {
i++;
j++;
if (j == m) {
printf("Pattern found at index %d\n", i - j);
j = pi[j - 1];
}
}
else {
if (j > 0) {
j = pi[j - 1];
}
else {
i++;
}
}
}
}

int main() {
char text[] = "ABABDABACDABABCABAB";
char pattern[] = "ABABCABAB";

printf("Text: %s\n", text);
printf("Pattern: %s\n\n", pattern);

printf("Matches:\n");
colussi(text, pattern);

return 0;
}
52 changes: 52 additions & 0 deletions String Algorithms/Colussi Algorithm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Colussi Algorithm

## Description

The Colussi algorithm, also known as the KMP-Colussi algorithm, is a string matching algorithm that combines ideas from the Knuth-Morris-Pratt (KMP) algorithm and the Boyer-Moore algorithm. It was introduced by Livio Colussi in 1991. The algorithm achieves linear time complexity for string matching by utilizing the prefix function and a shifting heuristic.

### Problem Definition

Given:
- A text string T of length n
- A pattern string P of length m

Objective:
- Find all occurrences of pattern P in text T

### Algorithm Overview

1. **Preprocessing**:
- Compute the prefix function for the pattern string
- The prefix function helps in determining the proper shift when a mismatch occurs

2. **Searching**:
- Scan the text from left to right
- Compare characters of the text and pattern
- If a match is found, continue comparing until either the end of the pattern is reached (indicating a complete match) or a mismatch occurs
- In case of a mismatch, use the prefix function to determine the appropriate shift

### Time Complexity

- Preprocessing: O(m), where m is the length of the pattern
- Searching: O(n), where n is the length of the text
- Overall time complexity: O(n + m)

### Space Complexity

O(m), where m is the length of the pattern (for storing the prefix function)

## Implementation

The implementation in C demonstrates the Colussi algorithm for string matching. It includes:

1. Function to compute the prefix function for the pattern
2. The main string matching function implementing the Colussi algorithm
3. A demonstration of the algorithm's usage

## Usage

Compile the program and run it. The example in the main function demonstrates how to use the Colussi algorithm to find occurrences of a pattern string in a given text.

## Note

The Colussi algorithm combines the ideas of the KMP algorithm and the Boyer-Moore algorithm to achieve efficient string matching. It utilizes the prefix function for determining proper shifts and provides linear time complexity for searching patterns in a text.