JuliaGraphs / JuliaGraphs/Graphs.jl

More performant `rem_vertices!`

Open
#243 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
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

https://github.com/JuliaGraphs/Graphs.jl/blob/53cb58152a19ca62fa01439300948c30035dca6a/src/SimpleGraphs/simpledigraph.jl#L470

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.