Amortized Analysis of Spreading and Gathering
Learn about the analysis of gather and spread methods.
We'll cover the following...
Next, we consider the cost of the gather(u)
and spread(u)
methods that may be executed by the add(i, x)
and remove(i)
methods. For the sake of completeness, here they are:
Press + to interact
void spread(Node u) {Node w = u;for (int j = 0; j < b; j++) {w = w.next;}w = addBefore(w);while (w != u) {while (w.d.size() < b)w.d.add(0, w.prev.d.remove(w.prev.d.size() - 1));w = w.prev;}}
Here is the implementation of the gather()
method:
Press + to interact
void gather(Node u) {Node w = u;for (int j = 0; j < b - 1; j++) {while (w.d.size() < b)w.d.add(w.next.d.remove(0));w = w.next;}remove(w);}
Amortization
The running time of each of these methods is dominated by the two nested loops. Both the inner and outer loops execute at most times, so the total running time of each of these ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy