Running time of FirstFit(FF): Original and Improved version

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Running Time
Click For Summary
SUMMARY

The running time of the First Fit (FF) algorithm in its original implementation is O(n^2), as it requires scanning all bins for each item, leading to a quadratic complexity. The improved version utilizes a segment tree to reduce the search time for the appropriate bin to O(log n), resulting in an overall complexity of O(n log n) when inserting n items. This efficiency is achieved by combining the linear traversal of items with logarithmic searches in the segment tree. Alternative methods, such as using a priority queue, can also achieve O(n log n) complexity.

PREREQUISITES
  • Understanding of algorithmic complexity (Big O notation)
  • Familiarity with the First Fit algorithm and its applications
  • Knowledge of segment trees and their operations
  • Experience with priority queues and their use in algorithm optimization
NEXT STEPS
  • Study the implementation details of the First Fit algorithm in various programming languages
  • Learn about segment trees and their applications in range queries
  • Explore priority queues and their role in optimizing bin packing algorithms
  • Review algorithmic complexity analysis techniques for different data structures
USEFUL FOR

Software developers, algorithm enthusiasts, and computer scientists interested in optimizing memory allocation strategies and understanding advanced data structures like segment trees and priority queues.

zak100
Messages
462
Reaction score
11
TL;DR
Hi,
I am trying to understand the running time of FF algorithm
Hi,

I am trying to understand the running time of FF both the original and improved version. For the original version book says:

A simple method of implementing first fit would process each item by scanning down the list of bins sequentially. This would take O(n^2).

What I understand from this that before placing the items, FF traverses all the bins from beginning to end i.e. n bins.
Now each bin can have n items, because of that the running time is O(n^2)

Kindly guide me is my understanding correct.

Zulfi.
 
Technology news on Phys.org
Hi,
I am now discussing the improved version:

I found the improved version here:

First Fit Tree Version (Improved Running time)

We would like to reduce the time needed to find the first (leftmost) bin where the current item can fit, which required O(n) time per item. To achieve this goal we can use a segment tree for maximum range queries on the remaining bin spaces. This way every time a new item is given we can go down the segment tree and find the bin that we should use in O(logn) time for each item. So the computatonal complexity of the algorithm drops down to O(nlogn)
Q.What is a segment tree above?

I can't understand the running time of this tree version. In case of tree version, searching should take O(log n) and insertion should take O(log n).

So the total running time should be O(log n log n).

Somebody please guide me why the running time is: O (n log n).

Zulfi.
 
zak100 said:
I can't understand the running time of this tree version. In case of tree version, searching should take O(log n) and insertion should take O(log n).

So the total running time should be O(log n log n).

Somebody please guide me why the running time is: O (n log n).
I don't understand why you multiply the times for searching and insertion to get O(log n log n).

O(n log n) is just the time to insert n items, if one item takes O(log n)
 
  • Like
Likes zak100 and QuantumQuest
zak100 said:
Summary:: Hi,
I am trying to understand the running time of FF algorithm

A simple method of implementing first fit would process each item by scanning down the list of bins sequentially. This would take O(n^2).

What I understand from this that before placing the items, FF traverses all the bins from beginning to end i.e. n bins.
Now each bin can have n items, because of that the running time is O(n^2)

My question is, have you studied the pseudocode for this? I ask because, the way you write it, seems that you're trying to understand it somewhat in the abstract. This is not necessarily wrong but studying the pseudocode of an algorithm is helpful in any case and particularly when you have any doubt. If you study the pseudocode, you'll see in which case and why the running time of the FF algorithm yields an asymptotical ##O(n^2)##.

In short, the worst case is the one in which a new bin has to be opened for each new insertion of an object.

Now, one very important use of this FF algorithm is as a partition selection algorithm in operating systems i.e. selecting free memory to accommodate a process. A high level way in which it works is: The OS looks at the whole of the sections of free memory. The process is allocated to the first hole found that is larger regarding the size of the process. If the size of the process matches the size of the hole then there is (obviously) no hole anymore otherwise the hole continues to exist but reduced by the size of the process.

First Fit is a fast method but not necessarily an optimal one.

zak100 said:
Q.What is a segment tree above?

Segment Tree

Also, take note that the segment tree is not the only way to achieve ##O(n\log n)## worst-case running time. You can also use other heuristics as well, like a priority queue, to accommodate, for example, the bins with maximum remaining capacity so, effectively, you'll have one loop for objects and a priority quee for bins yielding an ##O(n \cdot \log n)## worst-case running time.
 
Last edited:
  • Like
Likes zak100
Hi,
Thanks for your answers. Priority queue example clearly explains why the running time is O(nlog n). However, I am using Allen Weiss and that book does not have pseudocode of FF.

Segment tree is a difficult concept. I would try to understand it at some other time.

Zulfi.
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
5
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
1
Views
2K