algorithm

Rolling median algorithm in C

I am currently working on an algorithm to implement a rolling median filter (analogous to a rolling mean filter) in C. From my search of the literature, there appear to be two reasonably efficient ways to do it. The first is to sort the initial window of values, then perform a binary search to insert the new value and remove the exiting ...

Algorithm for multidimensional optimization / root-finding / something

I have five values, A, B, C, D and E. Given the constraint A + B + C + D + E = 1, and five functions F(A), F(B), F(C), F(D), F(E), I need to solve for A through E such that F(A) = F(B) = F(C) = F(D) = F(E). What's the best algorithm/approach to use for this? I don't care if I have to write it myself, I would just like to know where to...

How to do matrix conversions by row and columns toggles?

I have got a square matrix consisting of elements either 1 or 0. An ith row toggle toggles all the ith row elements (1 becomes 0 and vice versa) and jth column toggle toggles all the jth column elements. I have got another square matrix of similar size. I want to change the initial matrix to the final matrix using the minimum number of t...

Recursive function to add sum of sequence of numbers and the sums thereof

Given a sequence of n numbers (n can be any number, assume n <= 100 for this question), say for eg. 11, 23, 9, 17, 20, 8, 5, 6 . Problem is to write a recursive function in C to add each number in the sequence to get the sum. If this sum is of more than one digit then sum the digits again and again if the sum is of more than one digit th...

Is it possible to calculate median of a list of numbers better than O(n log n)?

I know that it is possible to calculate the mean of a list of numbers in O(n). But what about the median? Is there any better algorithm than sort (O(n log n)) and lookup middle element (or mean of two middle elements if an even number of items in list)? ...

How can I create an algorithm for generating random strings?

I want to generate random strings like: sssder tvmwww 66rfdd 123123 oo007oo 1234 2020 1111 rrrr r8r8r uiuiu wewewe fefefe abced xyz.. Specifically, I want a string with a length of 5 to 8 characters that is easy to remember. Is this possible? ...

Tiling Simplex Noise?

I've been interested (as a hobbyist) in pseudo-random noise generation, specifically the Perlin and Simplex algorithms. The advantage to Simplex is speed (especially at higher dimensions), but Perlin can be tiled relatively easily. I was wondering if anyone was aware of a tiling simplex algorithm? Fixed-dimension is fine, generic is bett...

Simulating nested loops

In a beginner's programming book (free licence) there was the following code, dynamically creating nested loops in Java: import java.util.Scanner; public class RecursiveNestedLoops { public static int numberOfLoops; public static int numberOfIterations; public static int[] loops; public static void main(String[] args) { S...

Finding a set of permutations, with a constraint

I have a set of N^2 numbers and N bins. Each bin is supposed to have N numbers from the set assigned to it. The problem I am facing is finding a set of distributions that map the numbers to the bins, satisfying the constraint, that each pair of numbers can share the same bin only once. A distribution can nicely be represented by an NxN ...

Recombine Number to Equal Math Formula

Hello, I've been thinking about a math/algorithm problem and would appreciate your input on how to solve it! If I have a number (e.g. 479), I would like to recombine its digits or combination of them to a math formula that matches the original number. All digits should be used in their original order, but may be combined to numbers (he...

Algorithm for detecting repeating decimals?

Is there an algorithm for figuring out the following things? If the result of a division is a repeating decimal (in binary). If it repeats, at what digit (represented as a power of 2) does the repetition start? What digits repeat? Some examples: 1/2 = 1/10 = 0.1 // 1 = false, 2 = N/A, 3 = N/A, 4 = N/A 1/3 = 1/11 = 0.010101... // 1 =...

How Prim's algorithm time complexity is ElogV using Priority Q?

Pseudo code which i used: for all V vertices: visited[n]=0 pick any vertex r from graph and set visited[r]=1 For all edges e incident to r PQ.insert() while(PQ is not empty)//line 1 f=PQ.min()//f(r,s) is such that visited[s]=0 for all edges e(s,t) incident to s//line 2 if(visited[t]==0) PQ.insert(e);//line 3 ...

What strategies can an ORM use to cache data while minimizing complexity?

If the application asks for a similar result set to one that has recently been requested, how might the ORM keep track of which results are stale and which can be reused from before without using too many resources (memory) or creating too much architectural complexity? ...

What is an efficient algorithm to create all possible combinations?

Let's say there's n amount of entries, each of whom can take the value of 0 or 1. That means there's 2^n possible combinations of those entries. The number of entries can vary from 1 to 6. How can you create each possible combination as a sequence of numbers (i.e. for n = 2: 00, 01, 10, 11), without resorting to a thousand IFs? ...

Big Number Subtraction in C

I just finished my exam in an introductory C course about 20 minutes ago. The first question on the exam caught me somewhat off guard, and involved finding the difference two large numbers. The goal was to take two structures (N1 and N2) by value, and store the difference in a structure passed by reference (N3). We were allowed to assum...

Overlapping line segments

The following diagram illustrates a problem I have encountered creating a Manhattan diagram: Overlapping Lines The box surrounds most of the line [(tx,midy)-(sx,midy)] that has overlapped an existing line (represented by psegment in the code below). I have removed the overlapping arrow heads (and tails) and am a little stumped on how t...

Single shortest path of an acyclic undirected disconnected graph

Hi, Is there a graph algorithm that given a start(v) and an end(u) will find a shortest path through the given set of edges, but if u is a disconnected vertex, it will also determine the shortest path to add missing edges until u is no longer disconnected? I have a pixel matrix where lines are made of 255's(black) and 0's(white). lines...

Using Rabin-Karp to search for multiple patterns in a string

According to the wikipedia entry on Rabin-Karp string matching algorithm, it can be used to look for several different patterns in a string at the same time while still maintaining linear complexity. It is clear that this is easily done when all the patterns are of the same length, but I still don't get how we can preserve O(n) complexit...

stable matching

Hi all, I'm reading a algorithm book in my lesure time. Here is a question I have my own answer but not quite sure. What's your opinion? Thanks! Question: There are 2 television network company, let as assume A and B, each are planning the TV progame schedule in n time slots of a day. Each of them are putting their n programs in those ...

Algorithm get a new list containing no duplicated item by adding any 2 elements in a big array

Hi , I can only think of this naive algorithm. Any better way? C/C++, Ruby ,Haskell is OK. arry = [1,5,.....4569895] //1000000 elements ,sorted , no duplicated newArray = Hash.new for (i = 0 ; i < arry.length ;i++ ) { for (j = 0 ; j < arry.length ;j ++ ) { elem = arry[i] + arry[j] if (! new...