algorithm

Durand-kerner implementation doesn't work

What's wrong with this implementation of the Durand-Kerner algorithm (here) ? def durand_kerner(poly, start=complex(.4, .9), epsilon=10**-16):#float('-inf')): roots = [] for e in xrange(poly.degree): roots.append(start ** e) while True: new = [] for i, r in enumerate(roots): new_r = r - (p...

Puzzle.. solving product of values in array X

Hello, Can you please help me solving this one? You have an unordered array X of n integers. Find the array M containing n elements where Mi is the product of all integers in X except for Xi. You may not use division. You can use extra memory. (Hint: There are solutions faster than O(n^2).) The basic ones - O(n^2) and one using divi...

Tricky Algorithm Interview Question

Possible Duplicate: Algorithm: How to tell if an array is a permutation in O(n)? I was asked this interview question at Microsoft, and it's been bugging me that I haven't been able to solve it. Does anybody have any ideas? You're passed an array with n integers in it. Each integer is between 1 and n (inclusive). These numb...

What are some good ways to calculate a score for how difference or close 2 users choices are?

For example, if it is the choice of chocolate, ice cream, donut, ..., for the order of their preference. If user 1 choose A B C D E F G H I J and user 2 chooses J A B C I G F E D H what are some good ways to calculate a score from 0 to 100 to tell how close their choices are? It has to make sense, such as if most answers are the ...

running time for print out all primes under N

int main() { int i, a[N]; // initialize the array for(i = 2; i < N; i++) a[i] = 1; for(i = 2; i < N; i++) if(a[i]) for(int j = i; j*i < N; j++) a[i*j] =0; // pirnt the primes less then N for(i = 2; i < N; i++) if(a[i]) cout << " " << i; cout << endl; } It w...

Books / tutorials on how to do object oriented analysis and design of algorithms?

There is readily available a lot on info on how to do analysis and design of standard CRUD applications, or at least, applications more geared to the traditional windows application - having a couple of windows, where you click on buttons and then things happen. In the backyard there is some sort of repository where you persist your info...

how to merge 2 deep hashes in perl

I write a sub in Perl to merge 2 hashes of the same structure; so that merge($a,$b) $a = { k1 => { sk1 => 'v1' }, k2 => { sk3 => 'v3', sk4 => 'v4' } }; $b = { k1 => { sk2 => 'v2'}, k3 => { sk5 => 'v5'} }; would result in $c = { k1 => { sk1 => 'v1', sk2 => 'v2' }, k2 => { sk3 => 'v3', sk4 => 'v4' } k3 => { sk5 => 'v5'} }; B...

Optimizing a 2 parameter distance function on line segments (ACM ICPC Regionals Elim.)

This problem is a subproblem of a problem posed in the ACM ICPC Kanpur Regionals Elimination Round: Given 2 line segments bounded by the 2D points (Pa, Pb) and (Pc, Pd) respectively, find p and q (in the range [0,1]) that minimizes the function f(p, q) = D(Px, Pa) + D(Py, Pd) + k D(Px, Py) where 2 <= k...

How can I compare two captures to see which one is louder?

Given two byte arrays of data captured from a microphone, how can I determine which one has more spikes in noise? I would assume there is an algorithm I can apply to the data, but I have no idea where to start. Getting down to it, I need to be able to determine when a baby is crying vs ambient noise in the room. If it helps, I am using...

Determining BigO of a recurrence

T (1) = c T (n) = T (n/2) + dn How would I determine BigO of this quickly? ...

Viterbi algorithm in linear time

Hey, I have a problem where given a Hidden Markov model and the states S I need to find an algorithm that returns the most probable path through the Hidden Markov model for a given sequence X in time O(|S|). I was thinking of developing a graph where I would have all the different states at different positions in X and run a shortest p...

Mergesort running time BigO

Snape’s “Unfriendly Algorithms for Wizards” textbook claims the running time of merge sort is O(n^4). Is this claim correct? Solution: Yes. This claim is technically correct, because O(n^4) only gives an upper bound for how long the algorithm takes. However, it’s an obnoxiously unhelpful answer, since the tight bound is Θ(n log n). I'm...

Efficient way to tokenize a string - C

I am trying to tokenize a string. I have a table of available tokens ordered in the form of a trie. Each token knows it has children. A simple tokens table will look like, pattern value has_children -------- ------ -------- s s-val 1 stack stack-val 0 over over-val 1 overflow ov...

C/C++/Obj-C Real-time algorithm to ascertain Note (not Pitch) from Vocal Input

I want to detect not the pitch, but the pitch class of a sung note. So, whether it is C4 or C5 is not important: they must both be detected as C. Imagine the 12 semitones arranged on a clock face, with the needle pointing to the pitch class. That's what I'm after! ideally I would like to be able to tell whether the sung note is s...

designing picture puzzle.

I am planning to develop a jigsaw puzzle game. Now I already have images and image pieces, so we don't need algorithm to cut the image in pieces. On the UI side there would be two sections First section contains the broken images in random order. Second section contains the outline of the full image. User need to drag and drop the the...

Help In Verifying Implemented Algorithms for Integers [Part 1]

Hi Guys, I have been asked to implement the following algorithms. I have done them according to my knowledge and would like you guys to help me by verifying them if am right. They are simple methods to sort arrays of integers The following code contains the methods: public class Algorithms { // Use this class to pass a compar...

Finding Nodes With Only Incoming Edges and Only Outgoing Edges in a Graph Via Perl

I have the following graph my %connections=(36=>[31,22],31=>[30],30=>[20],22=>[20,8],20=>[1],8=>[5],5=>[2],2=>[1,20]); Is there any existing algorithm that let us find node with only outgoing edges and only incoming edges. Hence given the above graph, it would yield: $node_only_incoming_edge = [36]; $node_only_outgoing_edge = [1]; ...

Math problem: how to blit correctly a "rotated" surface on another one

Hi, hope it's allowed to "crosspost" between stackexchange site... Does anyone know how to solve the following "math" problem ? http://gamedev.stackexchange.com/questions/5041/correct-blitting-2-surface-problem Thanks in advance ...

Getting the actual length of a UTF-8 encoded std::string?

my std::string is utf-8 encoded so obviously, str.length() returns the wrong result. I found this information but I'm not sure how I can use it to do this: The following byte sequences are used to represent a character. The sequence to be used depends on the UCS code number of the character: 0x00000000 - 0x00000...

Is there an algorithm to find the best set of Pairs of vertices in a weighted graph without repetition?

Is there any efficient algorithm to find the set of edges with the following properties, in a complete weighted graph with an even number of vertices. the set has the smallest, maximum edge weight for any set that meats the other criteria possible every vertex is connected to exactly one edge in the set All weights are positive dI c...