algorithm

How do you iterate over a tree ?

Hello, What is you preferred method of traversing a tree data structure, since recursive method calls can be pretty inefficient in some circumstances, I am simply using a generator like the one above. Do you have any hints to make it faster ? def children(self): stack = [self.entities] while stack: for e in stack.pop()...

How do I divide an ordered list of integers into evenly sized sublists?

Does anyone have a good algorithm for taking an ordered list of integers, i.e.: [1, 3, 6, 7, 8, 10, 11, 13, 14, 17, 19, 23, 25, 27, 28] into a given number of evenly sized ordered sublists, i.e. for 4 it will be: [1, 3, 6] [7, 8, 10, 11] [13, 14, 17, 19] [23, 25, 27, 28] The requirement being that each of the sublists are ordered and a...

How do I find all paths through a set of given nodes in a DAG?

I have a list of items (blue nodes below) which are categorized by the users of my application. The categories themselves can be grouped and categorized themselves. The resulting structure can be represented as a Directed Acyclic Graph (DAG) where the items are sinks at the bottom of the graph's topology and the top categories are sourc...

Calculate when a cron job will be executed then next time

I have a cron "time definition" 1 * * * * (every hour at xx:01) 2 5 * * * (every day at 05:02) 0 4 3 * * (every third day of the month at 04:00) * 2 * * 5 (every minute between 02:00 and 02:59 on fridays) And I have an unix timestamp. Is there an obvious way to find (calculate) the next time (after that given timestamp) the job is du...

Rosetta Stone: reservoir random sampling algorithm

I've been killing some time with a little puzzle project -- writing the algorithm described in Knuth for random sampling of a population without knowing the size of the population in advance. So far I've written it in JavaScript Rhino, Clojure, Groovy, Python, and Haskell. The basic parameters are: the function takes two arguments, a s...

Greatest linear dimension 2d set of points

Given an ordered set of 2D pixel locations (adjacent or adjacent-diagonal) that form a complete path with no repeats, how do I determine the Greatest Linear Dimension of the polygon whose perimeter is that set of pixels? (where the GLD is the greatest linear distance of any pair of points in the set) For my purposes, the obvious O(n^2) ...

Algorithm to split an array into N groups based on item index (should be something simple)

I feel that it should be something very simple and obvious but just stuck on this for the last half an hour and can't move on. All I need is to split an array of elements into N groups based on element index. For example we have an array of 30 elements [e1,e2,...e30], that has to be divided into N=3 groups like this: group1: [e1, ...,...

A packing algorithm ... kind of

Given an array of items, each of which has a value and cost, what's the best algorithm determine the items required to reach a minimum value at the minimum cost? eg: Item: Value -> Cost ------------------- A 20 -> 11 B 7 -> 5 C 1 -> 2 MinValue = 30 naive solution: A + B + C + C + C. Value: 30, Cost 22 best option: A...

Recursion algorithm to generate sitemap

I've got a DataTable containing a sitemap hierarchy with the following columns: ItemId ParentId Name Url I need to generate a set of nested lists in HTML (left the anchor elements out for clarity): <ul> <li>Item 1</li> <li>Item 2</li> <ul> <li>Sub Item 1</li> <li class="current">Sub Item 2</li> </ul> <li>Item 3</li> ...

Sources for visual explanations?

Does anyone has sources for visual explanations of algorithms (or maths) that is even more expressive, more intuitive, maybe aesthetically appealing ? Or, animations of algorithms? ...

Mediums to try out algorithms?

By definition algorithms are independent from the medium they run on. E.g. I use Excel tables to play with data structures and to do some move/shuffling/marking experiments before implementing an algorithm in a programming language. What tools and techniques do you use to design and simulate the function of an algorithm? How do you use...

What are some good websites for programming puzzles?

Programing puzzles can be a great way to practice your skills and kill time between projects. What sources do you use for programing puzzles? ...

Determine Whether Two Date Ranges Overlap

Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges overlap? As an example, suppose we have ranges denoted by DateTime variables StartDate1 to EndDate1 and StartDate2 to EndDate2. ...

Choosing an attractive linear scale for a graph's Y Axis

I'm writing a bit of code to display a bar (or line) graph in our software. Everything's going fine. The thing that's got me stumped is labeling the Y axis. The caller can tell me how finely they want the Y scale labeled, but I seem to be stuck on exactly what to label them in an "attractive" kind of way. I can't describe "attractive...

Blogs to freshen up my math (in practice)

My question, his question, but blogs as resources to be specific. I find blogs great to keep up to date... refresh material... So do you know any blogs who tackle math-related programming problems... ...

A loop to create neighbor nodes in 3d space

Hi everybody, I want to create the 26 neighbors of a cubic-voxel-node in 3-d space. The inputs are the x,y,z position of the node and the size of the cube side . I am trying to do this using a for loop but haven't managed yet. I am quite newbie in programming please help me. ...

Can I optimize code that has 3 for loops and 4 ifs?

Hi everybody i made another post here where I asked how to create the 26 neighbors of a cubic-voxel-node in 3-d space. I got a very good answer and implemented it. To that I added some MIN MAX Position checking. I would like to know if there is way, in relationship to the 3 for loops and 4 if used, to improve the execution time of t...

Image Classification Algorithms Using Java

My goal is to implements different image classification methods to show how they function and the advantages and disadvantages behind such methods. The ones I want to try and implement using Java include; Minimum distance classifier k-nearest neighbour classifier. I was wondering what can be used to accomplish my task that already ex...

Real world implementations of "classical algorithms"

I wonder how many of you have implemented one of computer science's "classical algorithms" like Dijkstra's algorithm or data structures (e.g. binary search trees) in a real world, not academic project? Is there a benefit to our dayjobs in knowing these algorithms and data structures when there are tons of libraries, frameworks and APIs ...

How does "Find Nearest Locations" work?

Nowadays most of the Restaurants and other businesses have a "Find Locations" functionality on their websites which lists nearest locations for a given address/Zip. How is this implemented? Matching the zipcode against the DB is a simple no-brainer way to do but may not always work, for example there may be a branch closer to the given l...