kanwei/algorithms

repository·master·Indexed 25 days ago

https://github.com/kanwei/algorithms

A Ruby library providing standard data structures and algorithms not present in the Ruby standard library. It includes containers such as Heaps, Priority Queues, Stacks, Queues, Deques, Red-Black Trees, Splay Trees, Tries, Suffix Arrays, and KD Trees. It also provides search algorithms (binary search, KMP), sorting algorithms (bubble, comb, selection, heapsort, insertion, shell, quicksort, mergesort, and dualpivotquicksort), and string operations like Levenshtein distance.

Tokens
6.5K
Snippets
37
Records
67
Agent score
74%

What's inside kanwei-algorithms

  1. Install and use the algorithms library

    master

    To use the algorithms library, require rubygems and algorithms. The library provides various data structures under the Containers namespace and algorithms under the Algorithms namespace. For better performance, it is highly recommended to install the C extensions.

    require 'rubygems'
    require 'algorithms'
    
    # Usage example
    max_heap = Containers::MaxHeap.new
  2. Use Containers::Trie for key-value mapping

    master

    A Containers::Trie is a Ternary Search Tree implementation that stores key-value pairs. It provides $O(m)$ lookup speed (where $m$ is the length of the key) and avoids collisions. It is suitable for longest prefix matching and wildcard searches.

    t = Containers::Trie.new
    t["hello"] = "world"
    puts t["hello"] #=> "world"
  3. Use Containers::SplayTreeMap for ordered key-value storage

    master

    A SplayTreeMap is a map that stores items in ascending order of their keys (using the <=> operator). It is self-optimizing: recently accessed nodes are moved near the root for faster subsequent access.

    Key characteristics:

    • Ordering: Keys are stored in order, allowing for ordered iteration.
    • Complexity: Most methods have amortized $O(\log n)$ performance, though worst-case is $O(n)$ if keys are added in sorted order.
    • Duplicates: Duplicate keys are not allowed; inserting a new value for an existing key overwrites the old value.
  4. Use Containers::RBTreeMap for sorted key-value storage

    master

    A Containers::RBTreeMap is a map that stores items in sorted order based on their keys (using the <=> operator). Unlike a standard Hash, keys can be iterated over in order. Duplicate keys are not allowed; inserting a duplicate key will overwrite the existing value.

    Containers::RBTreeMap automatically selects the faster C implementation (Containers::CRBTreeMap) if available, otherwise it falls back to the Ruby implementation (Containers::RubyRBTreeMap).

  5. Include the Containers module to simplify container initialization

    master

    By default, all container classes are namespaced under the Containers module. To avoid prefixing every initialization with Containers::, you can include Containers in your scope.

    require 'algorithms'
    include Containers
    
    tree = RBTreeMap.new
  6. Reference available Containers and Algorithms

    master

    The library provides the following implementations:

    Containers

    • Heaps: Containers::Heap, Containers::MaxHeap, Containers::MinHeap
    • Priority Queue: Containers::PriorityQueue
    • Deque: Containers::Deque, Containers::CDeque (C extension)
    • Stack: Containers::Stack
    • Queue: Containers::Queue
    • Red-Black Trees: Containers::RBTreeMap, Containers::CRBTreeMap (C extension)
    • Splay Trees: Containers::SplayTreeMap, Containers::CSplayTreeMap (C extension)
    • Tries: Containers::Trie
    • Suffix Array: Containers::SuffixArray

    Algorithms

    • Search: Algorithms::Search.binary_search, Algorithms::Search.kmp_search
    • Sorting: Algorithms::Sort.bubble_sort, Algorithms::Sort.comb_sort, Algorithms::Sort.selection_sort, Algorithms::Sort.heapsort, Algorithms::Sort.insertion_sort, Algorithms::Sort.shell_sort, Algorithms::Sort.quicksort, Algorithms::Sort.mergesort, Algorithms::Sort.dualpivotquicksort
  7. Check for substrings with Containers::SuffixArray#has_substring?

    master

    Use has_substring?(substring) to determine if a specific substring exists within the original string. This method uses binary search for efficiency.

    Complexity: O(m + log n) where m is the length of the substring and n is the total number of suffixes.

    Returns: true if the substring is found, false otherwise.

    s_array = Containers::SuffixArray.new("abracadabra")
    s_array.has_substring?("abra") #=> true
    s_array.has_substring?("nope") #=> false
  8. Perform substring search with Knuth-Morris-Pratt (KMP) algorithm

    master

    Use Algorithms::Search.kmp_search(string, substring) to efficiently find the starting position of a substring within a string.

    • Returns: The integer index of the starting position where the substring is found. Returns nil if no match is found.
    • Complexity: O(n + k), where n is the length of the string and k is the length of the substring.
    • Instance Method: You can include Algorithms::Search in a class (like String) to call kmp_search as an instance method.
    # As a module method
    Algorithms::Search.kmp_search("ABC ABCDAB ABCDABCDABDE", "ABCDABD") #=> 15
    Algorithms::Search.kmp_search("ABC ABCDAB ABCDABCDABDE", "ABCDEF") #=> nil
    
    # As an instance method via inclusion
    class String; include Algorithms::Search; end
    "ABC ABCDAB ABCDABCDABDE".kmp_search("ABCDABD") #=> 15
  9. Delete an item from SplayTreeMap

    master

    Use delete(key) to remove the item associated with the key. It returns the value of the deleted item, or nil if the key was not found. This operation has amortized $O(\log n)$ complexity.

    map = Containers::SplayTreeMap.new
    map["MA"] = "Massachusetts"
    map.delete("MA") #=> "Massachusetts"
    map.delete("MA") #=> nil
    map = Containers::SplayTreeMap.new
    map["MA"] = "Massachusetts"
    map.delete("MA") #=> "Massachusetts"