Giter Club home page Giter Club logo

binpacking's Introduction

Best Fit Bin Packing Algorithm in O(n log n)

The best fit bin packing algorithm is generally implemented in O(n**2).

In this implementation, the algorithm runs in O(nlogn) with the use of a hash table.

The algorithm initiates a class for each bin which accepts add, remove, sum and show operations.

The AVL tree stores the capacity of each bin. As elements are added to a bin or a new bin is created, the AVL tree is updated.

A hash table stores the capacity and index of bin as key, value pairs.

The best_fit() function traverses through the AVL tree to find the least capacity that is greater than or equal to the item.

If a suitable capacity is found, the corresponding bin index is obtained from the hash table and the element is added to that bin โ€“ the AVL tree and the hash table are updated.

If no suitable capacity is found in the AVL tree, a new bin is created for the element and the remaining capacity is added to the AVL tree.

The (capacity, bin_index) is added as key, value pair in the hash table.

The algorithm only traverses the height of the AVL tree (log n). Therefore, the total runtime complexity improves to O(nlogn).

To apply this to best fit decreasing, you can sort the items and apply the best fit algorithm.

binpacking's People

Contributors

karthikvellanki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sus0pid

binpacking's Issues

Incorrect result

The best_fit function does not always produce the correct result. Consider the following item list: [0.38, 0.35, 0.06, 0.21, 0.28, 0.57, 0.15]. The Best Fit algorithm should use exactly two bins: (0.38, 0.35, 0.06, 0.21) and (0.28, 0.57, 0.15). However, the code reports three bins being used.

This is likely caused due to a floating-point precision issue.

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.