Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

String Processing Assignment 4: String Matching Algorithms

This solution implements two advanced string matching algorithms with a complete interactive program that demonstrates their functionality and performance.

Assignment Overview

Implementation of two string matching algorithms: Boyer-Moore and Rabin-Karp, with an interactive menu system, pattern search, and performance comparison.

Compilation

make              # Using Makefile
# OR
g++ -std=c++17 -o string_matching string_matching.cpp

Running the Program

./string_matching

Features Implemented

Algorithms

  • Boyer-Moore Algorithm - Right-to-left pattern matching with bad character rule
  • Rabin-Karp Algorithm - Hash-based pattern matching with rolling hash

Interactive Menu

  1. Load Text File
  2. Enter Text Manually
  3. Search Using Boyer-Moore
  4. Search Using Rabin-Karp
  5. Compare Algorithms
  6. Exit

Key Features

  • Pattern highlighting with brackets: [PATTERN]
  • Overlapping pattern detection
  • Performance statistics (comparisons, execution time, match count)
  • Algorithm comparison with performance metrics
  • File I/O operations

Files

  • string_matching.cpp - Complete implementation
  • input.txt - Sample test data
  • Makefile - Build automation
  • Assignment_4.pdf - Assignment specification

Usage Examples

Example 1: Load file and search with Boyer-Moore

Menu Selection: 1
Enter filename: input.txt
Menu Selection: 3
Enter pattern: DATA

Example 2: Manual text entry and Rabin-Karp search

Menu Selection: 2
Enter text: AAAAAA
Menu Selection: 4
Enter pattern: AAA

Example 3: Algorithm comparison

Menu Selection: 1
Enter filename: input.txt
Menu Selection: 5
Enter pattern: STRUCTURE

Test Cases

The program has been tested with the following test cases from the assignment:

Text Pattern Expected Result Status
AAAAAAAAA AAA Multiple overlapping matches
DATA STRUCTURE COURSE STRUCTURE Match found
HELLO WORLD TEST TEST Match found
MISSISSIPPI ISSI Multiple matches
Text DATA Multiple matches

Test Result: Overlapping Pattern Detection

For text "AAAAAA" with pattern "AAA":

  • Boyer-Moore found: 5 matches at indices [184, 185, 186, 187, 212]
  • Rabin-Karp found: 5 matches at indices [184, 185, 186, 187, 212]
  • ✅ Overlapping detection working correctly

Output Features

Pattern Highlighting

The program highlights found patterns in square brackets:

Input:  DATA STRUCTURE COURSE
Output: [DATA] [STRUCTURE] COURSE

Statistics Display

After each search, the program displays:

  • Algorithm used
  • Pattern searched
  • Number of comparisons made
  • Number of matches found
  • Execution time in milliseconds

Example:

========== SEARCH STATISTICS ==========
Algorithm: Boyer-Moore
Pattern: DATA
Number of Comparisons: 107
Number of Matches: 5
Execution Time: 0.016461 ms
======================================

Algorithm Comparison

========== ALGORITHM COMPARISON ==========
Pattern: STRUCTURE
Text length: 223 characters

Boyer-Moore Algorithm:
  Comparisons: 76
  Matches: 4
  Execution Time: 0.435985 ms

Rabin-Karp Algorithm:
  Comparisons: 254
  Matches: 4
  Execution Time: 0.008248 ms

Winner (Fewer Comparisons): Boyer-Moore (76 vs 254)
Winner (Faster Execution): Rabin-Karp (0.008248ms vs 0.435985ms)
=========================================

Algorithm Complexity Analysis

Boyer-Moore

  • Time Complexity: O(n/m) best case, O(nm) worst case
  • Space Complexity: O(1)
  • Key Advantage: Skips large portions of text, especially with long patterns

Rabin-Karp

  • Time Complexity: O(n+m) average case, O(nm) worst case
  • Space Complexity: O(1)
  • Key Advantage: Efficient for multiple pattern searching in one pass

Input File Format

The input.txt file contains sample test data:

DATA STRUCTURE AND ALGORITHMS COURSE
THIS COURSE TEACHES DATA HANDLING AND TREE STRUCTURES
RABIN KARP AND BOYER MOORE ARE STRING MATCHING ALGORITHMS
DATA DATA DATA STRUCTURE STRUCTURE
AAAAAA TEST FOR OVERLAPPING AAA PATTERN

Technical Details

Boyer-Moore Implementation

  • Builds bad character table for shifting optimization
  • Compares pattern from right to left
  • Dynamically adjusts shift amount based on mismatches
  • Properly handles overlapping patterns

Rabin-Karp Implementation

  • Uses prime number (101) for hash computation
  • Implements rolling hash for efficient window updates
  • Verifies character-by-character after hash match
  • Counts comparisons for performance analysis

Assignment Compliance

All Requirements Met:

  • Interactive menu system (6 options)
  • Boyer-Moore algorithm implementation
  • Rabin-Karp algorithm implementation
  • Multiple pattern search capability
  • Pattern highlighting
  • Overlapping pattern detection
  • File processing
  • Comprehensive statistics
  • Algorithm comparison
  • All required functions implemented
  • Test cases verified
  • Proper output formatting

Files Included

  1. string_matching.cpp (700+ lines)

    • Complete implementation with both algorithms
    • All required functions
    • Menu system and I/O handling
  2. input.txt

    • Sample test data for demonstration
    • Contains various test patterns
  3. run_tests.sh

    • Automated test script
    • Tests all main features

Building for Submission

The program is ready for submission with:

  • ✅ Complete source code
  • ✅ Compiled executable
  • ✅ Test input file
  • ✅ Test automation script
  • ✅ Comprehensive documentation
  • ✅ All requirements verified

Notes

  • The program uses C++17 features
  • Cross-platform compatible (Windows, Linux, macOS)
  • No external dependencies required
  • Clean, well-documented code
  • Proper error handling for edge cases

About

This repo serves as a codespace for an applied data structure assignement.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages