algorithm

How to rank stories based on "controversy"?

Hi guys, I'd like to rank my stories based on "controversy" quotient. For example, reddit.com currently has "controversial" section: http://www.reddit.com/controversial/ When a story has a lot of up and a lot of down votes, it's controversial even though the total score is 0 (for example). How should I calculate this quotient score so ...

Algorithm to match preferred partners into groups of three

What's a good algorithm to solve this problem? I have three groups of people - group A, group B, and group C. There are the same number of people in each group. They each have a list of people in the other groups that they're willing to work with. I want to group all these people together in groups of 3 (one from A, one from B, and one ...

Calculating a moving average in F#

I'm still working on groking the F# thing - trying to work out how to 'think' in F# rather than just translating from other languages I know. I've recently been thinking about the cases where you don't have a 1:1 map between before and after. Cases where List.map falls down. One example of this is moving averages, where typically you w...

Sorting algorithm for a non-comparison based sort problem?

I am currently faced with a difficult sorting problem. I have a collection of events that need to be sorted against each other (a comparison sort) and against their relative position in the list. In the simplest terms I have list of events that each have a priority (integer), a duration (seconds), and an earliest occurrence time that th...

Is there a perfect algorithm for chess?

Dear Stack Overflow community, I was recently in a discussion with a non-coder person on the possibilities of chess computers. I'm not well versed in theory, but think I know enough. I argued that there could not exist a deterministic Turing machine that always won or stalemated at chess. I think that, even if you search the entire s...

Find known sub image in larger image

Does anyone know of an algorithm (or search terms / descriptions) to locate a known image within a larger image? e.g. I have an image of a single desktop window containing various buttons and areas (target). I also have code to capture a screen shot of the current desktop. I would like an algorithm that will help me find the target i...

Roulette wheel selection algorithm

Can anyone provide some pseudo code for a roulette selection function? How would I implement this: I don't really understand how to read this math notation.I want General algorithm to this. ...

please break this synchronization algorithm

P1 and P2 are processes or nodes in a cluster. f1 and f2 are their flags. Supposing strong memory model, and that both processes may be restarted at any moment, and that restarting a process clears it's flag, here's an algorithm that I came up with and couldn't yet break, but that bothers me for it's not theoretically proven and looks t...

Why does Java's hashCode() in String use 31 as a multiplier?

In Java, the hash code for a String object is computed as s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. Why is 31 used as a multiplier? I understand that the multiplier should be a relatively large prime ...

Algorithms for spacing objects visually

Hi, I am wondering if there are any well-known algorithms that I should be aware of for spacing objects visually. For instance, a LINQ to SQL diagram has many tables but automatically spaces them for readability. Is this pretty much a "place randomly and move if too close/overlap" type algorithm or is there more to this? Thanks for a...

Best way to find a point on a circle closest to a given point

Hi, Given a point (pX, pY) and a circle with a known center (cX,cY) and radius (r), what is the shortest amount of code you can come up with to find the point on the circle closest to (pX, pY) ? I've got some code kind of working but it involves converting the circle to an equation of the form (x - cX)^2 + (y - cY)^2 = r^2 (where r is ...

Sort numbers by sum algorithm

Hello all, I have a language-agnostic question about an algorithm. This comes from a (probably simple) programming challenge I read. The problem is, I'm too stupid to figure it out, and curious enough that it is bugging me. The goal is to sort a list of integers to ascending order by swapping the positions of numbers in the list. Each ...

What is the best book for learning about Algorithms?

I know what algorithms are, but I have never consciously used or created one for any of the programming that I have done. So I'd like to get a book about the subject - I'd prefer if it was in python but that's not a strict requirement. What book about algorithms helped you most to understand, use, and create algorithms? One book per a...

Algorithm to determine the "usual" cash payment amounts for a given price

You walk into a store, select several products, then go to the counter to pay your bill. The total is some amount (A). You reach into your wallet, purse, or pocket and put down some cash (P), where P >= A, and the cashier gives you change. Given the set of coins and bills that are in circulation, what are the most likely values for P? ...

A range intersection algorithm better than O(n)?

Range intersection is a simple, but non-trivial problem. Its has been answered twice already: http://stackoverflow.com/questions/224878/find-number-range-intersection http://stackoverflow.com/questions/143552/comparing-date-ranges The first solutions is O(n) and the second solution is for a database (which is less than O(n) of cours...

C++ inserting a line into a file at a specific line number

I want to be able to read from an unsorted source text file (a record in each line), and insert the line/record into a destination text file by specifying the line number where it should be inserted. It will be determined where to insert the line/record into the destination file by comparing the incoming line from the source file to the...

Determine if two rectangles overlap each other?

Hi folks, I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectangles will exist parallel to the x and the y axis, that is all of their edges will have slopes of 0 or infinity. I've tried to implement what is mentioned i...

How does the Google "Did you mean?" Algorithm work?

I've been developing an internal website for a portfolio management tool. There is a lot of text data, company names etc. I've been really impressed with some search engines ability to very quickly respond to queries with "Did you mean: xxxx". I need to be able to intelligently take a user query and respond with not only raw search re...

Algorithm: Keeping count of key/value pair in NSDictionary

Being new to Cocoa, and probably not knowing all of the potential classes available that already have this functionality neatly wrapped in an OO class, here's an algorithm inquiry. What's the best bet to count how many times a particular key occurs in an array of multiple NSDictionary instances? Essentially my data structure (in this ca...

Explaining computational complexity theory

It is said that true mastery of a subject is achieved when you can explain it simply. Unfortunately as an Electronics Engineer I lack some of the more formal aspects of computer science. Taking into consideration that there is some background in math, how would you explain computational complexity theory to the naïve? Where to start ...