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

Discussion Overview

The discussion centers on understanding the running time of the First Fit (FF) algorithm, both in its original and improved versions. Participants explore the computational complexity associated with these implementations, including the use of segment trees and priority queues, and seek clarification on the underlying concepts and pseudocode.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant explains that the original FF algorithm has a running time of O(n^2) due to scanning all bins for each item, assuming n bins and n items per bin.
  • Another participant introduces the improved version using a segment tree, suggesting that it reduces the time to O(n log n) by allowing O(log n) time for finding the appropriate bin.
  • There is a challenge regarding the running time calculation, with one participant questioning why the total running time is O(n log n) instead of O(log n log n), as they believe searching and insertion times should be multiplied.
  • A later reply clarifies that O(n log n) represents the time to insert n items, with each insertion taking O(log n) time.
  • One participant emphasizes the importance of studying the pseudocode to understand the running time and mentions that the worst-case scenario involves opening a new bin for each item.
  • Another participant notes that segment trees are not the only method to achieve O(n log n) running time, suggesting alternatives like priority queues.

Areas of Agreement / Disagreement

Participants express differing views on the running time calculations and the understanding of segment trees. There is no consensus on the exact implications of the running times or the best approach to analyze the FF algorithm.

Contextual Notes

Some participants mention the lack of pseudocode in certain resources, which may limit their understanding of the algorithm's running time. The discussion also highlights the complexity of segment trees and alternative methods for achieving efficient running times.

Who May Find This Useful

This discussion may be useful for individuals interested in algorithm analysis, particularly those studying memory allocation algorithms and their computational complexities in computer science.

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   Reactions: 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   Reactions: 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.
 

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