algorithm

Sum of digits in C#

What's the fastest and easiest to read implementation of calculating the sum of digits? I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21 ...

Size Algorithm

Hi, Ok here's a little problem I would love to get some help on. I have a view and the viewport size will vary based on user screen resolution. The viewport needs to contain N boxes which are lined up next to each other from right to left and take up all of the horizontal space in the viewport. Now if all the boxes could be the same si...

Should I use recursion or memoization for an algorithm?

If I have a choice to use recursion or memoization to solve a problem which should I use? In other words if they are both viable solutions in that they give the correct output and can be reasonably expressed in the code I'm using, when would I use one over the other? ...

How do I group objects in a set by proximity?

I have a set containing thousands of addresses. Assuming I can get the longitude and latitude of each address, how would I go about splitting the set into groups by proximity? Further, I may want to retry the 'clustering' according to different rules: N groups M addresses per group maximum distance between any address in a group Ad...

What's a good weighting function?

I'm trying to perform some calculations on a non-directed, cyclic, weighted graph, and I'm looking for a good function to calculate an aggregate weight. Each edge has a distance value in the range [1,∞). The algorithm should give greater importance to lower distances (it should be monotonically decreasing), and it should assign the val...

Reduce Permutation

I need an algorithm that can map the runs in a permutation to a single number, but also reduce the subsequent numbers. So a run is a sequential set of numbers in a permutation that is sorted and in-order. In the list, 1;2;3;5;6;4 there are two runs, 1;2;3 and 5;6. I want to replace these with a single number, a minimum, so if, after re...

Finding the next in round-robin scheduling by bit twiddling

Consider the following problem. You have a bit-string that represents the current scheduled slave in one-hot encoding. For example, "00000100" (with the leftmost bit being #7 and rightmost #0) means that slave #2 is scheduled. Now, I want to pick the next scheduled slave in a round-robin scheduling scheme, with a twist. I have a "reque...

Algorithm to filter/normalise bad signal

Working on a tracking application using GPS. It is all fine, but sometimes because of the closed areas or bad weather I get inaccurate points. When you plot them, it just doesn't look right, with lots of hops/jumps. What algorithm should I run to filter out the bad signals It looks like an application of a blurring algorithm to me, but...

What is the best way to sort a partially ordered list?

Probably best illustrated with a small example. Given the relations A < B < C A < P < Q Correct outputs would be ABCPQ or APQBC or APBCQ ... etc. In other words, any ordering is valid in which the given relationships hold. I am most interested in the solution that is easiest to implement, but the best O(n) in speed and time is int...

Programmatically obtaining Big-O efficiency of code

I wonder whether there is any automatic way of determining (at least roughly) the Big-O time complexity of a given function? If I graphed an O(n) function vs. an O(n lg n) function I think I would be able to visually ascertain which is which; I'm thinking there must be some heuristic solution which enables this to be done automatically....

Code to calculate "median of five" in C#

Note: Please don't interpret this as "homework question." This is just a thing I curious to know :) The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons. What is the best way to implement this "median of five using 6 comparisons" in C# ? All of my attempts seem t...

What's a good way to start learning about Data Structures & Algorithms?

I would like to learn more about Data Structures & Algorithms. Can anyone suggest a good starting point? ...

Effective method to hide email from spam bots

Hi. On my home page I'm using next method to hide my email from spam bots: <a href="admin [at] example.com" rel="nofollow" onclick="this.href='mailto:' + 'admin' + '@' + 'example.com'">Contact me</a> What do you think about it? Is it effective? What other methods do you know or use? ...

Strategy to find your best route via Public Transportation only?

Finding routes for a car is pretty easy: you store a weighted graph of all the roads and you could use Djikstra's algorithm [1]. A bus route is less obvious. With a bus you have to represent things like "wait 10 minutes for the next bus" or "walk one block to another bus stop" and feed those into your pathfinding algorithm. It's not eve...

How to calculate permutations in linear time, with a twist

I have a resource scheduling issue in Java where things need to be sequenced, but there are restrictions on what resources can be next to each other. A good analogy is a string of "digits", where only certain digits can be next to each other. My solution was recursive, and works fine for small strings, but run time is O(X^N), where X i...

An algorithm to "spacify" CamelCased strings

Pretty basic, I'm just curious how others might implement this algorithm and would like to see if there are any clever tricks to optimize the algorithm...I just had to implement this for a project that I am working on. Given a string in CamelCase, how would you go about "spacifying" it? e.g. given FooBarGork I want Foo Bar Gork back. ...

What are some good strategies for determining block size in a deflate algorithm?

Hello all, I'm writing a compression library as a little side project, and I'm far enough along (My library can extract any standard gzip file, as well as produce compliant (but certainly not yet optimal) gzip output) that it's time to figure out a meaningful block termination strategy. Currently, I just cut the blocks off after every...

Image rotation algorithm

I'm looking for an algorithm that rotates an image by some degrees (input). public Image rotateImage(Image image, int degrees) (Image instances could be replaced with int[] containing each pixel RGB values, My problem is that i need to implement it for a JavaME MIDP 2.0 project so i must use code runnable on JVM prior to version 1.5 C...

Levenshtein algorithm: How do I meet this text editing requirements?

Hi, I'm using levenshtein algorithm to meet these requirements: When finding a word of N characters, the words to suggest as correction in my dictionary database are: Every dictionary word of N characters that has 1 character of difference with the found word. Example: found word:bearn, dictionary word: bears Every dictionary word ...

Algorithm for drawing an anti-aliased circle?

What's a good algorithm for drawing anti-aliased circles? (Filled and not filled.) ...