Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Function to delete a bunch of vextexs at once #1043

Closed
oxinabox opened this issue Oct 8, 2018 · 6 comments
Closed

Function to delete a bunch of vextexs at once #1043

oxinabox opened this issue Oct 8, 2018 · 6 comments

Comments

@oxinabox
Copy link

oxinabox commented Oct 8, 2018

rem_vertex! can remove 1 vertex.
Which is fine.

But if you want to remove more than one vertex then it is really annoying to do so.
Since the ids will all change every time you remove one.

@oxinabox
Copy link
Author

oxinabox commented Oct 8, 2018

I am currently using code like

@show inds = findall(isolated_vertexes(sog))
@show offset_inds = inds .- (0:(length(inds)-1))
sog_clean = deepcopy(sog)
for to_del in offset_inds
    rem_vertex!(sog_clean, to_del)
end

Output:

inds = findall(isolated_vertexes(sog)) = [5, 6, 8, 10, 12, 13, 14, 15, 16, 17, 18, 20, 22]
offset_inds = inds .- (0:length(inds) - 1) = [5, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 9, 10]

@simonschoelly
Copy link
Contributor

Probably not the solution you are looking for, but you could just build the subgraph of non-isolated vertices. Something like

keep = setdiff(vertices(sog), inds)
sog_clean = sog[keep]

A rem_vertices! function might still be nice - we also have an add_vertices! function

@oxinabox
Copy link
Author

oxinabox commented Oct 8, 2018

Nice, I didn't know indexing did that.

@sbromberger
Copy link
Owner

sbromberger commented Oct 8, 2018

The proper way to get rid of a bunch of vertices at once is to use induced subgraphs, as @simonschoelly shows.

There's no good/efficient way to do a rem_vertices!. Frankly, graph modification is so expensive in any case that you shouldn't be doing it too much in the first place (or you should probably look at creating a new graph structure that can handle it better.)

@simonschoelly
Copy link
Contributor

The problem with the induced subgraphs that we have right now, is that they copy the graph which for large graphs uses a lot of memory. I see a legit use case for a function rem_vertices!: A user loads or creates a large graph and then removes all the vertices that they don't need, such as isolated vertices or vertices that do not belong to the largest component.

We even could have a flag rem_vertices!( ..., preserve_order=true) , which preserves the order. This operation is very costly if we would do it for every vertex that we want to remove, but if we do it for the whole batch of vertices that should be removed, then it might be worth it.

@sbromberger
Copy link
Owner

PRs welcome.

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

No branches or pull requests

3 participants