algorithm

Uses of self referencing lists

Hello, I know it is possible to create a self referencing list in languages like Python: >>> my_list = [1,2] >>> my_list.append(my_list) >>> print my_list [1,2,[...]] >>> print my_list[0] 1 >>> print my_list[2] [1,2,[...]] What algorithms benefit from self referencing lists? I cannot think of one. Thanks. ...

Difference among approximatelyEqual and essentiallyEqual in The art of computer programming

I get this code snippet from some where else. According to the webmaster, the code is picked from The art of computer programming by Knuth Since I do not have a copy of that book, may I know what is the difference among the two functions? bool approximatelyEqual(float a, float b, float epsilon) { return fabs(a - b) <= ( (fabs(a) < ...

First non-repeating char in a string ? in haskell or F#

Given a sequence of char what is the most efficient way to find the first non repeating char Interested purely functional implementation haskell or F# preffered. ...

Search in a sorted array of ints which rolls over

Is there a easy way to search in an array like this? Some examples below : 5 6 7 8 19 45 21 32 40 // Rolled over at 7 th element 15 22 32 45 121 341 40 // Rolled over at 7 th element 1 22 32 45 121 341 400 // Rolled over at 0 th element ...

List all k-tuples with entries summing to n, ignoring rotations

Is there an efficient algorithm for finding all sequences of k non-negative integers that sum to n, while avoiding rotations (completely, if possible)? The order matters, but rotations are redundant for the problem I'm working on. For example, with k = 3 and n = 3, I would want to get a list like the following: (3, 0, 0), (2, 1, 0), (2...

[SOLVED] Sudoku solver keeps getting stuck for some reason...

So I had to write a program for a computer project for high school and I thought of doing a sudoko solver. The 'solve' algorithm is implemented like this:- For any points where only one element 'fits' looking at rows, columns, 3x3 set, put that number in. Do this repeatedly till it can't be done anymore. This is seen in the 'singleLeft...

Find the subsequence with largest sum of elements in an array

Hi, I recently interviewed with a company and they asked me to write an algorithm that finds the subsequence with largest sum of elements in an array. The elements in the array can be negative. Is there a O(n) solution for it? Any good solutions are very much appreciated. ...

Confused on Miller-Rabin

As an exercise for myself, I'm implementing the Miller-Rabin test. (Working through SICP). I understand Fermat's little theorem and was able to successfully implement that. The part that I'm getting tripped up on in the Miller-Rabin test is this "1 mod n" business. Isn't 1 mod n (n being some random integer) always 1? So I'm confused at ...

What type of technology do airlines use for booking tickets?

I have always been fascinated by the algorithm airlines use when we book the tickets. I am an undergraduate CS student and I am really interested in knowing how this works. For example, how does it figure out connecting flights? How does the fare allocation work? Is it all handled by a single company or each airlines uses its own system...

Microsoft Bing?

What is the name of the algorithm used in Microsoft BING Search engine? ...

How to divide players into divisions?

Lets say we have a two players game, where one player always wins (there can't be draw). The question is: How to divide n players into k divisions if we don't know anything about their skills? Each division should consist of the same number of players, and the best players players should be in first division, worst players in last divis...

Check if string has all the letters of the alphabet

What would be the best logic to check all the letters in a given string. If all the 26 letters are available in the provided string, I want to check that and perform so ops. eg. Pack my box with five dozen liquor jugs. Would using a Hash be useful? Or using a bit map? or any other way? BTW my code would be in Java. ...

Permuting All Possible (0, 1) value arrays

I am having writing an algorithm to generate all possible permutations of an array of this kind: n = length k = number of 1's in array So this means if we have k 1's we will have n-k 0's in the array. For example: n = 5; k = 3; So obviously there are 5 choose 3 possible permutations for this array because n!/(k!(n-k)! 5!/(3!2!) = (5...

Is there algorthm for 2 way 2D plane transformation based on 3 points and angle?

So I have such structure: 3 points (X, Y) BAC and knowledge that in real 3d world BAC angle is 90 degrees.So an image would look like that: And what we want to get at first is : than we wanna add some stuff like 2 parallel lines and what I need next is some formula for somehow shrinking image back to its original view but now w...

Measuring the average thickness of traces in an image

Here's the problem: I have a number of binary images composed by traces of different thickness. Below there are two images to illustrate the problem: First Image - size: 711 x 643 px Second Image - size: 930 x 951 px What I need is to measure the average thickness (in pixels) of the traces in the images. In fact, the average thick...

Fill sparse array

I have a sparse array, for example: rare = [[0,1], [2,3], [4,5], [7,8]] I want to plot a chart with these data, each pair are point coordinates. As you can see I don't have points for x=1, x=3 , x=5, x=6 I want to fill the array with the previous values, so for the above example I will get: filled = [[0,1], [1,1], [2,3], [3,3], [4,5...

Why is my algorithm for Project Euler Problem 12 so slow?

Hi, I have created solution for PE P12 in Scala but is very very slow. Can somebody can tell me why? How to optimize this? calculateDevisors() - naive approach and calculateNumberOfDivisors() - divisor function has the same speed :/ import annotation.tailrec def isPrime(number: Int): Boolean = { if (number < 2 || (number != 2 && num...

How should I filter this data?

I have a several series of data points that need to be graphed. For each graph, some points may need to be thrown out due to error. An example is the following: The circled areas are errors in the data. What I need is an algorithm to filter this data so that it eliminates the error by replacing the bad points with flat lines, like so:...

Fast algorithm for repeated calculation of percentile?

In an algorithm I have to calculate the 75th percentile of a data set whenever I add a value. Right now I am doing this: Get value x Insert x in an already sorted array at the back swap x down until the array is sorted Read the element at position array[array.size * 3/4] Point 3 is O(n), and the rest is O(1), but this is still quite ...

A* implementation always returns same value

Hi all, I seem to be either losing my mind or misimplementing the A* algorithm: Below is my code, it seems that no matter what values I enter it will always return 360. Am I missing some critical piece of information here? Also before anyone asks yes this is related to a machine learning assignment I received. public class A_...