algorithm

How to search for closest value in a lookup table?

I have a simple one dimmensional array of integer values that represent a physical set of part values I have to work with. I then calculate and ideal value mathematically. How could I write an efficient search algorithm that will find the smallest abosulte difference from my ideal value in the array? The array is predetermined and con...

Balanced layout of n items in a grid.

I have a list of n logos to display in a grid, with a maximum of 3 per row. What's an algorithm to decide how many to display per row such that the number of logos per row is as balanced as possible without using more than the minimum possible number of rows? For example: n -> number in each row 1 -> 1 2 -> 2 3 -> 3 4 -> 2, 2 5 -...

mod,prime -> inverse possible

Hi all. I was wondering if one can do the following: We have: X is a product of N-primes, thus I assume unique. C is a constant. We can assure that C is a number that is part of the N-primes or not. Whichever will work best. X mod C = Z We have Z and C and we know that X was a product of N-primes, where N is restricted lets say firs...

License key pattern detection?

This is not a real situation; please ignore legal issues that you might think apply, because they don't. Let's say I have a set of 200 known valid license keys for a hypothetical piece of software's licensing algorithm, and a license key consists of 5 sets of 5 alphanumeric case-insensitive (all uppercase) characters. Example: HXDY6-R3D...

Why DFS and not BFS for finding cycle in graphs.

Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph. ...

Point to point linear gradient?

I want to make an application that can generate point to point gradient (like Photoshop does). I'm familiar with how to generate an up to down gradient but not point to point. How is this conceptually done. Thanks ...

Algorithm for converting hierarchical flat data (w/ ParentID) into sorted flat list w/ indentation levels

I have the following structure: MyClass { guid ID guid ParentID string Name } I'd like to create an array which contains the elements in the order they should be displayed in a hierarchy (e.g. according to their "left" values), as well as a hash which maps the guid to the indentation level. For example: ID Name ParentI...

How does Photoshop (Or drawing programs) blit?

I'm getting ready to make a drawing application in Windows. I'm just wondering, do drawing programs have a memory bitmap which they lock, then set each pixel, then blit? I don't understand how Photoshop can move entire layers without lag or flicker without using hardware acceleration. Also in a program like Expression Design, I could hav...

Is there "good" PRNG generating values without hidden state?

I need some good pseudo random number generator that can be computed like a pure function from its previous output without any state hiding. Under "good" I mean: I must be able to parametrize generator in such way that running it for 2^n iterations with any parameters (or with some large subset of them) should cover all or almost all v...

Correct permutation cycle for Verhoeff algorithm

Hello, I'm implementing the Verhoeff algorithm for a check digit scheme, but there seems to be some disagreement in web sources as to which permutation cycle should form the basis of the permutation table. Wikipedia uses: (36)(01589427) while apparently, Numerical Recipies uses a different cycle and this book uses: (0)(14)(23)(56789),...

Algorithm: How to tell if an array is a permutation in O(n)?

Hello, Input: A read-only array of N elements containing integer values from 1 to N (some integer values can appear more than once!). And a memory zone of a fixed size (10, 100, 1000 etc - not depending on N). How to tell in O(n) if the array represents a permutation? --What I achieved so far (an answer proved that this was not good)...

How to map a long integer number to a N-dimensional vector of smaller integers (and fast inverse)?

Given a N-dimensional vector of small integers is there any simple way to map it with one-to-one correspondence to a large integer number? Say, we have N=3 vector space. Can we represent a vector X=[(int16)x1,(int16)x2,(int16)x3] using an integer (int48)y? The obvious answer is "Yes, we can". But the question is: "What is the fastest wa...

Robust algorithm for chromatic instrument tuner?

Who knows the most robust algorithm for a chromatic instrument tuner? I am trying to write an instrument tuner. I have tried the following two algorithms: FFT to create a welch periodogram and then detect the peak frequency A simple autocorrelation (http://en.wikipedia.org/wiki/Autocorrelation) I encountered the following basic prob...

How to Implement AutoSize

I'm trying to figure out a good way to auto-size a Rectangle that has text drawn inside of it. I basically want the size to have a ratio of width/height and then "grow" according to that ratio to fit the text. I've looked at Graphics.MeasureString but I don't think it does what I'm looking for (maybe it does and I'm just using it wrong)....

Select i th smallest element from an array

I have a divide and conquer method to find the i th smallest element from an array. Here is the code: public class rand_select{ public static int Rand_partition(int a[], int p, int q, int i) { //smallest in a[p..q] if ( p==q) return a[p]; int r=partition (a,p,q); int k=r-p+1; if (i==k) return ...

algorithm for checking addresses for matches?

I'm working on a survey program where people will be given promotional considerations the first time they fill out a survey. In a lot of scenarios, the only way we can stop people from cheating the system and getting a promotion they don't deserve is to check street address strings against each other. I was looking at using levenshtein...

Shortest Path algorithm of a different kind

Hey guys, Lets say you have a grid like this (made randomly) Now lets say you have a car starting randomly from one of the while boxes, what would be the shortest path to go through each one of the white boxes? you can visit each white box as many times as you want and cant Jump over the black boxes. The black boxes are like walls. I...

What are the advantages of a rebase over a merge in git?

In this article, the author explains rebasing with this diagram: Rebase: If you have not yet published your branch, or have clearly communicated that others should not base their work on it, you have an alternative. You can rebase your branch, where instead of merging, your commit is replaced by another commit with a di...

Longitudinal Redundancy Check fails

I have an application that decodes data from a magnetic stripe reader. But, I'm having difficulty getting my calculated LRC check byte to match the one on the cards. If I were to grab 3 cards each with 3 tracks, I would guess the algorithm below would work on 4 of the 9 tracks in those cards. The algorithm I'm using looks like this (C#)...

Rush Hour - Solving the game

Rush Hour if you're not familiar with it, the game consists of a collection of cars of varying sizes, set either horizontally or vertically, on a NxM grid that has a single exit. Each car can move forward/backward in the directions it's set in, as long as another car is not blocking it. You can never change the direction of a car. There ...