vasturiano / force-graph

Force-directed graph rendered on HTML5 canvas
https://vasturiano.github.io/force-graph/example/directional-links-particles/
MIT License
1.6k stars 248 forks source link

Support for Adjacency List as argument to graphData #110

Open subhamX opened 4 years ago

subhamX commented 4 years ago

Problem Currently, the schema which is supported by the graphData is { nodes: [], links: [] }. Mostly client does a lot of computations using Adjacency Matrix {nodes: [ links: [], metadata:{} ]}. And just to render this graph converts the Adjacency Matrix to the above schema. And all this is done on client's browser. For example: A graph with N nodes and E edges or links. A full traversal takes O((N+1)*E). Which is not at all good for a very dense graph with a lot of nodes.

Solution It would be better to give users an option (say a flag) to send data either in using the current schema ({ nodes: [], links: [] }) or the adjacency list schema ({nodes: [ links: [], metadata:{} ]}). For backward compatibility, we should have the current format too.

What're you thoughts on this?

vasturiano commented 4 years ago

@subhamX thanks for your suggestion!

The schema is inspired by the format used in d3-force.

The rationale for the current schema is that it should be as close to the format that needs to be handed over to the force engine as possible, without having to do further transformation which may add performance overhead. Another way to think of it is that if the lib offered the adjacency matrix option, it would not yield much performance gain because the transformation would still need to be done internally.

So, the motivation that remains is the convenience of being able to input data in the adjacency matrix format. While this could be useful, for the sake of API simplicity I think it's better to have this conversion module externally. Code-wise is relatively easy to convert between the two formats, even maintaining node instance references, therefore I leave it up to the consumer to opt for the preferred way to maintain their state.

subhamX commented 4 years ago

Yes, you are right it's easy to convert from one form to other. But as the number of nodes and edges increases the complexity of performing this transformation also increases. Simple algorithms like BFS, DFS etc implementation using Adjancency List is very easy and efficient, that's why I proposed this feature.

vasturiano commented 4 years ago

@subhamX I understand. But you can keep on performing your traversal algorithms in Adjacency List syntax. Just convert the data format before you feed it to the force-graph module.