algorithm

How to create a GUID / UUID in Javascript?

I'm trying to create globally-unique identifiers in Javascript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, etc.. The GUID / UUID should be at least 32 characters and should stay in the ASCII range to avoid trouble when passing them around. ...

How to get scientific results from non-experimental data (datamining?)

I want to obtain maximum performance out of a process with many variables, many of which cannot be controlled. I cannot run thousands of experiments, so it'd be nice if I could run hundreds of experiments and vary many controllable parameters collect data on many parameters indicating performance 'correct,' as much as possible, for th...

Algorithm to blend gradient filled corners in image

I need to put an alpha blended gradient border around an image. My problem is in blending the corners so they are smooth where the horizontal and vertical gradients meet. I believe there is a standard algorithm that solves this problem. I think I even encountered it in school many years ago. But I have been unsuccessful in finding any...

An algorithm to generate a game map from individual images

I am designing a game to be played in the browser. Game is a space theme and I need to generate a map of the "Galaxy". Basic idea of the map is here: The map is a grid, with each grid sector can contain a planet/system and each of these has links to a number of adjacent grids. To generate the maps I figured that I would have a coll...

Big-O for Eight Year Olds?

I'm asking more about what this means to my code. I understand the concepts mathematically, I just have a hard time wrapping my head around what they mean conceptually. For example, if one were to perform an O(1) operation on a data structure, I understand that the amount of operations it has to perform won't grow because there are mor...

How to compute the critical path of a directional acyclic graph?

What is the best (regarding performance) way to compute the critical path of a directional acyclic graph when the nodes of the graph have weight? For example, if I have the following structure: Node A (weight 3) / \ Node B (weight 4) Node D (weight 7) / \ Node E (weight...

Optimal multiplayer maze generation algorithm

I'm working on a simple multiplayer game in which 2-4 players are placed at separate entrypoints in a maze and need to reach a goal point. Generating a maze in general is very easy, but in this case the goal of the game is to reach the goal before everyone else and I don't want the generation algorithm to drastically favor one player ov...

What's the simplest way to test whether a number is a power of 2 in C++?

I need a function like this: // return true iff 'n' is a power of 2, e.g. // is_power_of_2(16) => true is_power_of_2(3) => false bool is_power_of_2(int n); Can anyone suggest how I could write this? Can you tell me a good web site where this sort of algorithm can be found? ...

convert timestamp to alphanum

I have an application where an user has to remember and insert an unix timestamp like 1221931027. In order to make it easier to remember the key I like to reduce the number of characters to insert through allowing the caracters [a-z]. So I'm searching for an algorithm to convert the timstamp to a shorter alphanum version and do the same ...

Best way to randomize a string array in C#

I was just wondering what is the best way to randomize an array of strings in C#. My array contains about 500 strings and I'd like to create a new Array with the same strings but in a random order. Any suggestions? ...

Best algorithm to count the number of set bits in a 32-bit integer?

8 bits representing the number 7 look like this: 00000111 Three bits are set. What is the best algorithm to determine the number of set bits in a 32-bit integer? ...

Algorithm to calculate the number of divisors of a given number

What would be the most optimal algorithm (performance-wise) to calculate the number of divisors of a given number? It'll be great if you could provide pseudocode or a link to some example. EDIT: All the answers have been very helpful, thank you. I'm implementing the Sieve of Atkin and then I'm going to use something similar to what Jon...

Shortest Root to Leaf Path

What is the easiest way, preferably using recursion, to find the shortest root-to-leaf path in a BST (Binary Search Tree). Java prefered, pseudocode okay. Thanks! ...

Stable, efficient sort?

I'm trying to create an unusual associative array implementation that is very space-efficient, and I need a sorting algorithm that meets all of the following: Stable (Does not change the relative ordering of elements with equal keys.) In-place or almost in-place (O(log n) stack is fine, but no O(n) space usage or heap allocations. O(n ...

What is a performant string hashing function that results in a 32 bit integer with low collision rates?

I have lots of unrelated named things that I'd like to do quick searches against. An "aardvark" is always an "aardvark" everywhere, so hashing the string and reusing the integer would work well to speed up comparisons. The entire set of names is unknown (and changes over time). What is a fast string hashing algorithm that will generate s...

What is a good reference for how calculation engines such as Excel work?

I am interested in learning how dependency-based calculation engines work in practice. Of course, I can make up my own algorithm, but I was curious if there were any well explained algorithms that are used in practice. I have tried Google, but most articles are < "good". ...

Algorithm to detect intersection of two rectangles?

I'm looking for an algorithm to detect if two rectangles intersect (one at an arbitrary angle, the other with only vertical/horizontal lines). Testing if a corner of one is in the other ALMOST works. It fails if the rectangles form a cross-like shape. It seems like a good idea to avoid using slopes of the lines, which would require sp...

What pre-existing services exist for calculating distance between two addresses?

I'd like to implement a way to display a list of stored addresses sorted by proximity to a given address. Addresses in the list will be stored in a database table. Separate parts have separate fields (we have fields for postal code, city name, etc.) so it is not just a giant varchar. These are user-entered and due to the nature of the...

Finding common blocks

I have two files (f1 and f2) containing some text (or binary data). How can I quickly find common blocks? e.g. f1: ABC DEF f2: XXABC XEF output: common blocks: length 4: "ABC " in f1@0 and f2@2 length 2: "EF" in f1@5 and f2@8 ...

How would you display an array of integers as a set of ranges? (algorithm)

Given an array of integers, what is the simplest way to iterate over it and figure out all the ranges it covers? for example, for an array such as: $numbers = array(1,3,4,5,6,8,11,12,14,15,16); The ranges would be: 1,3-6,8,11-12,14-16 ...