bitwalker / libgraph

A graph data structure library for Elixir projects
MIT License
524 stars 75 forks source link

remove vertex labels #22

Closed ToadJamb closed 5 years ago

ToadJamb commented 6 years ago

There does not seem to be a way to remove/reset/overwrite vertex labels. The only method that appears to be an option is Graph.label_vertex.

graph = Graph.new
graph = Graph.add_vertex(graph, :v1)
graph = Graph.label_vertex(graph, :v1, [:label1])
Graph.vertex_labels(graph, :v1) #=> [:label1]
graph = Graph.label_vertex(graph, :v1, [:label1, :label2])
Graph.vertex_labels(graph, :v1) #=> [:label1, :label1, :label2]

If label_vertex is the only method supplied, I would expect it to overwrite the labels, but it does not. It always adds a new label even if the label already exists. The most correct fix in my opinion is to update label_vertex to simply set the labels for a vertex to the ones passed in, but that may break existing code, so the next best solution may be to add a remove_vertex_labels that would remove all labels for a given vertex. Would you be willing to accept a pull request for either of these approaches and if so, do you have a preference for one over the other?

Update the behavior of Graph.label_vertex:

graph = Graph.new
graph = Graph.add_vertex(graph, :v1)

graph = Graph.label_vertex(graph, :v1, [:label1])
Graph.vertex_labels(graph, :v1) #=> [:label1]

graph = Graph.label_vertex(graph, :v1, [:label2, :label3])
Graph.vertex_labels(graph, :v1) #=> [:label2, :label3]

Add a new method (Graph.remove_vertex_labels):

graph = Graph.new
graph = Graph.add_vertex(graph, :v1)

graph = Graph.label_vertex(graph, :v1, [:label1])
Graph.vertex_labels(graph, :v1) #=> [:label1]

graph = Graph.remove_vertex_labels(graph, :v1)
Graph.vertex_labels(:v1) #=> []
semarco commented 5 years ago

👍 ran into the same "issue" ...
really, no updates since 11/2018?

bitwalker commented 5 years ago

This is addressed in master, will be published in a new release soon

georgfaust commented 1 year ago

any news on this?