vycke / digl

MIT License
55 stars 3 forks source link

Straight longest path #23

Open markhats opened 1 year ago

markhats commented 1 year ago

Hello

I have been experimenting with this great library and just wondered if there was any way to keep certain nodes that are on the longest path in a straight line when visualising? For example, I have a graph that looks like this:

image

However, I would quite like all the green nodes in each graph to be in a straight line. For this case, it would mean swapping nodes 2 and 8 and moving node 4 up a slot. e.g.

image

Is is possible to account for this in the algorithm or is it better done as a post-creation process?

Thanks.

vycke commented 1 year ago

By design layout was removed from the package in v1.0.0. So positioning nodes on a straight line is something that has to be done after processing the nodes. The library only gives back how nodes can be positioned in an optimized way in relation to each other so to say. It just returns in which column and which row nodes need to fall.

I am afraid there is also no automated way to get the correct order of "2" and "8". From an optimization point of view, both results are seen as equal, they are both optimal (looking at columns and rows only). Achieving this result would mean reordering optimized graphs against some rules (e.g. longest path on top, or symmetrical).

Not sure if this library is really the best place for it, but I can give it some thoughts. If you have suggestions for the implementation, I am open to it.

markhats commented 1 year ago

Thanks for the reply.

I think I'm probably wanting an optimized graph with the longest path at the vertical center, with other paths symmetrically above and below the center path. At present, the algorithm seems to always make longest path be at the bottom.

I have tried re-ordering the nodes vertically as a post-process. However, this then obviously creates lots of crossings. Where is the best place in the code to be looking in order to implement this re-ordering within the algorithm itself? If you have any thoughts as to what would need doing, it would be much appreciated.

Thanks.

vycke commented 1 year ago

In the src/index.ts the starting behavior is created. There is a function called getPathByNode() that determines all possible paths from a given node. This function by default sorts paths from longest to shortest. Building in a option for symmetry (or something like that), this would be the starting point to make changes, I believe.

One way to make a starting point creating a sorting that has the longest path in the middle, and structure all other paths around it. Another point of entry is building in options to force the longest path at the center, or remain at the top, in src/optimize.ts.

Biggest downside I see already, is that the algorithm will require more steps to come to an optimized result. In addition, this is definitely not a guarantee for the result that you are looking for.