Initialize array in amortized constant time -- what is this trick called?

Posted by user946850 on Programmers See other posts from Programmers or by user946850
Published on 2012-07-09T20:13:38Z Indexed on 2012/07/10 3:22 UTC
Read the original article Hit count: 226

Filed under:
|

There is this data structure that trades performance of array access against the need to iterate over it when clearing it. You keep a generation counter with each entry, and also a global generation counter. The "clear" operation increases the generation counter. On each access, you compare local vs. global generation counters; if they differ, the value is treated as "clean".

This has come up in this answer on Stack Overflow recently, but I don't remember if this trick has an official name. Does it?

One use case is Dijkstra's algorithm if only a tiny subset of the nodes has to be relaxed, and if this has to be done repeatedly.

© Programmers or respective owner

Related posts about algorithms

Related posts about naming