algorithm

Boost Graph Library: Is there a neat algorithm built into BGL for community detection?

Anybody out there using BGL for large production servers? How many node does your network consist of? How do you handle community detection Does BGL have any cool ways to detect communities? Sometimes two communities might be linked together by one or two edges, but these edges are not reliable and can fade away. Sometimes there ...

What is a data structure kind of like a hash table, but infrequently-used keys are deleted?

I am looking for a data structure that operates similar to a hash table, but where the table has a size limit. When the number of items in the hash reaches the size limit, a culling function should be called to get rid of the least-retrieved key/value pairs in the table. Here's some pseudocode of what I'm working on: class MyClass { ...

What is the best solution for the 'Students and Lockers' problem?

I thought that it would be fun to present some classic CS problems and let people show their algorithm optimization skills. The hope is that we get to see some clever techniques to solve abstract problems that we may be able to implement in practice. Ideally solutions would be presented in pseudo code with a big O classification. Proof ...

Secret santa algorithm.

Every Christmas we draw names for gift exchanges in my family. This usually involves mulitple redraws until no one has pulled their spouse. So this year I coded up my own name drawing app that takes in a bunch of names, a bunch of disallowed pairings, and sends off an email to everyone with their chosen giftee. Right now, the algorith...

How does the fill operation work in paint applications?

All paint programs, independent of how simple or complex they are, come with a fill tool. This basically replaces the color of a closed region with another color. I know that there are different APIs to do this, but I am interested in the algorithm. What would be an efficient algorithm to implement this tool? A couple of things I can th...

How do I calculate the week number given a date?

If I have a date, how do I calculate the week number for that date within that year? For example, in 2008, January 1st to January 6th are in week 1 and January 7th to the 13th are in week 2, so if my date was January 10th 2008, my week number would be 2. An algorithm would be great to get me started and sample code would also help - I'...

Better way to implement count_permutations?

I need a function count_permutations() that returns the number of permutations of a given range. Assuming that the range is allowed to be modified, and starts at the first permutation, I could naively implement this as repeated calls to next_permutation() as below: template<class Ret, class Iter> Ret count_permutations(Iter first, Iter ...

What is a bubble sort good for?

Do bubble sorts have any real world use? Every time I see one mentioned, it's always either: A sorting algorithm to learn with. An example of a sorting algorithm not to use. ...

How to test if an array contains a pair of numbers whose product is odd?

How can I write a function that takes an array of integers and returns true if their exists a pair of numbers whose product is odd? What are the properties of odd integers? And of course, how do you write this function in Java? Also, maybe a short explanation of how you went about formulating an algorithm for the actual implementation. ...

Date 'wrap' in subtracting months

What is a mathematical way of of saying 1 - 1 = 12 for a month calculation? Adding is easy, 12 + 1 % 12 = 1, but subtraction introduces 0, stuffing things up. My actual requirement is x = x + d, where x must always be between 1 and 12 before and after the summing, and d any unsigned integer. ...

Best compression algorithm? (see below for definition of best)

I want to store a list of the following tuples in a compressed format and I was wondering which algorithm gives me smallest compressed size fastest de/compression tradeoff optimum ("knee" of the tradeoff curve) My data looks like this: (<int>, <int>, <double>), (<int>, <int>, <double>), ... (<int>, <int>, <double>) One of the tw...

ASP.NET Templating

We're building a text templating engine out of a custom HttpModule that replaces tags in our HTML with whole sections of text from an XML file. Currently the XML file is loaded into memory as a string/string Dictionary so that the lookup/replace done by the HttpModule can be performed very quickly using a regex. We're looking to expand...

What is the most elegant way of bubble-sorting in F#?

What is the most elegant way of bubble-sorting in F#? UPDATE As pointed out in one of the answers, bubble sorting isn't efficient in a functional language to begin with. A humourously-cynical commenter also pointed out that bubble sorting is only appropriate when the list is small and it's almost sorted anyway. However, I'm curious t...

Best Algorithm for key/value pair where key is an int64 in Delphi, pre Delphi 2009?

I need an algorithm to store a key/value pair, where the key is an Int64. I'm currently using a sorted IntList (same as a TStringList, but stores int64s). This gives me O(log n) for search, Insert and delete operations. Since I don't ever need the items sorted, this is a little inefficient. I need some kind of hashtable for O(1) operatio...

How to best sort a portion of a circular buffer?

I have a circular, statically allocated buffer in C, which I'm using as a queue for a depth breadth first search. I'd like have the top N elements in the queue sorted. It would be easy to just use a regular qsort() - except it's a circular buffer, and the top N elements might wrap around. I could, of course, write my own sorting implemen...

C# Finding Relevant Document Snippets for Search Result Display

In developing search for a site I am building, I decided to go the cheap and quick way and use Microsoft Sql Server's Full Text Search engine instead of something more robust like Lucene.Net. One of the features I would like to have, though, is google-esque relevant document snippets. I quickly found determining "relevant" snippets is m...

What's the Hi/Lo algorithm?

What's the Hi/Lo algorithm? I've found this in the NHibernate documentation (it's one method to generate unique keys, section 5.1.4.2), but I haven't found any good explanation of how does it work. I know that Nhibernate handles it, and I don't need to know the inside, but I'm just curious. ...

How can I split a C# string when a member of the array may consist of more than one word?

Hi, I am working on a small project to take a CSV file and then insert its data into a HTML table (I would use datagrid and dataset or datatable, but the system I will be talking to does not support ASP.NET uploads for sending newsletters). Anyway, I will use the file.readalllines method to return the contents of the csv file into a st...

Choosing buffer size for FTP and HTTP transfers

Hello, How to choose the size of buffer (bytes that I read from or write to socket) for the maximum throughput when I'm implementing a low-level HTTP and FTP transfer? My application should transfer data with HTTP or FTP on connections varying from 130 Kbps to 3 Mbps (I know expected speed beforehand). Sometimes it's one way transfer, s...

Ways to hash a numeric vector?

Are there any known hash algorithms which input a vector of int's and output a single int that work similarly to an inner product? In other words, I am thinking about a hash algorithm that might look like this in C++: // For simplicity, I'm not worrying about overflow, and assuming |v| < 7. int HashVector(const vector<int>& v) { cons...