From 50ce22c7c38988007606416d615daa1886803fec Mon Sep 17 00:00:00 2001 From: Ted Fujimoto Date: Sat, 3 May 2014 23:08:48 -0700 Subject: [PATCH 1/5] A naive vertex/edge implementation --- src/graph.jl | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/graph.jl b/src/graph.jl index 20443cab..216d77e0 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -99,3 +99,89 @@ 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 edges(g) && 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 + + # A desperate attempt at shaving off runtime + if vertex_type(g) == Int64 || vertex_type(g) == Float64 + f_index = binary_search(g.finclist[ui], e) + b_index = binary_search(g.binclist[vi], e) + else + 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 + end # if-else + + + 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, remove_edges=true) + @assert v in vertices(g) + vi = vertex_index(v, g) + splice!(vertices(g), vi) + + # remove all edges containing v (VERY Inefficient) + if remove_edges + for edge in edges(g) + if source(edge, g) == v || target(edge, g) == v + remove_edge!(g, edge) + end # if + end #for + end # if +end From d093e70b4def7f37be6ff999d7b53430d70db69e Mon Sep 17 00:00:00 2001 From: Ted Fujimoto Date: Sun, 4 May 2014 00:49:16 -0700 Subject: [PATCH 2/5] some changes --- src/graph.jl | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/graph.jl b/src/graph.jl index 216d77e0..6426fde2 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -109,26 +109,19 @@ function remove_edge!{V, E}(g::GenericGraph{V, E}, u::V, v::V, e::E) ui = vertex_index(u, g)::Int vi = vertex_index(v, g)::Int - # A desperate attempt at shaving off runtime - if vertex_type(g) == Int64 || vertex_type(g) == Float64 - f_index = binary_search(g.finclist[ui], e) - b_index = binary_search(g.binclist[vi], e) - else - 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 - end # if-else + 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) From 26e01d0f49dafdbd46db9f927f8cc35a63b26a4c Mon Sep 17 00:00:00 2001 From: Ted Fujimoto Date: Tue, 6 May 2014 22:11:27 -0700 Subject: [PATCH 3/5] style changes --- src/graph.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/graph.jl b/src/graph.jl index 6426fde2..afb1f778 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -103,8 +103,8 @@ add_edge!{V,E}(g::GenericGraph{V,E}, u::V, v::V) = add_edge!(g, u, v, make_edge( # 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 edges(g) && source(e, g) == u && target(e, g) == v +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 @@ -150,7 +150,7 @@ 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) +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 @@ -161,17 +161,17 @@ function remove_edge!{V, E}(g::GenericGraph{V, E}, u::V, v::V) end -remove_edge!{V, E}(g::GenericGraph{V,E}, e::E) = remove_edge!(g, source(e, g), target(e, g)) +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, remove_edges=true) - @assert v in vertices(g) +function remove_vertex!{V,E}(g::GenericGraph{V,E}, v::V, remove_edges=true) + @assert v in g.vertices vi = vertex_index(v, g) - splice!(vertices(g), vi) + splice!(g.vertices, vi) # remove all edges containing v (VERY Inefficient) if remove_edges - for edge in edges(g) + for edge in g.edges if source(edge, g) == v || target(edge, g) == v remove_edge!(g, edge) end # if From 2f1dba94d02b5cd38f0f88a03bdafc75cdf65a6d Mon Sep 17 00:00:00 2001 From: Ted Fujimoto Date: Tue, 6 May 2014 22:23:46 -0700 Subject: [PATCH 4/5] update --- src/graph.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/graph.jl b/src/graph.jl index afb1f778..9c2cfacf 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -170,6 +170,7 @@ function remove_vertex!{V,E}(g::GenericGraph{V,E}, v::V, remove_edges=true) splice!(g.vertices, vi) # remove all edges containing v (VERY Inefficient) + # Should the user even have a choice to remove edges? if remove_edges for edge in g.edges if source(edge, g) == v || target(edge, g) == v From ce74fd714fe0aa4bd02477eee8f84bdad91e6570 Mon Sep 17 00:00:00 2001 From: Ted Fujimoto Date: Thu, 8 May 2014 14:11:17 -0700 Subject: [PATCH 5/5] Changes before branching --- src/graph.jl | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/graph.jl b/src/graph.jl index 9c2cfacf..c561a21b 100644 --- a/src/graph.jl +++ b/src/graph.jl @@ -164,18 +164,14 @@ 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, remove_edges=true) +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) - # remove all edges containing v (VERY Inefficient) - # Should the user even have a choice to remove edges? - if remove_edges - for edge in g.edges - if source(edge, g) == v || target(edge, g) == v - remove_edge!(g, edge) - end # if - end #for - end # if + for edge in g.edges + if source(edge, g) == v || target(edge, g) == v + remove_edge!(g, edge) + end # if + end #for end