algorithm

Efficient string matching algorithm

I'm trying to build an efficient string matching algorithm. This will execute in a high-volume environment, so performance is critical. Here are my requirements: Given a domain name, i.e. www.example.com, determine if it "matches" one in a list of entries. Entries may be absolute matches, i.e. www.example.com. Entries may include wild...

Computing the difference between images

Do you guys know of any algorithms that can be used to compute difference between images? Take this webpage for example http://tineye.com/ You give it a link or upload an image and it finds similiar images. I doubt that it compares the image in question against all of them (or maybe it does). By compute I mean like what the Levenshtein...

Generating Random Number with Certain Rate

Dear all, I have the following C++ code that tried to generate a random number. The idea is we given some rate "x" and number of runs; we hope it would generate the number as many as (x * number of runs times). #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <time.h> using names...

How to get all the combinations from a UML State diagram

I have a simple UML state diagram from which I would like to get the list of all the combinations possible. Does anybody know a program or algorithm who could do this for me? ...

How can I count the unique numbers in an array without rearranging the array elements?

I am having trouble counting the unique values in an array, and I need to do so without rearranging the array elements. How can I accomplish this? ...

Are there any good / interesting analogs to regular expressions in 2d?

Are there any good (or at least interesting but flawed) analogs to regular expressions in two dimensions? In one dimension I can write something like /aaac?(bc)*b?aaa/ to quickly pull out a region of alternating bs and cs with a border of at least three as. Perhaps as importantly, I can come back a month later and see at a glance what ...

How to find the top several values from an array?

I have an array of float values and want the value and more importantly the position of the maximum four values. I built the system originally to walk through the array and find the max the usual way, by comparing the value at the current position to a recorded max-so-far, and updating a position variable when the max-so-far changes. T...

Scale image to completely fill bounding box

For instance, if I need to fill a bounding box that is 100px wide by 50px tall, the following input images would have the following behavior: 200w x 200h gets scaled down 50% and 25% gets chopped off the top and bottom. 200w x 100h gets scaled down 50% with no cropping. 100w x 200h gets is not scaled, but 75px get chopped off top and b...

Where is my one-line implementation of rot13 in JavaScript going wrong?

Code in question with syntax highlighting here: via Friendpaste rot13.js: <script> String.prototype.rot13 = rot13 = function(s) { return (s = (s) ? s : this).split('').map(function(_) { if (!_.match(/[A-Za-z]/)) return _; c = Math.floor(_.charCodeAt(0) / 97); k = (_.toLowerCase().charCodeAt(0) - 96) % 26 + 13; ...

Algorithm for clustering pictures based on date taken

Anyone know of an algorithm that will group pictures into events based on the date the picture was taken. Obviously I can group by the date, but I'd like something a little more sophisticated that would(might) be able to group pictures spanning multiple days based on the frequency over a certain timespan. Consider the following grouping...

using of std::accumulate

Need prettier solution of below example but with std::accumulate. #include <algorithm> #include <vector> #include <iostream> class Object { public: Object( double a, double b ): a_( a ), b_( b ) {} double GetA() const { return a_; } double GetB() const { return b_; } // other methods private: do...

Java: reverse sorting without keeping the order

Update: In fact, the only acceptable solution for this problem would be sorting the array ascending and then reversing it. Let S be the following sequence of events: Event | Time A | 0:00 B | 0:01 C | 0:01 D | 0:02 I have a simple Comparator to sort S, which sorts the elements according to the time value. public int ...

What's The Best Approach To Do Keyword Searching On A Text Field?

I have a database table which holds meta data about images, the field in concern is the caption field. I want users to be able to enter keywords into a textbox and have the app return a selection of images that match the keywords based on their caption. I already have the code that returns an array of the individual keywords entered by ...

Aging a dataset

For reasons I'd rather not go into, I need to filter a set of values to reduce jitter. To that end, I need to be able to average a list of numbers, with the most recent having the greatest effect, and the least recent having the smallest effect. I'm using a sample size of 10, but that could easily change at some point. Are there any r...

A simple algorithm for generating positive-semidefinite matrices

I want to generate positive random semi-definite matrices. I am looking for an algorithm or more preferably an simple implementation of the algorithm in C, matlab, java or any language. ...

What algorithms there are for failover in a distributed system?

I'm planning on making a distributed database system using a shared-nothing architecture and multiversion concurrency control. Redundancy will be achieved through asynchronous replication (it's allowed to lose some recent changes in case of a failure, as long as the data in the system remains consistent). For each database entry, one nod...

How can I implement this more efficiently

So I have a function (I'm writing this in a pseudo-functional language, I hope its clear): dampen (lr : Num, x : Num) = x + lr*(1-x) And I wish to apply this n times to a value x. I could implement it recursively: dampenN (0, lr, x) = dampen(lr, x) dampenN (n, lr, x) = dampenN(n-1, lr, dampen(x)) But there must be a way I can do i...

Detect cycles in a geneology graph during a Depth-first search

I'm loading horse genealogical data recursively. For some wrong sets of data my recursion never stops... and that is because in the data there are cycles. How can I detect those cycles to stop recursing? I thougth of while recursing mantain a hashTable with all "visited" horses. But that will find some false positives, because a horse ...

Best way to search for a saturation value in a sorted list

A question from Math Battle. This particular question was also asked to me in one of my job interviews. " A monkey has two coconuts. It is fooling around by throwing coconut down from the balconies of M-storey building. The monkey wants to know the lowest floor when coconut is broken. What is the minimal number of attempts needed to est...

Calculate Bounding box coordinates from a rotated rectangle, Picture inside.

Hello, I have the coordinates of the top left Point of a rectangle as well as its width, height and rotation from 0 to 180 and -0 to -180. I am trying to get the bounding coordinates of the actual box around the rectangle. What is a simple way of calculating the coordinates of the bounding box - min y, max y, min x, max x ? The A p...