JuliaGraphs / JuliaGraphs/Graphs.jl
More performant `rem_vertices!`
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 538
- Forks
- 128
- Avg merge
- 10h 25m
- Merged PRs (30d)
- 2
Description
rem_vertices! is $O(|V|)$ or worse. For example
Here is another implementation. I've tested it a bit, but not thoroughly. It has different API than the current version. In fact it's impossible to replace the current implementation with an efficient one because it returns a Vector whose length is equal to $|V|$. Furthermore, the "forward" map direction (vmap below rather than ivmap) is actually very useful
I could make a PR, maybe with another name.
This uses Dictionaries, but can be changed with a couple of lines to work with Dict. The signature would have to be cleaned up. In fact it's general because I also use it with a structure outside of Graphs.
function _follow_map(dict, ind)
new1 = ind
ct = 0
loopmax = length(values(dict)) + 2
new2 = new1 # value thrown away
for i in 1:loopmax
ct += 1
new2 = get(dict, new1, new1)
new2 == new1 && break
# Following should help compress
# Dictionaries.unset!(dict, new1)
# Dictionaries.set!(dict, ind, new2)
new1 = new2
end
if ct == loopmax
@show ind, ct
throw(ErrorException("Map does not have required structure"))
end
return new2
end
_index_type(::SimpleDiGraph{IntT}) where IntT = IntT
_index_type(::StructVector{<:Node{IntT}}) where IntT = IntT
function remove_vertices!(g, vertices, remove_func!::F=Graphs.rem_vertex!) where {F}
IntT = _index_type(g)
vmap = Dictionary{IntT, IntT}()
ivmap = Dictionary{IntT, IntT}()
for v in vertices
n = Graphs.nv(g)
rv = get(vmap, v, v)
Dictionaries.unset!(vmap, v)
remove_func!(g, rv)
if rv != n # If not last vertex, then swap and pop was done
nval = get(vmap, rv, rv)
nn = _follow_map(ivmap, n) # find inv map for current last vertex
Dictionaries.set!(vmap, nn, nval)
Dictionaries.set!(ivmap, nval, nn)
end
end
return (vmap, ivmap)
end
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the current rem_vertices! implementation in src/SimpleGraphs/simpledigraph.jl at the linked location, then compare its API and return value with the proposed dictionary-based implementation. Clarify whether this should replace the existing function or introduce another name, and determine the required map directions and signature. Done means an agreed efficient API is implemented and validated for the affected graph types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- data
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100