# Memory efficient undo data structure

I was playing a puzzle game and started wondering how they implemented their undo feature. The game only has five possible moves, and only when the player does a move does the game state change, but the game state can be quite complicated.

The standard undo implementation would be to keep track of a stack of all past game states. However, since the game state can get quite complicated, this can end up taking up a lot of memory.

Assuming the game state changes deterministically, a more memory efficient implementation would be to only keep track of all performed moves, because they require way less memory. However, an undo operation would then require recalculating all past moves which would take up a lot of processing power instead.

I was wondering if there is any literature on a data structure that tries to optimize both. Intuitively, I expect it to be possible to implement a data structure that only requires storing a logarithmic number of game states while also requiring a logarithmic number of move calculations for every undo operation. Of course this still requires storing all past moves, but the assumption is that this is significantly less memory than storing all past game states.

To be clear, I have some ideas myself of how this could be implemented. I am mostly interested in whether there already exists some literature on the topic, because I was unable to find any.

For some context, the game that made me bring this into question is called baba is you. I suspect they simply store a log of changes to the game state rather than the entire game states, because this usually, though not always, requires significantly less memory. However, it still made me wonder about this. Moreover, the game state does not always change deterministically in baba is you, but this could be fixed by storing some rng seed together with the performed moves.

• I haven't thought it through yet, because I was hoping to just find a reference on this. But you can do square root complexity relatively easily by storing the last $\sqrt{n}$ states and every state multiple of $\sqrt{n}$. Whenever you've undone all past $\sqrt{n}$ states you can recalculate the last $\sqrt{n}$ states before that. I think if you do something more clever with powers of two, logarithmic complexity should be possible. Aug 31 at 12:09
• Instead of waiting until you've undone all past $\sqrt{n}$ states, you can recalculate the $\sqrt{n}$ states before that gradually as you're performing undo operations, kind of in an amortized way. This should allow square root memory complexity with only constant processing power per undo operation. This will be way more complicated if you try something like this with powers of two to reach logarithmic memory, but I think it should be possible. Aug 31 at 12:57
{"ft_lang_label":"__label__en","ft_lang_prob":0.97498465,"math_prob":0.9121772,"size":1871,"snap":"2023-40-2023-50","text_gpt3_token_len":365,"char_repetition_ratio":0.13122657,"word_repetition_ratio":0.0,"special_character_ratio":0.18866916,"punctuation_ratio":0.09322034,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9657657,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-03T21:50:17Z\",\"WARC-Record-ID\":\"<urn:uuid:19a9d28e-8c2b-4fce-9af6-0f6a4d45196f>\",\"Content-Length\":\"162312\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:23081568-5c59-40bc-9072-e48947fb4712>\",\"WARC-Concurrent-To\":\"<urn:uuid:c31718ef-a29d-44e7-97cb-29b70cd9de4d>\",\"WARC-IP-Address\":\"172.64.144.30\",\"WARC-Target-URI\":\"https://cs.stackexchange.com/questions/161832/memory-efficient-undo-data-structure\",\"WARC-Payload-Digest\":\"sha1:EKRO6OIYESJJIS3OG47QNGJC73KYOCDS\",\"WARC-Block-Digest\":\"sha1:VITHKECNEMJZ5GCXF4MHLBJCEQOGZHMT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100508.53_warc_CC-MAIN-20231203193127-20231203223127-00884.warc.gz\"}"}