• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

18 replies to this topic

### #1sunnysideup  Members

105
Like
0Likes
Like

Posted 01 November 2012 - 06:12 PM

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?

### #2archanian  Members

247
Like
4Likes
Like

Posted 01 November 2012 - 09:39 PM

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

### #3Telcontar  Members

1371
Like
4Likes
Like

Posted 01 November 2012 - 10:41 PM

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 what kind 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.

I Create Games to Help Tell Stories | Writing Blog

### #4MaxDZ8  Members

4976
Like
1Likes
Like

Posted 02 November 2012 - 01:10 AM

Sorry but I disagree.

Sorting algorithms. Here's what you need to know.
std::sort().
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, 02 November 2012 - 01:11 AM.

Previously "Krohm"

### #5Nypyren  Members

10965
Like
2Likes
Like

Posted 02 November 2012 - 01:34 AM

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, 02 November 2012 - 01:40 AM.

### #6Alpheus  GDNet+

6806
Like
1Likes
Like

Posted 02 November 2012 - 01:36 AM

May I suggest looking through the book "Data Structures for Game Programmers"
External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science

Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley

If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

### #7Yrjö P.  Members

1416
Like
1Likes
Like

Posted 02 November 2012 - 03:40 AM

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.

### #8NEXUSKill  Members

475
Like
0Likes
Like

Posted 02 November 2012 - 10:31 AM

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...
Game making is godlike

### #9NEXUSKill  Members

475
Like
0Likes
Like

Posted 02 November 2012 - 10:36 AM

Oh, also get a good grip on recursivity and asynchronous execution, they are tough topics many people crash against sooner or later
Game making is godlike

### #10swiftcoder  Senior Moderators

17793
Like
4Likes
Like

Posted 02 November 2012 - 11:17 AM

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.

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...

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #11MaxDZ8  Members

4976
Like
2Likes
Like

Posted 02 November 2012 - 11:51 AM

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.

Previously "Krohm"

### #12sunnysideup  Members

105
Like
0Likes
Like

Posted 02 November 2012 - 02:24 PM

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.

### #13lride  Members

652
Like
0Likes
Like

Posted 02 November 2012 - 03:11 PM

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.
An invisible text.

### #14slayemin  Members

5630
Like
0Likes
Like

Posted 02 November 2012 - 03:21 PM

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.

### #15swiftcoder  Senior Moderators

17793
Like
0Likes
Like

Posted 02 November 2012 - 03:33 PM

For a game developer, you don't really need to know that in detail.

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.

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.

Then you should be learning LISP

Tristam MacDonald - Software Engineer @ Amazon - [swiftcoding] [GitHub]

### #16Aeramor  Members

1767
Like
0Likes
Like

Posted 05 November 2012 - 03:25 PM

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.

-Aeramor

CTO at Conjecture, Inc.

### #17Alpheus  GDNet+

6806
Like
0Likes
Like

Posted 05 November 2012 - 03:51 PM

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.

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.
External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science

Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley

If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

### #18Postie  Members

1459
Like
1Likes
Like

Posted 05 November 2012 - 05:10 PM

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 actually have a use for it.
Currently working on an open world survival RPG - For info check out my Development blog:

### #19archanian  Members

247
Like
0Likes
Like

Posted 07 November 2012 - 09:56 PM

I think this is good advice, but the only way to know the appropriate uses for such algorithms/structures is to understand how they work and what their strengths/weaknesses are, for which you will need to at least study them, and preferably practice using them.

I know a lot of programming can be done by just "using what works", but for me it all depends how seriously you want to take your game development. If you want to make a career out of it, you're going to need to know a lot of theory as well as practical experience.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.