# What Sorts of Algorithms and Data Structures are Useful to Game Programmers?

This topic is 1896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello to you all. I purchased the book "Introduction to Algorithms" because I'd like to get a better understanding of the sorts of algorithms and data structures that are used in game programming. The first thing that I noticed about the book is that it is HUGE. There's no way I could just sit down and read this thing from cover to cover, and even if I did I would hardly be able to retain all of that information.. I plan on going through all of the sorting algorithms since the book mentions that they are used by many of the other algorithms, and I am already familiar with linked-lists. What other algorithms and data structures are common in game programming?

##### Share on other sites
Some off the top of my head that I've found useful to learn:

- Stacks & queues
- Linked lists (mostly singly-linked but you may find all the variations useful)
- Trees

- Sorting algorithms of all varieties
- Pathfinding algorithms (A*, Dijkstra's)
- Object pooling

##### Share on other sites
Well, you'll find all sorts of neccessities in games programming so my suggestion is learn them all - eventually. Most CS courses will build up from the basics (which archanian listed pretty well).

Really, unless you know [i]what[/i] [i]kind[/i] of games you are going to be working on, it would be hard to say exactly what you'll need. Problems found in game creation are many and varied - if not just as varied as in any other field, than pretty close to it. Learn a variety of algorithms for a number of different applications (when would you use a stack over a queue? ).

Yeah, sorry. I know it's a pain in the ass kind of answer, but it's the best and only one I can give. Educate yourself broadly, and know where to go for answers to specific questions (I've forgotten most of what I learned in my CS courses, but I remember where to find it all again!).

Oh, and learn about hashing. Hashing is magical.

##### Share on other sites
Sorry but I disagree.

Sorting algorithms. Here's what you need to know.
[url="http://www.cplusplus.com/reference/algorithm/sort/"]std::sort()[/url].
Having spent several months at sorting algorithms, I feel safe stating that this is the only thing I care about sorting as an algorithm book would discuss.

Pathfinding algorithms.
Spending effort on them even before starting is a bit of a stretch. I'm not saying this is not necessary, but you can get a lot of mileage before this becomes a blocking stop.

Object pooling.
Studying that in theory is complete nonsense. Get a real problem first. Profile. Fix (if necessary).

What's really needed? How to build sane code. Documentation, style, engineering, knowledge of real-world problems.
Or more related to implementation: object lifetimes, smart pointers, proper OOP, various code patterns etc. Edited by Krohm

##### Share on other sites
Game programmers use every algorithm and data structure. An unhelpful statement, but true.

You have to classify the different types of game programming tasks to understand what algorithms and data structures are applicable.

For example, someone implementing a 3D rendering system will use totally different things than the guy writing high-level gameplay logic.

My suggestion: Some tasks in code are FAR more common than others. You can get 80% of any programming job done with nothing but simple lists (std::vector, System.Collections.Generic.List<T>, etc) and map/filter operations (map = convert one list to another by applying a conversion function to each element, filter = return a subset of a list based on a function that tests each element). Learn how to use these well. When you run into a case where performance or memory use is too unacceptable, open your book up and look for something that's specific to your situation. Edited by Nypyren

##### Share on other sites
May I suggest looking through the book "Data Structures for Game Programmers"

##### Share on other sites
For basic game logic, you need to understand at least basic trees because that's what you need to represent hierarchical things.

Basic algos: at least binary search, breadth-first search. The latter is a simple way to solve a wide range of general problems, including pathfinding. A*, IDA* etc. are more complicated, potentially more efficient ways of solving similar problems but you can look at them later if and when you actually need performance.

For performance you are going to have to learn heaps and hashes.

As an aside, you'll rarely want to use a linked list because the performance sucks in practice. Vectors/arrays are usually the right way even when textbook logic would suggest linked list. A lot of the time you will want to keep these sorted so that you can use binary search on them.

##### Share on other sites
Get familiar with graphs and trees, most spacial algebra, coordinate spaces, translation, rotation, scale, cinematic physics, smart pointers are useful but expensive, there is nothing like knowing how to be careful with dynamic memory, state machines... profiling and debugging tools & techniques...

##### Share on other sites
Oh, also get a good grip on recursivity and asynchronous execution, they are tough topics many people crash against sooner or later

##### Share on other sites
[quote name='Krohm' timestamp='1351840220' post='4996447']
What's really needed? How to build sane code. Documentation, style, engineering, knowledge of real-world problems.
Or more related to implementation: object lifetimes, smart pointers, proper OOP, various code patterns etc.[/quote]
So, that's an interesting point (not necessarily one that I agree with, but still).

All the things you describe are software engineering topics, whereas the OP's book is intended for courses in computer science. And there is a fair bit of truth to the assertion that in the day-to-day of software development, you'll make much more use of practical software engineering skills than you will of a theoretical computer science background.

But, and to my mind this is fairly important, a good developer has both the practical and theoretical backgrounds, and makes use of both fairly equally. Sooner or later you are going to need to build a cache-optimised graph traversal, or a lockless multi-threaded event dispatch mechanism - and at that point, you'll be feeling the lack of all that computer science knowledge...

##### Share on other sites
Thank you for pointing this out. I believe it is an important difference.
Not to be on the defensive but I still haven't had the chance to experience what you picture.

##### Share on other sites
All right, thanks for the input guys. I guess I should have expected these types of answers. Game programming is a broad subject as is data structures and algorithms so it's hard to give a solid answer. I'll just keep coding and reading and I'm sure my question will be answered in time. I bought the book because I don't have a formal education in computer science and I thought it would be usefull to learn some of this stuff. Plus, it was recommended by Mike McShaffry in the book "Game Coding Complete" that a programmer learn this stuff if he wants to write games. So I thought, hey what the heck.

@Krohm: I'd have to disagree. The "concept x is useless because I've never used it before." argument doesn't hold much water. Unless I'm just misunderstanding your point, in which case I apologize.

##### Share on other sites
Really everything in STL.
For a game developer, you don't really need to know that in detail. But know how data structures work. For example you should know how trees, linked lists and dynamic arrays work.
And learn to use the interface.

##### Share on other sites
I primarily use the following:

-Lists
-Dictionaries
-Stacks and Queues
-Arrays

Dictionaries are pretty much hash tables. If I need to store items and pull them out by some key value, then I use a dictionary. If I just need to store a collection of items, I use a list. I rarely use stacks and queues, but sometimes the occassion pops up where they're useful. Arrays are handy for keeping data continguous in memory (which helps with CPU caching), but I think I recall that C# lists are really just arrays wrapped into a list class. It might be worth looking into that...

I rarely ever use binary trees, but I do use binary space partitioning ideas (for QuadTrees) and recursion.
Since I primarily use dictionaries and look up items by a key, I never need to sort my items.

I think the very core of programming most software is about logically managing lists of data At least, that's what I find myself doing a lot.

##### Share on other sites
[quote name='lride' timestamp='1351890692' post='4996667']
For a game developer, you don't really need to know that in detail.[/quote]
Depends on what type of game developer. if you are working on mobile/console, then you are likely going to need to know a whole lot about cache architectures, efficient algorithm design, etc. If you are writing for PC, can probably get away without.

[quote name='slayemin' timestamp='1351891266' post='4996669']
I think the very core of programming most software is about logically managing lists of data [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] At least, that's what I find myself doing a lot.[/quote]
Then you should be learning LISP [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

##### Share on other sites
Game developers often wear many hats (particularly early in their career) and at some point you will probably be writing a shader, even if it's a simple one. But before anyone will really accept you in this industry (that is to say, hire you) you will need to have made a game or at least a solid demo of a game. The unfortunate truth is you will likely be doing this alone. Get a case of redbull and every programming book you can afford; long nights ahead.

##### Share on other sites
[quote name='Stroppy Katamari' timestamp='1351849220' post='4996481']
For basic game logic, you need to understand at least basic trees because that's what you need to represent hierarchical things.

Basic algos: at least binary search, breadth-first search. The latter is a simple way to solve a wide range of general problems, including pathfinding. A*, IDA* etc. are more complicated, potentially more efficient ways of solving similar problems but you can look at them later if and when you actually need performance.

For performance you are going to have to learn heaps and hashes.

As an aside, you'll rarely want to use a linked list because the performance sucks in practice. Vectors/arrays are usually the right way even when textbook logic would suggest linked list. A lot of the time you will want to keep these sorted so that you can use binary search on them.
[/quote]
I've reintroduced myself to a data structure that's simpler but as effective as a binary (search) tree. It's called the skip list.

##### Share on other sites
In my opinion its more important to know that other data structures and algorithms exist, and that using an array (or a list) will normally work, but there are often data structures much better for the task at hand. The trick is recognising those situations.

Be aware of data structures, but you don't need to go around implementing them until you [i]actually [/i]have a use for it.