Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indexOfId and adjacency matrix #3047

Closed
danielramosg opened this issue Jul 27, 2022 · 4 comments
Closed

indexOfId and adjacency matrix #3047

danielramosg opened this issue Jul 27, 2022 · 4 comments

Comments

@danielramosg
Copy link

Description of new feature

Add a method to obtain the adjacency matrix of a graph.
Ideally, add a method to build a graph from its adjacency matrix.
Document properly the function cy.nodes().indexOfId()

Motivation for new feature

While it is not difficult to create the adjacency matrix from a cytoscape graph instance, it seems a handy method to be supported by default.

Looking for "adjacency" in the repository, it is already implemented inside other functions, for instance:
https://github.com/cytoscape/cytoscape.js/blob/e6541de603f5714f572238573ea06ef17503282b/src/collection/algorithms/page-rank.js

In that example, it is used the method cy.nodes().indexOfId("str") which gives you the index of an element with id str (in the array cy.nodes()[] ). This function works perfectly, but it is not documented in the cytoscape page.

  • Is there any reason not to document or assume that the users can use the function indexOfId() ?
  • Can we rely on the order of the nodes within the array cy.nodes()[] or is there any function that alters that ordering unexpectedly? (it is expected when one removes a node, for instance)
  • Should the users implement an assignation of an index to each node independently from the array indexing?

Describe your use case for this new feature.
The function indexOfId is needed to create the adjacency matrix, and since it exists, it would be easier to use it instead of adding another indexing.
There are many use cases for the adjacency matrix.

@maxkfranz
Copy link
Member

You have the start to a great idea here. It would make a nice extension.

Matrices are useful for calculations, and so you’d need to support one or many popular output formats (e.g. glMatrix, Math.js) so that the matrices are easily computable. Matrices are more useful when they also can support scores instead of just binary adjacency values, so that would be a great option within your extension.

You should use your own indexing scheme for the nodes in the matrix — for general reasons and also because extensions consumers will tend to want specific, custom orderings anyway.

Be clear about the features and limitations of your extension in the readme. For instance, how will you handle multigraphs in the matrices? How will you handle compound node hierarchies?

Looking forward to seeing your extension!

@danielramosg
Copy link
Author

Thanks, I am working on a small educational project about graphs from a maths point of view. We will maybe create an extension(s) with some invariants (girth, diameter...) and utils (adjacency matrix).

To change the index ordering using indexOfId(), I tried to re-order in-place the cy.nodes() array but it is not straightforward; it seems to be protected somehow. I had to make a copy, re-order, and rebuild the graph with cy.json(), which does not seem very performant. So, maybe using a custom indexing function is better, as you suggest.

To be clear: There is no plan to document the function indexOfId() in the public API?; therefore, should using it be considered unsafe?

@maxkfranz
Copy link
Member

To be clear: There is no plan to document the function indexOfId() in the public API?; therefore, should using it be considered unsafe?

There are definitely cases where it won't work -- and in confusing, surprising ways. It shouldn't be public.

It's used internally in the core library only with strict preconditions. It would probably be better if we didn't use indexOfId() internally either.

Good luck with your project. Maybe link it in #914 when it's ready, if you like.

@danielramosg
Copy link
Author

Ok, thanks Max.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants