algorithm

Time complexity of Euclid's Algorithm

I am having difficulty deciding what the time complexity of Euclid's great common denominator algorithm is. Sample pseudo-code is: function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a The only decision I think of is, "It depends." It seems to depend on "a" and "b." But what would the big-O n...

In-place sorting algorithm for the k smallest integers in an array on n distinct integers

Is there an in-place algorithm to arrange the k smallest integers in an array of n distinct integers with 1<=k<=n? I believe counting sort can be modified for this, but I can't seem to figure out how? Any help will be appreciated. ...

Manhattan distance algorithm implementation in 8 puzzle

Could you please help me in implementing a Manhattan distance euristic algorithm in solving 8 puzzle or give a link where I can find a solution? Thanks in advance. ...

hacker news algorithm in php?

this is the hacker news ranking algorithm, which i think is a simple way of ranking things, espcially if users are voting on items, but i really dnt understand this, can this be converted to php, so i can understand it fully? ; Votes divided by the age in hours to the gravityth power. ; Would be interesting to scale gravity in a sli...

N Records Taken R at a time Algorithm

If I know I have n records, and I want to do something to a group of r records at a time (basically do stuff to n in batches because n is really large), what is the algorithm for that? Any language is OK (though I'm using PHP). The algorithm should keep in mind that in the final iteration, there may not be enough records to create a co...

How can one test time complexity "experimentally"?

Could it be done by keeping a counter to see how many iterations an algorithm goes through, or does the time duration need to be recorded? ...

Hadoop last map job stuck - Need help

Hi, I am doing some text processing using hadoop map-reduce jobs. My job is 99.2% complete and stuck on last map job. The last few lines of the map output show as below. Last time, when this problem occured, I tried printing out the key values emmited from map and noticed that one of the key is having large number of values associated...

Find tunnel 'center line'?

Hi All, I have some map files consisting of 'polylines' (each line is just a list of vertices) representing tunnels, and I want to try and find the tunnel 'center line' (shown, roughly, in red below). I've had some success in the past using Delaunay triangulation but I'd like to avoid that method as it does not (in general) allow fo...

Algorithm for filling a matrix of item, item pairs

Hey guys, I have a sort of speed dating type application (not used for dating, just a similar concept) that compares users and matches them in a round based event. Currently I am storing each user to user comparison (using cosine similarity) and then finding a round in which both users are available. My current set up works fine for sma...

Looking for some good text animation algorithms (commercial or open source)

I have a need to animate some text and don't have the time to come up with polished animation routines. I need some quality algorithms that provide source, because I will need to implement on multiple platforms. An example of what I'm looking for is the following Flash authoring product, but with source code, and preferably in C/C++, Jav...

Problems with binary search function.

Having trouble with the binary_search function listed at the top. not sure where to go with it. I'm not very familiar with binary searching. #include <iostream> #include <cstdlib> #include <fstream> using namespace std; void get_input(ifstream& fin, int a[], int size, int & array_size); void binary_search (int a[], int & array_size) ...

Derangement or what?

I was looking at the description of a derangement that states "a derangement is a permutation of the elements of a set such that none of the elements appear in their original position". But then it gave 9 derangements for a set of 4 items. That doesn't make sense to me, because I only get 4 discrete sets from 4 items. For example: 12...

Algorithm to select 1 of 2 Items Based on Probability

I'm coding a project that generates two arrays containing data. One array contains data for a specific country and the other contains data for all countries. For example, if a user from the US makes a request, we will generate two arrays with data. One with data only for the US and the other with data for worldwide, including the US. I ...

How does "paletteMap" work?

I need to port a bit of ActionScript code into Java. btw, never touched a as file in my life. So far it's been easy, because AS syntax is fairly simple. There's a piece of code that calls bmpData.paletteMap( bmpData , rect, point, rArray, gArray, bArray); //all arrays of size 256 The entire file is kind of dependent on this function ...

In C# is it a good practice to use recursive functions in algorithms?

In many functional languages using a recursion is considered to be a good practice. I think it is good because of the way compiler optimizes functional language's code. But is it a good practice to use recursion in C#, when creating an algorithm? Is it right to say in regards to C#, that recursive algorithms will result in your stack g...

Searching keywords(from a matrix) in a string(around 500 char)

Hey, basically what i am trying to do is automatically assign Tags to a user input string. Now i have 5 tags to be assigned. Each tag will have around 10 keywords. A String can only be assigned one tag. In order to assign tag to string, i need to search for words matching keywords for all the five tags. Example: TAGS: Keywords Drink...

slot machine payout calculation

I want to create a 5 reels slot machine calculation system and I'm not sure what approach to take. I understand that there is a lot of math within it, especially if I want the machine to be enjoyable to a player. Are there any tips/links for that? Was looking for info at the web but they discuss on it from the player's perspective rath...

automatically compare two series -Dissimilarity test

I have two series, series1 and series2. My aim is to find how much Series2 is different from Series1,on a bin to bin basis, (each bin represents a particular feature,) automatically/quantitatively. This image can be seen in its original size by clicking here. Series1 is the expected result. Series2 is the test/incoming series. I am pr...

Efficient random sampling of constrained n-dimensional space

I'm about to optimize a problem that is defined by n (n>=1, typically n=4) non-negative variables. This is not a n-dimensional problem since the sum of all the variables needs to be 1. The most straightforward approach would be for each x_i to scan the entire range 0<=x_i<1, and then normalizing all the values to the sum of all the x's...

Interview question: three arrays and O(N*N)

Assume we have three arrays of length N which contain arbitrary numbers of type long. Then we are given a number M (of the same type) and our mission is to pick three numbers A, B and C one from each array (in other words A should be picked from first array, B from second one and C from third) so the sum A + B + C = M. Question: could w...