algorithm

What is wrong with the pearson algoritm from “Programming Collective Intelligence”?

This function is from the book "Programming Collective Intelligence”, and is supposed to calculate the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1. If two critics rate items very similarly the function should return 1, or close to 1. With real user data I sometimes get weird results....

Traverse Matrix in Diagonal strips

I thought this problem had a trivial solution, couple of for loops and some fancy counters, but apparently it is rather more complicated. So my question is, how would you write (in C) a function traversal of a square matrix in diagonal strips. Example: 1 2 3 4 5 6 7 8 9 Would have to be traversed in the following order: [1],[...

Perceptual Image Downsampling

So here is my problem: I have an image, that image is large (high resolution) and it needs to be small (much lower resolution). So I do the naive thing (kill every other pixel) and the result looks poor. So I try to do something more intelligent (low pass filtering using a Fourier transform and re-sampling in Fourier space) and the re...

How do I encode a 4-byte string as a single 32-bit integer?

First, a disclaimer. I'm not a CS grad nor a math major, so simplicity is important. I have a four-character string (e.g. "isoy") that I need to pass as a single 32-bit integer field. Of course at the other end, I need to decode it back to a string. The string will only contain A-Z, and case is not important, if that helps. The funny ...

Random number generation

I need a random number generation algorithm that generates a random number for a specific input. But it will generate the same number every time it gets the same input. If this kind of algorithm available in the internet or i have to build one. If exists and any one knows that please let me know. (c, c++ , java, c# or any pseudo code wil...

What is the best autocomplete/suggest algorithm,datastructure [C++/C]

We see Google, Firefox some AJAX pages shows up list of probable items while user types characters. Can someone give good algorithm,datastructure for implementing autocomplete ? ...

which sorting method is most suitable for parallel processing?

I am now looking at my old school assignment and want to find the solution of a question. Here is the question: Which sorting method is most suitable for parallel processing? Bubble sort Quick sort Merge sort Selection sort I guess quick sort (or merge sort?) is the answer. Am I correct? ...

Efficient algorithm to remove any map that is contained in another map from a collection of maps.

I have set (s) of unique maps (Java HashMaps currently) and wish to remove from it any maps that are completely contained by some other map in the set (i.e. remove m from s if m.entrySet() is a subset of n.entrySet() for some other n in s.) I have an n^2 algorithm, but it's too slow. Is there a more efficient way to do this? Edit: th...

Resources for Image Recognition

I am looking for a recommendation for an introduction to image processing algorithms (face and shape recognition, etc.) and wondered if anyone had an good recommendations, either for books, whitepapers or websites. I am starting from knowing very little about image recognition and did some maths at University (a long time ago). Any h...

Python - Simple algorithmic task on lists (standard question for a job-interview)

There are 2 input lists L and M, for example: L = ['a', 'ab', 'bba'] M = ['baa', 'aa', 'bb'] How to obtain 2 non-empty output lists U and V such that: ''.join(U) == ''.join(V)) is True, and every element of U is in L, and every element of V is in M? For example, one possible solution for the two input lists above is: U=['bba'...

Is there a O(n) algorithm to build a max-heap?

Given a array of number, is there a O(n) algorithm to build a max-heap? ...

Ideal data structure for mapping integers to integers?

I won't go into details, but I'm attempting to implement an algorithm similar to the Boyer-Moore-Horspool algorithm, only using hex color values instead of characters (i.e., there is a much greater range). Following the example on Wikipedia, I originally had this: size_t jump_table[0xFFFFFF + 1]; memset(jump_table, default_value, sizeo...

Tic Tac Toe help

I've written a tic tac toe code that fine to a point. I have Alpha-Beta Pruning working also. I've ran across a problem that I need ideas, NOT CODE. How can I choose a move that will win in 4 moves versus a move that will win in 8 moves. The problem I'm having is the branch that returns a optimal score from minimax/AB prunning will possi...

Algo for a stable 'download-time-remaining' in a download window

While displaying the download status in a window, I have information like: 1) Total file size (f) 2) Downloaded file size (f') 3) Current download speed (s) A naive time-remaining calculation would be (f-f')/(s), but this value is way-to-shaky (6m remaining / 2h remaining / 5m remaining! deja vu?! :) Would there be a calculation whi...

Creating an tree graph from a collection of objects where only parent node is known

We have a large collection of objects that we need to create a tree structure from. However, our problem is that we only know each objects parent...which makes it a bit tricky since this needs to be fast. What is the best algorithm for creating an tree graph from a collection of objects where only parent node is known? ...

Median of Medians in Java

I am trying to implement Median of Medians in Java for a method like this: Select(Comparable[] list, int pos, int colSize, int colMed) list is a list of values of which to find a specified position pos is the specified position colSize is the size of the columns that I create in the first stage colMed is the position in those columns...

What's a good algorithm to distribute points between weighted items?

I have 100 points to dole out to a set of items. Each item must receive a proportional number of points relative to others (a weight). Some items may have a weight of 0, but they must receive some points. I tried to do it by giving each item 5 points, then doling out the remaining points proportionally, but when I have 21 items, the a...

Creating a photo album; Need an algorithm for dynamically placing varied-size images

OK, so in my little album app (in Flash/AS3, but language doesn't matter), each page has a 5x4 grid of photos. However, some photos I want to elevate in prominence, so instead of occupying a 1x1 space, some of them would occupy a 2x2 space. So, if I have an array of image objects that I'm iterating, in order, to fill pages sequentially....

What's a good, simple, 2D rectangles-only collision detection algorithm?

I'm designing a collision detection game tutorial for young adults, so I want this to be as simple as possible to make it easier to explain. The requirements are very simple. The world is 2D and contains only rectangles (of arbitrary sizes). BSP and even quadtrees seems like it would be overkill (again, the emphasis is on simplicity) bu...

Minimal binary diff for similar 1000 byte blocks with static noise?

I need a minimal diff for similar 1000 byte blocks. These blocks will have at most 20% of the bits different. The flipped bits will be like radio static -- randomly flipped bits with a uniform distribution over the whole block. Here's my pseudo code using XOR and lzo compression: minimal_diff=lzo(XOR(block1,block2)) Since the blocks a...