Initialize array in O(1) -- how 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/09
21:22 UTC
Read the original article
Hit count: 148
algorithms
|naming
There is this pattern that trades performance of array access against the need to iterate 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 array has been reset.
This has come up in StackOverflow 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