algorithm

Algorithm for calculating the power set

Hi! I just discovered an algorithm for finding the power set. I googled after solutions, but found none that worked any good, so I figured out one myself. But I wonder what algorithm it is, because I cannot find it on the net or in any books. I mean, does it have a name? I hardly think I'm the inventor of it. But compared to the algorit...

What is wrong with this python function from "Programming Collective Intelligence"?

This is the function in question. It calculates the Pearson correlation coefficient for p1 and p2, which is supposed to be a number between -1 and 1. When I use this with real user data, it sometimes returns a number greater than 1, like in this example: def sim_pearson(prefs,p1,p2): si={} for item in prefs[p1]: if ite...

Why is vector array doubled?

Why does the classic implementation of Vector (ArrayList for Java people) double its internal array size on each expansion instead of tripling or quadrupling it? ...

Algorithm to Find Overlapping Line Segments

n4------------------n3--------------------n2--n1 | | | | | | | P1 | | | | | | | n6--n5 | | | | n11--n10 | n17 ...

random and unique subsets generation

Lets say we have numbers from 1 to 25 and we have to choose sets of 15 numbers. The possible sets are, if i'm right 3268760. Of those 3268760 options, you have to generate say 100000 What would be the best way to generate 100000 unique and random of that subsets? Is there a way, an algorithm to do that? If not, what would be the bes...

Cheap algorithm to find measure of angle between vectors

Finding the angle between two vectors is not hard using the cosine rule. However, because I am programming for a platform with very limited resources, I would like to avoid calculations such as sqrt and arccos. Even simple divide's should be limited as much as possible. Fortunately, I do not need the angle perse, but only need some valu...

Fast stable sorting algorithm implementation in javascript

I'm looking to sort an array of about 200-300 objects, sorting on a specific key and a given order (asc/desc). The order of results must be consistent and stable. What would be the best algorithm to use, and could you provide an example of it's implementation in javascript? Thanks! ...

Bresenham line algorithm (thickness).

Hello. I was wondering if anyone knew of any algorithm to draw a line with specific thickness, based on Bresenham's line algorithm or any similar. On a second thought, I've been wondering about for each setPixel(x,y) I'd just draw a circle, e.g.: filledCircle(x,y,thickness); for every x,y but that would of course be very slow. I also ...

Non-repeating pseudo random number stream with 'clumping'

I'm looking for a method to generate a pseudorandom stream with a somewhat odd property - I want clumps of nearby numbers. The tricky part is, I can only keep a limited amount of state no matter how large the range is. There are algorithms that give a sequence of results with minimal state (linear congruence?) Clumping means that ...

Search For String Permutations In String Set

The title of this is kind of awkward; I wasn't really sure how to sum this up. I know how I can do this, I'm just not sure how to do it efficiently. Here's my problem: I have a string as input. Let's say: foo bar And I have a very large set of strings (tens of thousands). Let's say: foo, baz, bar, blah, foo bar, foo baz I...

Given a start, end, and increment value, I want an algorithm that counts up and down.

Ok, I have a control that want to fade in and out (continuously). In order to fade it I adjust the transparency value of the control in the drawing routine. So I set up a timer that runs and I adjust the m_transparency member. The timer runs and it should sweeps back and forth between the two defined values m_start, m_end. These can be 0...

Is an algorithm to judge the age of person in a photo feasible?

My friend works for a non-profit organization working to stop the illegal exploitation of minors over sites such as craigslist.org, which is one of the more popular mediums. The question is whether or not it is possible, now or in the near future, to develop an algorithm to analyze a photo of a person and return a prediction of their rel...

How would one implement Apple iTunes 'Genius' algorithm?

I've always wondered about how and what the best way to go about implementing the 'Genius' feature on iTunes. I could probably brute force it, but was just wondering if anyone had any insight. Thanks. ...

How to optimally solve the flood fill puzzle?

I like playing the puzzle game Flood-It, which can be played online at: http://floodit.appspot.com/ It's also available as an iGoogle gadget. The aim is to fill the whole board with the least number of successive flood-fills. I'm trying to write a program which can solve this puzzle optimally. What's the best way to approach this pr...

How to solve a system of inequalities?

I've reduced my problem (table layout algorithm) to the following problem: Imagine I have N variables X1, X2, ..., XN. I also have some (undetermined) number of inequalities, like: X1 >= 2 x2 + X3 >= 13 etc. Each inequalities is a sum of one or more variables, and it is always compared to a constant by using the >= operator. I cannot ...

PHP/MySQL - algorithm for "Top Rated"

So I just built a star-rating system and and trying to come up with an algorithm to list the "Top Rated" items. For simplicity, here are the columns: item_name average_rating (a decimal from 1 to 5) num_votes I'm trying to determine the "sweet spot" between number of votes and rating. For example... An item rated (4.6 / 20 votes) sh...

Map Clustering Algorithm

My current code is pretty quick, but I need to make it even faster so we can accommodate even more markers. Any suggestions? Notes: The code runs fastest when the SQL statement is ordered by marker name - which itself does a very partial job of clustering the markers (the names of markers in the same location are often, but not alway...

Algorithm to check if directional graph is Strongly connected

I am looking to check if a directional graph is Strongly connected i.e all nodes can be reached by any node.(not necessarily through direct edge). One way of doing this is running a dfs and bfs on every node and see all others are still reachable. Is there a better approach to do that? ...

Joining unordered line segments

My algorithm produces a list of (usually) several thousand line segments (all 2D) which I need to join up into large polylines. These resulting polylines might be closed or open, but they are never self-intersecting. The line segments are not directed, i.e. it might be needed to flip a line segment before it can be joined to its neighbou...

How to find kth largest number in pairwise sums like setA + setB?

Here are two integers set, say A and B and we can get another set C, in which every element is sum of element a in A and element b in B. For example, A = {1,2}, B = {3,4} and we get C = {4, 5, 6} where 4=1+3, 5=1+4=2+3, 6=2+4 Now I want to find out which number is the kth largest one in set C, for example 5 is 2nd largest one in above ...