algorithm

Calculating future occurences of Friday the 13th

I'd like to be able to start with a year, and calculate occurrences of Friday the 13th. A brute force solution is easy and obvious. I have something slightly better, but I have no doubt that someone else can come up with an elegant algorithm for this. Perhaps a little trickier, I'd be interested in giving the program a month, and have...

Finding all cycles in graph

How can I find (iterate over) ALL the cycles in a directed graph from/to a given node? For example, I want something like this: A->B->A A->B->C->A but not: B->C->B ...

RAR decompression algorithm

I'm looking for the description of an algorithm that can be used to decompress RAR files. I don't need to create new archives, only decompress existing ones. The Wotsit.org has the description of the RAR file format (version 2), but the description does not describe the decompression algorithm. Also, does anyone know whether RAR versio...

List all possible combinations of k integers between 1...n (n choose k)

Hi, Out of no particular reason I decided to look for an algorithm that produces all possible choices of k integers between 1...n, where the order amongst the k integer doesn't matter (the n choose k thingy). From the exact same reason, which is no reason at all, I also implemented it in C#. My question is: Do you see any mistake in ...

How do I parenthesize an expression programmatically?

I have an idea for a simple program to make that will help me with operator precedence in languages like C. The most difficult part of this is parenthesizing the expression. For example, I want this: *a.x++ = *b.x++ Converted to this: ((*(((a).(x))++)) = (*(((b).(x))++))) Which I did manually in these steps: *a.x++ = *b...

Need Better Algorithm for Finding Mapping Between 2 Sets of Points with Minimum Distance

Problem: I have two overlapping 2D shapes, A and B, each shape having the same number of pixels, but differing in shape. Some portion of the shapes are overlapping, and there are some pieces of each that are not overlapping. My goal is to move all the non-overlapping pixels in shape A to the non-overlapping pixels in shape B. Since the n...

Explanation of how this code works

I am making a seperate thread here because I believe if I write a comment, the comment won't push the thread to the top and thus there'll be notification for anyone who has written to that thread. This code was supplied by Micheal Buen at the thread http://stackoverflow.com/questions/410324/writing-text-to-the-middle-of-a-file : ...

Pre RTree step: Divide a set of points into rectangular regions each containing one point...

Hi, given my current position (lat,long) I want to quickly find the nearest neighbor in a points of interest problem. Thus I intend to use an R-Tree database, which allows for quick lookup. However, first the database must be populated - of course. Therefore, I need to determine the rectangular regions that covers the area, where each re...

Simple algebra question, for a program I'm writing.

How would I solve: -x^3 - x - 4 = 0 You can't use quadratic, because it is to the 3rd power, right? I know I it should come out to ~1.3788 but I'm not sure how I would derive that. I started with: f(x) = x + (4/(x^2 + 1)). Solving for 0, moving the x over to the other side, multiplying by (x^2 + 1) on both sides, I end up wi...

How is Amazon Faceted Search so fast?

Search for a term on amazon.com, for example "stack overflow", and the search results come back very quickly. On the left hand side of the window, there is a faceted search that shows in certain categories, the count of products that match that term. You can then drill into those terms. For example, there are 1094 books that match the...

Algorithm to find two repeated numbers in an array, without sorting

There is an array of size n (numbers are between 0->n-3) and only 2 numbers are repeated. Elements are placed randomly in the array. e.g. in {2, 3, 6, 1, 5, 4, 0, 3, 5} n=9, and repeated numbers are 3 and 5. What is the best way to find the repeated numbers? P.S. [You should not use sorting] ...

Sample the average of values received in last X seconds

I have a class that dispatches a Success and a Failure event and I need to maintain a statistic on the average number of failure/total number of events in the last X seconds from that class. I was thinking something along the lines of using a circular linked list and append a success or failure node for each event. Then count the numbe...

Heightmap generation algorithm?

I was looking around the internet and couldn't find a perfect algorithm for this particular problem: Our customer have a set of points and weight data along with each point as can be demonstrated by this image: Of which, we have a GIS program that could generate a "heightmap" or a sort of terrain data from these points and their weig...

What's the difference between an Algorithm and a Design Pattern

I was searching for "Undo/Redo algorithms" and found something marked as a duplicate, but the duplicate was a request for a "Undo Design Pattern". I'd really like an algorithm for this. I don't think I necessarily need a design pattern. Is there a fundamental difference between "Design Pattern" and "Algorithm" or is it OK that someon...

Most efficient way to see if an ArrayList contains an object in Java

I have an ArrayList of objects in Java. The objects have four fields, two of which I'd use to consider the object equal to another. I'm looking for the most efficient way, given those two fields, to see if the array contains that object. The wrench is that these classes are generated based on XSD objects, so I can't modify the classes...

What are the complicated data structures you should have heard of?

This is a derivative question, but I'm inquiring as to the data structures that you should at least be familiar with for their usefulness. These structures are too hard to implement without some expertise however. I would say a good boundary between the two is a heap -- you should be able to code a heap, but it would take you a day. N...

Regular expressions Equivalence

Is there a way to find out if two arbitrary regular expressions are equivalent? Looks like complex problem to me, but there might be some DFA simplification mechanism or something? ...

How do you construct an array suitable for numpy sorting?

I need to sort two arrays simultaneously, or rather I need to sort one of the arrays and bring the corresponding element of its associated array with it as I sort. That is if the array is [(5, 33), (4, 44), (3, 55)] and I sort by the first axis (labeled below dtype='alpha') then I want: [(3.0, 55.0) (4.0, 44.0) (5.0, 33.0)]. These are ...

How to detect subjective image quality

For a image-upload tool I want to detect the (subjective) quality of an image automatically, resulting in a rating of the quality. I have the following idea to realize this heuristically: Obviously incorporate the resolution into the rating. Compress it to JPG (75%), decompress it and compare jpg-size vs. decompressed size to gain a r...

How do I handle long comparisons in SQL

I have a pathological issue with SQL, so I usually sort all of my database issues by building quickie software applications to handle my SQL problems. (as I am also doing in this case) Thanks to StackOverflow I think I can be shamed into correctness, so I would like to learn how to make this kind of SQL troubleshooting in actual SQL o...