algorithm

Bitwise XORing and shifting of integer arrays

Suppose a bit sequence of size M, and another bit sequence of size N, with M >> N. Both M and N can be saved inside integer arrays: If N has a length of 30 then an array with only one integer will be needed, but if N has a length of 300 then an array with 10 integers will be needed to store it. What I am trying to do is to shift N insi...

Possible to rearrange an array in place in O(N)?

If I have a size N array of objects, and I have an array of unique numbers in the range 1...N, is there any algorithm to rearrange the object array in-place in the order specified by the list of numbers, and yet do this in O(N) time? Context: I am doing a quick-sort-ish algorithm on objects that are fairly large in size, so it would be ...

Iterating over subsets of any size

I can iterate over the subsets of size 1 for( int a = 0; a < size; a++ ) { or subsets of size 2 for( int a1 = 0; a1 < size; a1++ ) { for( int a2 = a1+1; a2 < size; a2++ ) { or 3 for( int a1 = 0; a1 < size; a1++ ) { for( int a2 = a1+1; a2 < size; a2++ ) { for( int a3 = a2+1; a3 < size; a3++ ) { But how to do this for subse...

Generating a gaussian distribution with only positive numbers

Is there any way to randomly generate a set of positive numbers such that they have a desired mean and standard deviation? I have an algorithm to generate numbers with a gaussian distribution, but I don't know how to deal with negative numbers in a way the preserves the mean and standard deviation. It looks like a poisson distribution...

Algorithm for positioning rectangular and randomly sized objects inside a non-rectangular canvas.

. . Hi there, everybody. . . I have a canvas defined by many points (x,y)--it's not rectangular, but at least it will follow some almost-hexagonal shape (like a distorted hexagon, with angles that are always < 180 degrees). I also have a big collection of different rectangles (more or less 140 of them, with different widths and heights)...

There is some way to do this string extraction faster?

I need to extract the virtual host name of a HTTP request. Since this willl be done for every request, I´m searching for the fastest way to do this. The following code and times are just some of the ways I had studied. So, there is some faster way to do this? $hostname = "alphabeta.gama.com"; $iteractions = 100000; //While Test $ti...

Finding if a Valid Rummikub Solution exist from selected Tiles

I'm currently making a rummikub "game" so that when I make a Mistake I am able to get the board back easily to what it was before. Currently in need to find out for a given number of tiles if a solution exists and what the solution is. Example Elements Selected are {Red1, Red1, Blue1, Black1, Orange1, Orange1} And a solution would b...

Removing duplicates from a list with "priority"

Given a collection of records like this: string ID1; string ID2; string Data1; string Data2; // : string DataN Initially Data1..N are null, and can pretty much be ignored for this question. ID1 & ID2 both uniquely identify the record. All records will have an ID2; some will also have an ID1. Given an ID2, there is a (time-consuming...

What's the most efficient way to compare two blocks of memory?

I need a comparison function for blocks of memory for doing binary searches on arrays of bytes in the D programming language. It does not need to have any useful semantics. It only needs to be fast and be a valid comparison function (one that produces a total ordering). The blocks of memory to be compared are already known to be the s...

Finding the most frequent subtrees in a collection of (parse) trees

I have a collection of trees whose nodes are labelled (but not uniquely). Specifically the trees are from a collection of parsed sentences (see http://en.wikipedia.org/wiki/Treebank). I wish to extract the most common subtrees from the collection - performance is not (yet) an issue. I'd be grateful for algorithms (ideally Java) or pointe...

Verify Knuth shuffle algorithm is as unbiased as possible

I'm implementing a Knuth shuffle for a C++ project I'm working on. I'm trying to get the most unbiased results from my shuffle (and I'm not an expert on (pseudo)random number generation). I just want to make sure this is the most unbiased shuffle implementation. draw_t is a byte type (typedef'd to unsigned char). items is the count of i...

How to get the plane out of a arbitrary collection of points?

I've got a arbitrary collection of 3d points, i know they're coplanar, but how do i calculate that plane? ...

Weighted average of angles

I want to calculate the weighted mean of a set of angles. In this Question, there's an answer how to calculate the mean as shown in this page. Now I'm trying to figure out how to calculate the weighted average. That is, for each angle there is a weight (the weights sum up to 1) 0.25, 0 degrees 0.5, 20 degrees 0.25, 90 degrees The we...

Hash function that maps similar inputs to similar outputs?

Is there a hash function where small changes in the input result in small changes in the output? For example, something like: hash("Foo") => 9e107d9d372bb6826bd81d3542a419d6 hash("Foo!") => 9e107d9d372bb6826bd81d3542a419d7 <- note small difference ...

Converting vector-contoured regions (borders) to a raster map (pixel grid)

I have a map that is cut up into a number of regions by borders (contours) like countries on a world map. Each region has a certain surface-cover class S (e.g. 0 for water, 0.03 for grass...). The borders are defined by: what value of S is on either side of it (0.03 on one side, 0.0 on the other, in the example below) how many points t...

How can I calculate how many nights are there in a date range?

I need to calculate the quantity of nights (stay at a hotel) from the checkin and checkout dates. What is the best way to do it? ie: If I have Checkin: 12/11/2009 15:00 hs Checkout: 14/11/2009 12:00 hs Doing (Checkout - Checkin).Days would give me 1 night instead of 2 I'm thinking of adding a simple if to check the hours (if ch...

Algorithm for a directed graph problem

Please help me out with an algorithm for the following problem - Given a collection of facts, we would like to get rid of as much redundancy as possible. The facts involved in this problem are members of a transitive relation between uppercase letters. So each fact is a pair of uppercase letters such as AB meaning that A is related to ...

How to reverse bits of a byte ?

Best in PHP, for example, 11011111 ==> 11111011 ...

Reversing order of elements

The following code rearranges elements by the attribute "amount". How can I alter this code so the items will be reversed? Thanks. var parent = $('#items'); var children = $('a', parent); children.sort(function(a, b) { return parseInt($(a).attr('amount')) - parseInt($(b).attr('amount')); }) $.each(children, function(i, child) { ...

Suggestions for a fast two way encrypt?

Can someone suggest a fast 2 way encryption algorithm for long ints? My candidates are: AES: the Advanced Encryption Standard specified by NIST FIPS-197. BLOWFISH: the Blowfish algorithm defined by Bruce Schneier. DES: the Data Encryption Standard algorithm defined by NIST FIPS-46-3. DESEDE: the "Triple DES" algorithm defined by NIST ...