Skip to content

Commit

Permalink
Merge pull request #1371 from pratheekv39/feature/colussi
Browse files Browse the repository at this point in the history
#1308 Added Colussi Algorithm in String Algorithms
  • Loading branch information
pankaj-bind authored Oct 29, 2024
2 parents 39441b2 + c6579ca commit 85b21b8
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
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.

0 comments on commit 85b21b8

Please sign in to comment.