algorithm

Creating multiple numbers with certain number of bits set

Problem I need to create 32 Bit numbers (signed or unsigned doesn't matter, the highest bit will never be set anyway) and each number must have a given number of Bits set. Naive Solution The easiest solution is of course to start with the number of zero. Within a loop the number is now increased by one, the number of Bits is counted, ...

Find the number of internal angles of a polygon, bigger than 180º

How can I find the number of internal angles of a polygon, bigger than 180º, having only the vertices of the polygon? For each vertex I want always the internal angle, not the external. Thanks from Brazil. ...

C# Large Tree Iteration

I have a large result set assembled in a parent/child relationship. I need to walk the tree and display the results to the user. I've done this before using recursion, but because my result set may be large, I want to avoid the possibility of receiving a StackOverflowException. I found the following example on MSDN which uses a Stac...

complexity of algorithm

Hi All, I would have quite general question. Have you ever had to really compute(e.g on the paper) complexity of an algorithm except at school as a programmer? And if.. can you give me an example please. thank you :) ...

Genetic algorithm resource

Lately I'm interested in a topic of genetic algorithms, but i couldn't find any good resource. If you know any good resource, book or a site i would appreciate it. I have solid knowledge of algorithms and A.I. but im looking for something with good introduction in genetic programing. ...

Recommendations for discrete math resources

Hi, I recently Graduated from uni in the UK but I only scraped through the Algorithmic and Mathsy units that I was made to do and am now finding that I'd actually like to know how to do some of the stuff that was taught. In particular I'm trying to follow the MIT lectures of the Algorithms lectures on Google Video. I'm finding it very ...

Ideas for converting straight quotes to curly quotes

I have a file that contains "straight" (normal, ASCII) quotes, and I'm trying to convert them to real quotation mark glyphs (“curly” quotes, U+2018 to U+201D). Since the transformation from two different quote characters into a single one has been lossy in the first place, obviously there is no way to automatically perform this conversio...

Clique problem algorithm design

One of the assignments in my algorithms class is to design an exhaustive search algorithm to solve the clique problem. That is, given a graph of size n, the algorithm is supposed to determine if there is a complete sub-graph of size k. I think I've gotten the answer, but I can't help but think it could be improved. Here's what I have:...

Algorithm for Finding Redundant Edges in a Graph or Tree

Is there an established algorithm for finding redundant edges in a graph? For example, I'd like to find that a->d and a->e are redundant, and then get rid of them, like this: => Edit: Strilanc was nice enough to read my mind for me. "Redundant" was too strong of a word, since in the example above, neither a->b or a->c is considered ...

Can you suggest a good book on graphs and graph algorithms?

Can you suggest a good book or other resource on graphs and graph algorithms? Something that will cover some basics but also advanced problems and algorithms. ...

Add or Sum of hours like 13:30+00:00:20=13:30:20 but how?

Hello coders!!! i want to add second(00:00:02) or minutes (00:00:20) on datetime value (may be stored string type) but how? Examples: 13:30+02:02:02= 15:32:02 , 13:30+00:00:01= 13:30:01 , 13:30+00:01:00=13:31:00 or 13:30(not important) how can you help me? I need your cool algoritm :) thanks again... ...

What is the best way to implement longest-prefix matching for ipv6?

An ipv6 router stores a number of routes as the first n bits of the address. In 2000, researchers found only 14 distinct prefix lengths in 1500 ipv6 routes. Incoming packets are routed to different outgoing ports based on the longest prefix match, so if the first 8 bits of packet x match an 8 bit route but the first 48 bits of the same p...

Computing the statistical mode

Hi all, I'm currently trying to verify whether or not, given an unsorted array A of length N and an integer k, whether there exists some element that occurs n/k times or more. My thinking for this problem was to compute the mode and then compare this to n/k. However, I don't know how to compute this mode quickly. My final result needs t...

Python: loop over consecutive characters?

In Python (specifically Python 3.0 but I don't think it matters), how do I easily write a loop over a sequence of characters having consecutive character codes? I want to do something like this pseudocode: for Ch from 'a' to 'z' inclusive: # f(Ch) Example: how about a nice "pythonic" version of the following? def Pangram(Str): ...

Algorithm to determine an objects neighbor

I vaguely remember reading about a programming exercise where objects are drawn on the screen. If an object has less than 2 neighbors, it dies because it is lonely, if it has more than 3 it dies because it is crowded. If the amount of neighbors is 2 or 3 then it will spawn children. the general idea was to see how many generations cou...

Best way to solve a distance problem

There are a set of point that are collinear. The problem is to add a new point that lies in the same line so that sum of distance from the new point to existing points is minimum. (Assume that the point lies in a horizontal line). The solution I thought of is: Sort the points according to their x-coordinates (y does not matter anyway)...

Generate sequence of integers in random order without constructing the whole list upfront

How can I generate the list of integers from 1 to N but in a random order, without ever constructing the whole list in memory? (To be clear: Each number in the generated list must only appear once, so it must be the equivalent to creating the whole list in memory first, then shuffling.) This has been determined to be a duplicate of thi...

What's wrong with my soft-shadow code?

I'm trying to write a simple raytracer as a hobby project and it's all working fine now, except I can't get soft-shadows to work at all. My idea of soft-shadows is that the lightsource is considered to have a location and a radius. To do a shadow test on this light I take the point where the primary ray hit an object in the scene and cas...

How can I do efficient range searching + counting with latitude/longitude data?

I'm working with a large set of points represented by latitude/longitude pairs (the points are not necessarily unique, there could be several points in the set that are at the same location). The points are stored in a database. What I need to do is figure out a way to efficiently perform a search to get the number of points that lie wi...

Pseudorandom directory tree generation?

I'm trying to write a program which will pseudorandomly autogenerate (based on a seed value so I can re-run the same test more than once) a growing directory structure consisting of files. (this is to stress test a source control database installation) I was wondering if any of you were aware of something similar to the quasirandom "spa...