anvaka / ngraph.graph

Graph data structure in JavaScript
BSD 3-Clause "New" or "Revised" License
520 stars 66 forks source link

Adds oriented version of getLink and allow people to set default orientatidness #24

Open FelixMo42 opened 4 years ago

FelixMo42 commented 4 years ago

I added an oriented param to getLink so that you can get links pointing either direction easier and makes it easier to see if two links are connected in either direction. Even if they are connected in both direction it will only return one.

Allow people to set the default oriented parameter for using getLink and forEachLinkedNode by doing createGraph({oriented: true}).

I made some unit-testing for it.

anvaka commented 4 years ago

There is an option in forEachLinkedNode() method, which allows to do oriented/non oriented traversal. Does it work for your use case?

By default getLink is oriented, and I'm somewhat reluctant to extend its API without proper use case. Very often this method lies on a hot path, and adding a few more arguments/checks while seem innocent on the surface may cause higher price for CPU intensive algorithms. If there is a general need for getting non-oriented link via getLink we can discuss it, and it seem to be well suited for the scope of https://github.com/anvaka/ngraph.graph/issues/13

What do you think?

FelixMo42 commented 4 years ago

Currently, there is no fast way to figure out if to nodes are connected in either direction. You have to call hasLink(from, to) and hasLink(to, from).