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

Naive edge/vertex removal for GenericGraphs #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,79 @@ end
add_edge!{V,E}(g::GenericGraph{V,E}, e::E) = add_edge!(g, source(e, g), target(e, g), e)
add_edge!{V,E}(g::GenericGraph{V,E}, u::V, v::V) = add_edge!(g, u, v, make_edge(g, u, v))


# Ad-hoc vertex/edge removers below

# Naive general case
function remove_edge!{V,E}(g::GenericGraph{V,E}, u::V, v::V, e::E)
@assert e in g.edges && source(e, g) == u && target(e, g) == v
ei = edge_index(e, g)::Int
ui = vertex_index(u, g)::Int
vi = vertex_index(v, g)::Int

for i = 1:length(g.finclist[ui])
if g.finclist[ui][i] == e
f_index = i
break
end # if
end # for

for j = 1:length(g.binclist[vi])
if g.binclist[vi][j] == e
b_index = j
break
end # if
end # for

splice!(g.edges, ei)
splice!(g.finclist[ui], f_index)
splice!(g.binclist[vi], b_index)

if !g.is_directed
rev_e = revedge(e)
for i = 1:length(g.finclist[ui])
if g.finclist[ui][i] == rev_e
f_index = i
break
end # if
end # for

for j = 1:length(g.binclist[vi])
if g.binclist[vi][j] == rev_e
b_index = j
break
end # if
end # for

splice!(g.finclist[ui], f_index)
splice!(g.binclist[vi], b_index)
end # if
end


# Needed since edge indexing is not unique. That is, if e = edge(1, 2) is in graph g, then e != make_edge(g, 1, 2).
function remove_edge!{V,E}(g::GenericGraph{V,E}, u::V, v::V)
for edge in g.edges
if source(edge, g) == u && target(edge, g) == v
uv_edge = edge
break
end # if
end #for
remove_edge!(g, u, v, uv_edge)
end


remove_edge!{V,E}(g::GenericGraph{V,E}, e::E) = remove_edge!(g, source(e, g), target(e, g))


function remove_vertex!{V,E}(g::GenericGraph{V,E}, v::V)
@assert v in g.vertices
vi = vertex_index(v, g)
splice!(g.vertices, vi)

for edge in g.edges
if source(edge, g) == v || target(edge, g) == v
remove_edge!(g, edge)
end # if
end #for
end