algorithm

Permutations of a binary tree.

Consider a binary tree: n is a node, if n is an integer (+ a b) is a node, if a and b are nodes. We have the following three operations: (+ a b) -> (+ b a) (+ (+ a b) c) -> (+ a (+ b c)) (+ a (+ b c)) -> (+ (+ a b) c) -- (2. in reverse) I need an algorithm for giving all the possible permutations of a given tree using these opera...

Sort objects using predefined list of sorted values

I was wondering what would be the fastest way to sort an array of object in the same order as a different array. Here is an example in C#: class MyClass { public MyClass(int value) { this.value = value; } int value; public int Value { get { return value; } set { this.value = value; } ...

Summarise unlimited sequence using constant storage

Assume we're frequently sampling a particular value and want to keep statistics on the samples. The simplest approach is to store every sample so we can calculate whatever stats we want, but this requires unbounded storage. Using a constant amount of storage, we can keep track of some stats like minimum and maximum values. What else can ...

What type of game logic would this be called ?

So lets say there's a game that has a 'life bar' that consists of theoretical levels. As the user performs specific actions, depending on accuracy of their actions, the life bar grows at a corresponding speed. As it grows and goes into next levels, the criteria for desirable actions change and so the user now has to figure out what thos...

Securing AJAX Requests via GUID

I'm writing a web app that will be making requests via AJAX and would like to lock down those calls. After a little research, I am considering using some form of random token (string) to be passed back along with the request (GUID?). Here's the important parts of my algorithm: Assign a token to a JavaScript variable (generated server...

Finding Overlap among multiple intervals

Let's say I have a list of intervals (or ranges) (Eg. 10-15, 5-7, 9-12..). The problem is to find the subset of ranges that overlaps. Of course I can use Interval tree for this. The actual problem that I have is there are multiple ranges. Best explained by an example: 10-15, 5-7, 9-12 1-2, 3-6, 14-15 3-5, 9-15, 10-15 In the abov...

Factorial in C without conditionals, loops and arithmetic operators

How can I find the factorial of a number (from 1 to 10) in C, without using: loop statements like for, while, and do while; conditional operators like if and case; and arithmetic operators like + , − , * , % , /, ++, −−? FYI: I found this question in C aptitude. ...

Algorithms and data structures that are not mainstream?

There are a subset of algorithms and data structures that are very common, well-studied and very helpful. Examples of these are Topological sort, quicksort, depth-first search; on the other hand, dictionaries, trees, linked-lists and to a lesser extent red-black trees, and tries, are examples of the latter. However, there are other alg...

Is Multiplying the Inverse Better or Worse?

When dealing with double data types is multiplying by the inverse better or worse? Which way is faster? Which way uses less memory? Which way is preferred? How does MSIL handle this? SquareInches = MMSquared / 645.16 SquareInches = MMSquared * 0.0015500031000062000124000248000496 NB: 10K users will note that this is a duplica...

Longest contiguous subsequence algorithm

I have an array of "Range" objects with properties "Offset" and "Length", as shown below. Assume that it'll be sorted by "Offset" ascending. Range array contains: Offset Length Index ------- ------- ------- 100 10 0 110 2 1 112 5 2 117 3 3 3...

Depicting a graphical demo of a car lapping a track

I am planning to design an application which will provide a video of a car on a track (just like a demo of a game like RACE Pro), and depict its characteristics. For example, if the user selects a poor handling car, then a graphical demo will be shown of that car completing a lap around a modelled track with the car's behaviour in relat...

How do I find the time complexity T(n) and show that it is tightly bounded (Big Theta)?

I'm trying to figure out how to give a worst case time complexity. I'm not sure about my analysis. I have read nested for loops big O is n^2; is this correct for a for loop with a while loop inside? // A is an array of real numbers. // The size of A is n. i,j are of type int, key is // of type real. Procedure IS(A) for j = 2 to l...

Normal Distribution function

edit So based on the answers so far (thanks for taking your time) I'm getting the sense that I'm probably NOT looking for a Normal Distribution function. Perhaps I'll try to re-describe what I'm looking to do. Lets say I have an object that returns a number of 0 to 10. And that number controls "speed". However instead of 10 being t...

How to detect BPM of the song by programming

In my application, i have to detect all the songs and BPM of those songs. Detect the songs are finished now I have to detect BPM of songs. Does anyone tell me that how can I detect BPM of each song by programming in Cocoa. Thanks, Haresh. ...

I am looking for a content addressing data structure

I'm trying to design a data structure that allows efficient extraction of entries from a part of their content. Let's say I am looking for an entry that matches this: [ x 2 3 x x ] If [ 0 2 3 4 5 ] or [ 3 2 3 7 8 ] are in my data structure, they should be returned by my find function. I wrote such a data structure where I compare the...

Parsing expressions with an undefined number of arguments

I'm trying to parse a string in a self-made language into a sort of tree, e.g.: # a * b1 b2 -> c * d1 d2 -> e # f1 f2 * g should result in: # a * b1 b2 -> c * d1 d2 -> e # f1 f2 * g #, * and -> are symbols. a, b1, etc. are texts. Since the moment I know only rpn method to evaluate expressions, and my current solution...

How to find common strings among two very large files?

I have two very large files (and neither of them would fit in memory). Each file has one string (which doesn't have spaces in it and is either 99/100/101 characters long) on each line. Update: The strings are not in any sorted order. Update2: I am working with Java on Windows. Now I want to figure out the best way to find out all the s...

How many hash functions does my bloom filter need?

Wikipedia says: An empty Bloom filter is a bit array of m bits, all set to 0. There must also be k different hash functions defined, each of which maps or hashes some set element to one of the m array positions with a uniform random distribution. I read the article, but what I don't understand is how k is determined. Is it a functi...

What is the relation between merges and number of items in a in k-way merge

The question is: In a k-way merge, how many merge operation will we perform. For example: 2-way merge:2 nodes 1 merge; 3 nodes 2 merge; 4 nodes 3 merge. So we get M(n)=n-1. What the the M(n) when k is arbitrary? ...

Transaction implementation for a simple file.

Hello, I'm a part of a team writing an application for embedded systems. The application often suffers from data corruption caused by power shortage. I thought that implementing some kind of transactions would stop this from happening. One scenario would include copying the area of a file before writing to some additional storage (trans...