gonum / graph

Graph packages for the Go language [DEPRECATED]
249 stars 39 forks source link

Why NewNode() and AddNode() in Mutable interface? #28

Closed leoneu closed 9 years ago

leoneu commented 9 years ago

Hi. Can't think of a use case for creating a new node but not adding it to the graph. What was the reason for creating two methods for adding nodes to a graph?

UserAB1236872 commented 9 years ago

As I recall it, NewNode creates a new node and adds it, while AddNode adds an existing node. The idea is that if you're doing something with a graph, you may want more nodes than your source graph -- for instance, Johnson's Algorithm adds a dummy node to the graph.

Why NewNode instead of just AddNode? Because IDs can't conflict and the node you're adding needs to have an explicit ID. You could have something like a NextID() method, but realistically you're almost never going to use NextID without AddNode, and the overhead for creating a concrete.Node and returning it is negligible.

On Mon, Dec 15, 2014 at 1:08 PM, Leo Neumeyer notifications@github.com wrote:

Hi. Can't think of a use case for creating a new node but not adding it to the graph. What was the reason for creating two methods for adding nodes to a graph?

— Reply to this email directly or view it on GitHub https://github.com/gonum/graph/issues/28.

leoneu commented 9 years ago

I still don't see the use case, why would a Node exist without being part of the graph? Is there an example?

In mutdir.go NewNode calls AddNode.This can be confusing for the API user since it's not clear what the semantics are. I suggest we remove AddNode unless there is a compelling use case.

UserAB1236872 commented 9 years ago

Johnson's Algorithm is an example. The difference is AddNode is used to COPY a node from another graph -- since nodes are interfaces, if your implementation is a pointer type this makes the new graph contain the exact same nodes as your old graph, which is important because all our algorithms are guaranteed to return a node from YOUR graph, not merely a node with the same ID. So by using AddNode on Johnson's algorithm, by running a pathfinding algorithm, you get the user's nodes and not facsimilies. However, NewNode is also needed because Johnson's algorithm requires a dummy node that doesn't exist in the original graph to work properly.

Like I said, you can accomplish this with a NextID method and using AddNode to add a new concrete.Node yourself, and I could be persuaded to change it to that, but as of now, the only use for a NextID method would be to add a new node.

On Mon, Dec 15, 2014 at 4:53 PM, Leo Neumeyer notifications@github.com wrote:

I still don't see the use case, why would a Node exist without being part of the graph? Is there an example?

In mutdir.go NewNode calls AddNode.This can be confusing for the API user since it's not clear what the semantics are. I suggest we remove AddNode unless there is a compelling use case.

— Reply to this email directly or view it on GitHub https://github.com/gonum/graph/issues/28#issuecomment-67095264.

leoneu commented 9 years ago

I see, thanks!