algorithm

Saturating Addition in C

What is the best (cleanest, most efficient) way to write saturating addition in C? The function or macro should add two unsigned inputs (need both 16- and 32-bit versions) and return all-bits-one (0xFFFF or 0xFFFFFFFF) if the sum overflows. Target is x86 and ARM using gcc (4.1.2) and Visual Studio (for simulation only, so a fallback im...

What is the meaning and impication of one-element-too-large array/queue?

just wondering what it is. Edit: I know it's not a type of array but just a feature. So what does it mean by one-element-too-large ? ...

which book to follow to improve skills on algorithm analysis?

which book to follow to improve skills on algorithm analysis? ...

Mathematics / Algorithmic Resources: ProjectEuler.net puzzles

I've used brute force for the most part for the ProjectEuler.net problems that I have been able to solve. One thing I'm finding is that, for some of the puzzles, I'm not able to find good resources for 'backfilling' my understanding of the problem domains the puzzles represent. What are suggested resources for learning about those topi...

Need to create a layered dict from a flat one

I have a dict, that looks like this: { 'foo': { 'opt1': 1, 'opt2': 2, }, 'foo/bar': { 'opt3': 3, 'opt4': 4, }, 'foo/bar/baz': { 'opt5': 5, 'opt6': 6, } } And I need to get it to look like: { 'foo': { 'opt1': 1, 'opt2': 2, ...

Robust and fast checksum algorithm?

Which checksum algorithm can you recommend in the following use case? I want to generate checksums of small JPEG files (~8 kB each) to check if the content changed. Using the filesystem's date modified is unfortunately not an option. The checksum need not be cryptographically strong but it should robustly indicate changes of any size. ...

how to determine if a record in every source, represents the same person

I have several sources of tables with personal data, like this: SOURCE 1 ID, FIRST_NAME, LAST_NAME, FIELD1, ... 1, jhon, gates ... SOURCE 2 ID, FIRST_NAME, LAST_NAME, ANOTHER_FIELD1, ... 1, jon, gate ... SOURCE 3 ID, FIRST_NAME, LAST_NAME, ANOTHER_FIELD1, ... 2, jhon, ballmer ... So, assuming that records with ID 1, from sources 1 a...

How would you generate default user profile pictures?

I've been admiring StackOverflow's default quilt-like profile pictures (which I notice are also on the Fail Blog) and am curious what program both are using to generate them. But what I really want to know is: If you were to design the system to create default profile pictures, how would you do it? I'm looking for ideas on what algorit...

How do I generate an array of pairwise distances in Ruby?

Say I have an array that represents a set of points: x = [2, 5, 8, 33, 58] How do I generate an array of all the pairwise distances? Thanks! ...

Picking a random element from a set

How do I pick a random element from a set? I'm particularly interested in picking a random element from a HashSet or a LinkedHashSet, in Java. Solutions for other languages are also welcome. ...

Formula for controlling the movement of a tank-like vehicle?

Anyone know the formula used to control the movement of a simple tank-like vehicle? To 'steer' it, you need to alter the force applied the left and right "wheels". Eg. 1 unit of force on both wheels makes it go forward. -1 units of force on both wheels makes it go backwards. Apply more force to one wheel than the other and it turns....

What Algorithm for a Tic-Tac-Toe Game Can I Use To Determine the "Best Move" for the AI?

In tic-tac-toe implementation I guess the challenging part is to determine the best move to be played by the machine. What are some of the algorithms that can pursued? I'm looking into implementation from simple to complex. How would I go about tackling this part of the problem? ...

Piece together several images into one big image

I'm trying to put several images together into one big image, and am looking for an algorithm which determines the placing most optimally. The images can't be rotated or resized, but the position in the resulting image is not important. edit: added no resize constraint ...

Shape recognition algorithm(s)

Can anyone point me to a shape recognition algorithm, preferably in C#/Java but other languages are fine too. ...

Hash-algorithm

I am looking for a hash-algorithm, to create as close to a unique hash of a string (max len = 255) as possible, that produces a long integer (DWORD). I realize that 26^255 >> 2^32, but also know that the number of words in the English language is far less than 2^32. The strings I need to 'hash' would be mostly single words or some simp...

What is a good source for geometric algorithms?

I am looking for any good sources for geometric algorithms specifically; The simple stuff like when two lines cross and so on is easy enough (and easy to find), but I would like to find somewhere with algorithms for the more tricky things, such as finding the shape formed by expanding a given polygon by some amount; fast algorithms for...

Algorithm for counting the number of unique colors in an image

Looking for one that is fast enough and still graceful with memory. The image is a 24bpp System.Drawing.Bitmap. ...

Prisoner's Dilemma Algorithm

After watching The Dark Knight I became rather enthralled with the concept of the Prisoner's Dilemma. There must be an algorithm that that maximizes one's own gain given a situation. For those that find this foreign: http://en.wikipedia.org/wiki/Prisoner%27s_dilemma Very, very interesting stuff. Edit: The question is, what is, if any,...

How to determine differences in two lists of data

This is an exercise for the CS guys to shine with the theory. Imagine you have 2 containers with elements. Folders, URLs, Files, Strings, it really doesn't matter. What is AN algorithm to calculate the added and the removed? Notice: If there are many ways to solve this problem, please post one per answer so it can be analysed and vote...

looking for simulated annealing implementation in VB

Is anyone aware of a reasonably well documented example of simulated annealing in Visual Basic that I can examine and adapt? ...