algorithm

In place min-max tree invalidation problems

I’m trying to build a parallel implementation of a min-max search. My current approach is to materialize the tree to a small depth and then do the normal thing from each of these nodes. The simple way to do this is to compute the heuristic value for each leaf and then sweep up and compute the min/max. The problem is that it this omits a...

What integer hash function are good that accepts an integer hash key?

What integer hash function are good that accepts an integer hash key? ...

firefox cache hash key generation algorithm bug

There is a bug in Firefox (even in the new betas and in minefield releases) which prevents the caching of certain files because of the algorithm for creating a key in their cache hash. Here is a link to the source code of the function. I want to ensure that all of my site's files can be cached. However, I do not understand why their...

Extract first N unique integers from an Array

I have a large list of integers (thousands), and I want to extract the first N (in the order of 10-20) unique elements from it. Each integer in the list occurs roughly three times. Writing an algorithm to do this is trivial, but I wonder what's the most speed and memory efficient way to do it. There are some additional constraints and ...

Query about working out whether number is a power of 2

Using the classic code snippet: if (x & (x-1)) == 0 If the answer is 1, then it is false and not a power of 2. However, working on 5 (not a power of 2) and 4 results in: 0001 1111 0001 1111 0000 1111 That's 4 1s. Working on 8 and 7: 1111 1111 0111 1111 0111 1111 The 0 is first, but we have 4. In this link (http://www.exploringbi...

What's a good rate limiting algorithm?

I could use some pseudo-code, or better, Python. I am trying to implement a rate-limiting queue for a Python IRC bot, and it partially works, but if someone triggers less messages than the limit (e.g., rate limit is 5 messages per 8 seconds, and the person triggers only 4), and the next trigger is over the 8 seconds (e.g., 16 seconds la...

What is the best algorithm for arbitrary delimiter/escape character processing?

I'm a little surprised that there isn't some information on this on the web, and I keep finding that the problem is a little stickier than I thought. Here's the rules: You are starting with delimited/escaped data to split into an array. The delimiter is one arbitrary character The escape character is one arbitrary character Both the d...

Algorithm Question: Need to dynamically INCREMENT from 00FF00 to FF0000 over TIME, C#/Java

Hey all, I want to change the color Bright Green to Dark Red over time (240 hours). The best way I can see is to change the hex combo from 00FF00 to FF0000. I don't know how to dynamically count up to FF0000 from 00FF00 for the life of me. I'm looking over a 10 day period, so most likely over 240 hours to increment. Can anyone help ...

XML Versioning Algorithm

I'm looking for an efficient way to compare and get differences between two XML-based parse trees. What would you suggest to be the best way to store those differences? I would have done this: XML A: <w:p> <w:pPr> <w:spacing w:after="1"/> </w:pPr> <w:r> <w:t>World</w:t> </w:r> </w:p> XML B: <w:p> <w:pPr> <w:s...

Keeping top 5 values in PHP efficently

I'm writing a small algorithm in PHP that goes through n number of movies with ratings, and will store the top 5. I'm not reading from a datafile, but from a stream so I cannot simply order the movies by rating. My question is what is the most efficent way to keep track of the top 5 rated movies as I read the stream? Currently I do the ...

What are some hints that an algorithm should parallelized?

My experience thus far has shown me that even with multi-core processors, parallelizing an algorithm won't always speed it up noticably. In fact, sometimes it can slow things down. What are some good hints that an algorithm can be sped up significantly by being parallelized? (Of course given the caveats with premature optimization...

skew matrix algorithm

I'm looking for skew algorithm, just like on photoshop, edit->transform->skew is there any simple matrix which could do that? what I've seen so far was basic skew matrix (shear) but its lack of control point, doesn't like on photoshop which have at least 4 points on each corner of rectangle and we can move each control point freely. I ...

Optimize Binary Search Algorithm

In a binary search, we have two comparisons one for greater than and other for less than, otherwise its the mid value. How would you optimize so that we need to check only once? bool binSearch(int array[], int key, int left, int right) { mid = left + (right-left)/2; if (key < array[mid]) return binSearch(array, key, lef...

Searching a datastore for related topics by keyword

For example, how does StackOverflow decide other questions are similar? When I typed in the question above and then tabbed to this memo control I saw a list of existing questions which might be the same as the one I am asking. What technique is used to find similar questions? ...

What's a good data structure for building equivalence classes on nodes of a tree?

I'm looking for a good data structure to build equivalence classes on nodes of a tree. In an ideal structure, the following operations should be fast (O(1)/O(n) as appropriate) and easy (no paragraphs of mystery code): (A) Walk the tree from the root; on each node --> child transition enumerate all the equivalent versions of the child...

Calculating "Kevin Bacon" Numbers

I've been playing around with some things and thought up the idea of trying to figure out Kevin Bacon numbers. I have data for a site that for this purpose we can consider a social network. Let's pretend that it's Facebook (for simplification of discussion). I have people and I have a list of their friends, so I have the connections betw...

What's your take on this little input processing task?

I came across a little problem that looks very easy but requires a bit more thought than I first assumed. It can also be solved in many different ways and, I think, would be a perfect interview question. So what solution would you write down? Your input is a stream of pairs (x, y), with each x and y on a separate line. A short example, ...

n-dimensional matching algorithm

Hi all, Looking for some advice here. Does anyone know a good place to start looking into matching algorithm in a n-dimensional space. For example, any dating site out there must be using some sort of algorithm to match 2 people. What I have read is that we can map characteristics of a person in a n-dimensional array with a point syst...

How to find all possible subsets of a given array?

I want to extract all possible sub-sets of an array in C# or C++ and then calculate the sum of all the sub-set arrays' respective elements to check how many of them are equal to a given number. What I am looking for is the algorithm. I do understand the logic here but I have not been able to implement this one by now. :s ...

Finding Strings Neighbors By Up To 2 Differing Positions

Hi all, Given a seed string, I want to find its neighbors with at most differ in 2 positions. All the digits involve in generating string are only four (i.e. 0,1,2,3). This is the example for what I mean: # In this example, 'first' column # are neighbors with only 1 position differ. # The rest of the columns are 2 positions differ See...