algorithm

How Do I Choose Between a Hash Table and a Trie (Prefix Tree)?

So if I have to choose between a hash table or a prefix tree what are the discriminating factors that would lead me to choose one over the other. From my own naive point of view it seems as though using a trie has some extra overhead since it isn't stored as an array but that in terms of run time (assuming the longest key is the longest ...

How to find the records with most common tags, like the related questions in StackOverflow

We may tag a question with multiple tags in StackOverflow website, I'm wondering how to find out the most related questions with common tags. Assume we have 100 questions in a database, each question has several tags. Let's say user is browsing a specific question, and we want to make the system to display the related questions on the p...

Pattern name for flippable data structure?

I'm trying to think of a naming convention that accurately conveys what's going on within a class I'm designing. On a secondary note, I'm trying to decide between two almost-equivalent user APIs. Here's the situation: I'm building a scientific application, where one of the central data structures has three phases: 1) accumulation, 2) a...

Palindrome detection efficiency

I got curious by Jon Limjap's interview mishap and started to look for efficient ways to do palindrome detection. I checked the palindrome golf answers and it seems to me that in the answers are two algorithms only, reversing the string and checking from tail and head. def palindrome_short(s): length = len(s) for i in xrange(0,lengt...

How to Calculate Recurring Digits?

Given two integers a and b, how would I go about calculating the repeating decimal of a / b? This can be in any language; whatever it's easiest for you to express it in. ...

Binary Search in Array

How would I implement a binary search using just an array? ...

Efficient way to recursively calculate dominator tree?

I'm using the Lengauer and Tarjan algorithm with path compression to calculate the dominator tree for a graph where there are millions of nodes. The algorithm is quite complex and I have to admit I haven't taken the time to fully understand it, I'm just using it. Now I have a need to calculate the dominator trees of the direct children o...

Shortest code to calculate list min/max in .NET

I'd like something like int minIndex = list.FindMin(delegate (MyClass a, MyClass b) {returns a.CompareTo(b);}); Is there a builtin way to do this in .NET? ...

comparison sort problem

Does a comparison sort have to compare the A[i] largest and A[i+1] largest values? I think any comparison sort must, but I'm not sure. I've checked out mergesort, insertion sort, and quicksort and in each of them the A[i] largest and A[i+1] largest values have to be compared. ...

Best hashing algorithm in terms of hash collisions and performance

What would be the best hashing algorithm if we had the following priorities (in that order): Minimal hash collisions Performance It doesn't have to be secure. Basically I'm trying to create an index based on a combination of properties of some objects. All the properties are strings. Any references to c# implementations would be app...

Stacking rectangles to take as little space as possible

Hello there I have a program that will calculate the minimal area taken by fitting rectangles together. Input: Rectangles of different height and width. Output: One rectangle that contains all these rectangles. Rules: One cannot turn or roll the rectangles around and they cannot overlap. I understand that this is related or is possibl...

How to find the kth largest element in an unsorted array of length n in O(n)?

I believe there's a way to find the kth largest element in an unsorted array of length n in O(n). Or perhaps it's "expected" O(n) or something. How can we do this? Cheers! p.s. this is not for homework. ...

Should I first study algorithms or another programming language?

As a fairly novice programmer who has acquired a reasonable level of competence in C++, what should I do next to advance in general programming ability? Obviously, both learning new programming languages and studying algorithms are important, but what should take highest priority? More generally, how in-depth should my eventual study...

How does Cauchy Reed-Solomon Algorithm work?

Does anybody have any reference material that details Cauchy-Reed algorithm? Googling for Cauchy-Reed Solomon results cleversafe.org. Although they have an open sourced product based on Cauchy Reed-Solomon code, they haven't provided any material explaining how the algorithm works. ...

Resource on computing time complexity of algorithms

Is there any good resource (book, reference, web site, application...) which explains how to compute time complexity of an algorithm? Because, it is hard to make the things concrete in my mind. Sometimes it is talking about an iteration has time complexity of lg n; and then according to the another loop it becomes n.lg n; and sometimes ...

How to be good in Data Structures and Analysis?

I want to be good in Data Structures and Analysis esp. in Java. I often find myself very weak. What should I do to be good in it? Any good mental exercises? ...

fast geometric proximity predicate

I have 3 points (A, B and X) and a distance (d). I need to make a function that tests if point X is closer than distance d to any point on the line segment AB. The question is firstly, is my solution correct and then to come up with a better (faster) solution. My first pass is as follows AX = X-A BX = X-B AB = A-B // closer than...

multiplication chains that result in a constant modulo a power of 2

Is there a practical algorithm that gives "multiplication chains" To clarify, the goal is to produce a multiplication change of an arbitrary and exact length Multiplication chains of length 1 are trivial. A "multiplication chain" would be defined as 2 numbers, {start} and {multiplier}, used in code: Given a pointer to array of size ...

Skip List vs. Binary Tree

I recently came across the data structure known as a Skip list. They seem to have very similar behavior to a binary search tree... my question is - why would you ever want to use a skip list over a binary search tree? ...

Getting Random Durations within a range in C#

For a random event generator I'm writing I need a simple algorithm to generate random ranges. So, for example: I may say I want 10 random intervals, between 1/1 and 1/7, with no overlap, in the states (1,2,3) where state 1 events add up to 1 day, state 2 events add up to 2 days and state 3 events add up to the rest. Or in code: st...