Tree-like distribution problem (geometry, etc.)

For everything that's not in any way related to PureBasic. General chat etc...
Seymour Clufley
Addict
Addict
Posts: 1266
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Tree-like distribution problem (geometry, etc.)

Post by Seymour Clufley »

I doubt this forum will be the place to ask for a solution to this problem, but I ask if anyone can think of a more appropriate forum. Apart from the fact that the problem is very specific, I also don't intend to write the solution in PB, but in JavaScript. However, I can't just go onto some random JavaScript forum and ask about something like this - people expect syntax questions on such forums. This might be a problem for mathematicians - I don't know.

The problem is, I have a bunch of rectangles (about 50 in total) which represent linear chains of events, and I need to distribute these rectangles around a (limitless) 2D plane in the most optimal way. The rectangles are all of the same width, but of different heights. Furthermore, the chains of rectangles are occasionally inter-linked. There might be a "junction" rectangle which is linked to three, four, even five other rectangles. This is what has me completely stumped: how to distribute all of the rectangles such that the linkages are concentrated geographically, and don't sprawl all over the canvas? In other words: how to keep it tidy, and elegant?

Here is a mock-up of how the end result should look. The only difference is that the rectangles' heights will vary slightly. Note that there is a right-ward "drift", because the rectangles are prequels/sequels of each other.

Image

I am not a mathematical guy, and it's a mystery to me that, in so many of my creative projects, I somehow end up needing mathematics and geometry. I wish it didn't happen, because I hate having to ask for help as a complete "maths newbie" like this. Anyway, if anyone can think of a way to approach this problem, or a forum where I'd be best asking, please let me know.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: Tree-like distribution problem (geometry, etc.)

Post by jack »

hello Seymour Clufley, why not try http://www.mersenneforum.org/index.php, there are some sharp mathematicians there and programmers also.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Tree-like distribution problem (geometry, etc.)

Post by Tenaja »

User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Tree-like distribution problem (geometry, etc.)

Post by spikey »

Hmm, not really. Dijkstra's Algorithm finds the shortest path between two nodes in a mesh, it doesn't calculate a layout for a tree which is what I think Seymour is after.

What you want is the Reingold-Tilford algorithm. Possibly the Walker or Buchheim variation of same.
Try searching for these terms in proximity to JavaScript. I'd be most surprised if no-one has ever attempted it in JavaScript before.

But I'm afraid there is no solution that doesn't involve some maths of some form because although it might seem like a trivial issue on the surface, the problem is NP-Complete (mathematician speak for 'tricky').

Your example looks like a critical path analysis to me - is that what it is?
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Tree-like distribution problem (geometry, etc.)

Post by Danilo »

May have something to do with Graph drawing and a special Graph Layout Algorithm.

Maybe Graphviz can help you with that (write .dot files and let Graphviz generate the picture
using different algorithms (dotty, neato, etc.))

Books are available for "Graph Theory" and "Graph Algorithms".
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Tree-like distribution problem (geometry, etc.)

Post by spikey »

I found this, never used it before but it looks very promising...
http://getspringy.com
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Tree-like distribution problem (geometry, etc.)

Post by DarkDragon »

Finding the optimal layout isn't easy. I think it isn't even solvable in polynomial time, and your picture shows a graph with cycles and so on, so we cannot limit the problem somehow. I would suggest using spring embedding with multiple random initial alignments, since you will probably sometimes end up in a local force minimum which is not optimal.

[EDIT]

Short explanation: this is an iterative approach where you put springs between the nodes with a specific spring constant and then let it align itself by calculating the physics. After a while it will stand still (check movement of the nodes), but it might not be optimal. Therefore you try it multiple times with different random initial positions of the nodes and you take the result with minimal tension in the springs.

You can also see the movement in the springy.js example of the poster above me.
bye,
Daniel
Seymour Clufley
Addict
Addict
Posts: 1266
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Tree-like distribution problem (geometry, etc.)

Post by Seymour Clufley »

Thanks to all of you for replying.

Spikey's recommendation, the Springy library, seems to be exactly what I need! What a relief. Everything else in this project is being hand-coded by me, so I don't want to appear lazy, but this one part really had me stumped, so it's great to find a library to solve the problem for me.

Thanks again, everyone. I appreciate the help.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Post Reply