Giter VIP home page Giter VIP logo

algorithms's Introduction

algorithms Build Status

API Documentation

DESCRIPTION:

Started as a Google Summer of Code 2008 project

Written by Kanwei Li, mentored by Austin Ziegler

Original Proposal:

Using the right data structure or algorithm for the situation is an important aspect of programming. In computer science literature, many data structures and algorithms have been researched and extensively documented. However, there is still no standard library in Ruby implementing useful structures and algorithms like Red/Black Trees, tries, different sorting algorithms, etc. This project will create such a library with documentation on when to use a particular structure/algorithm. It will also come with a benchmark suite to compare performance in different situations.

COMPLETED:

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

* Search algorithms
  - Binary Search            Algorithms::Search.binary_search
  - Knuth-Morris-Pratt       Algorithms::Search.kmp_search
* Sorting algorithms           
  - Bubble sort              Algorithms::Sort.bubble_sort
  - Comb sort                Algorithms::Sort.comb_sort
  - Selection sort           Algorithms::Sort.selection_sort
  - Heapsort                 Algorithms::Sort.heapsort
  - Insertion sort           Algorithms::Sort.insertion_sort
  - Shell sort               Algorithms::Sort.shell_sort
  - Quicksort                Algorithms::Sort.quicksort
  - Mergesort                Algorithms::Sort.mergesort
  - Dual-Pivot Quicksort     Algorithms::Sort.dualpivotquicksort

SYNOPSIS:

require 'rubygems'
require 'algorithms'

max_heap = Containers::MaxHeap.new

# To not have to type "Containers::" before each class, use:
include Containers
max_heap = MaxHeap.new

REQUIREMENTS:

  • Ruby 1.8, Ruby 1.9, JRuby
  • C extensions (optional, but very much recommended for vast performance benefits)

LICENSE:

See LICENSE.md.

algorithms's People

Contributors

amanelis avatar anothermh avatar bmorearty avatar dgtized avatar febuiles avatar garrisonj avatar gnufied avatar jsncmgs1 avatar kanwei avatar keithrbennett avatar loganb avatar ltfschoen avatar prerna1 avatar ysksn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

algorithms's Issues

Heap is broken with objects

Works when the heap length is < 10 but fails with 10+

min_heap = ::Containers::Heap.new(10.times.map{Object.new}){|x, y| (x <=> y) == -1}

while val = min_heap.pop do
  p val
end

RuntimeError: Couldn't delete node from stored nodes hash

Missing tags

There are several releases on rubygems.org without corresponding git tags. It'd be helpful to have those for reproducibility.

How to avoid issue about delete method in rbtree

  # if key is not present.
  #
  # !!! Warning !!! There is a currently a bug in the delete method that occurs rarely
  # but often enough, especially in large datasets. It is currently under investigation.
  #
  # Complexity: O(log n)
  #
  #   map = Containers::TreeMap.new
  #   map.push("MA", "Massachusetts")
  #   map.push("GA", "Georgia")
  #   map.min_key #=> "GA"```

Heapsort inefficiencies

One of the key advantages of heap sort is it can be an in place algorithm (https://en.wikipedia.org/wiki/In-place_algorithm); however, your implementation does not do an in place implementation:

https://github.com/kanwei/algorithms/blob/master/lib/algorithms/sort.rb#L85-L90

Instead, you are popping the values off the heap and putting them in a new array (when they could be kept in that array but moved to the end). Instead of calculating the size of the array in the heap, you could keep an index for the end of the heap within the array, and thus move the sorted values that have been popped off to the end of the actual array and decrement the heap's index.

At a minimum, the array here could be instantiated with the known size of the container. That prevents the array from being resized repeatedly (Ruby resizes an array when it hits 3, 20, 37, 56, 85, ... sizes, see here: https://github.com/ruby/ruby/blob/ruby_2_0_0/array.c#L183)

KD Tree Bugs?

There is a note at the top of the KD Tree implementation that states that the current version contains bugs. Is this simply an outdated comment or is there in fact bugs within the implementation?

Kind Regards,

Segfault on rb trees and splaytrees

I installed the 0.2 gem (via gem install), copy paste the tree test in my editor (removed first line at the top and put the "require 'rubygems'" at the top),
Just launched it and get a segfault ... everything goes well if I dont populate the sbtree and the rbtree by commenting that lines :

      rbtree { random_array.each_with_index  { |x,index| rbtree[index] = x } }  
      splaytree { random_array.each_with_index  { |x,index| splaytree[index] = x } }  

Binary search should return index

It would be great if the binary search method return the index of the object in the array instead of the object itself. The index gives you more information (where it is instead of just whether or not it exists) and it's free-- you already have it when returning.

Found segfault issue in splay tree (possibly other trees as well)

Hi there.

While using the library (specially the splay tree) as backend to a project, I did a stress test and found my app segfaulting under some strange and very specific conditions (300,000 items added to the tree in one loop followed by another loop of 300,000 items, no problem. 350,000 items added in one loop = segfault).

This is recreatable by loading your benchmarks for the treemaps and changing the array size to 261_123 (or any value above 261,122).

Add a priority queue / heap with max size

It's a pretty common problem (and we have this problem :)) where you want a priority queue that "loses" items of lesser priority (has a max size). What do you think of extending heap?

add top (?) that takes max

Not sure if this is a good name for it. We do a lot of fetching an array from the top of the heap (priority queue).

def top(max)
  results = []
  while results.size < max and size > 0 do 
    results << pop 
  end
  results
end

If you think it's a good idea, I'll make a proper pull request with a UT.

gem won't install on jruby

Building native extensions. This could take a while...
/custom/jruby/lib/ruby/1.8/mkmf.rb:7: JRuby does not support native extensions. Check wiki.jruby.org for alternatives. (NotImplementedError)
from /custom/jruby/lib/ruby/1.8/mkmf.rb:1:in `require'
from extconf.rb:1
ERROR: Error installing algorithms:
ERROR: Failed to build gem native extension.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.