From 9a3c848862af164d6a37f4dd7513002496c53e5c Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 11:45:40 -0500 Subject: [PATCH 1/8] Added remove_edges and add_edges functions --- src/NamedGraphs.jl | 1 + src/utilities.jl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/utilities.jl diff --git a/src/NamedGraphs.jl b/src/NamedGraphs.jl index 0f68970..02f0d82 100644 --- a/src/NamedGraphs.jl +++ b/src/NamedGraphs.jl @@ -116,6 +116,7 @@ include(joinpath("steiner_tree", "steiner_tree.jl")) include(joinpath("traversals", "dfs.jl")) include("namedgraph.jl") include(joinpath("generators", "named_staticgraphs.jl")) +include("utilities.jl") # TODO: reexport Graphs.jl (except for `Graphs.contract`) export NamedGraph, diff --git a/src/utilities.jl b/src/utilities.jl new file mode 100644 index 0000000..e6d2333 --- /dev/null +++ b/src/utilities.jl @@ -0,0 +1,31 @@ +"""Remove a list of edges from a graph g""" +function remove_edges(g::AbstractGraph, edges) + g_out = copy(g) + for e in edges + rem_edge!(g_out, e) + end + + return g_out +end + +function remove_edges!(g::AbstractGraph, edges) + for e in edges + rem_edge!(g, e) + end +end + +"""Add a list of edges to a graph g""" +function add_edges(g::AbstractGraph, edges) + g_out = copy(g) + for e in edges + add_edge!(g_out, e) + end + + return g_out +end + +function add_edges!(g::AbstractGraph, edges) + for e in edges + add_edge!(g, e) + end +end \ No newline at end of file From abc77a68ea9e0dd4f5b5902d2876325bced3bb38 Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 11:46:24 -0500 Subject: [PATCH 2/8] Grammar --- src/utilities.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utilities.jl b/src/utilities.jl index e6d2333..1d92fa6 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -1,5 +1,5 @@ """Remove a list of edges from a graph g""" -function remove_edges(g::AbstractGraph, edges) +function rem_edges(g::AbstractGraph, edges) g_out = copy(g) for e in edges rem_edge!(g_out, e) @@ -8,7 +8,7 @@ function remove_edges(g::AbstractGraph, edges) return g_out end -function remove_edges!(g::AbstractGraph, edges) +function rem_edges!(g::AbstractGraph, edges) for e in edges rem_edge!(g, e) end From 77db7b9cc1a75f25df304dcafe84ecc2aa263b93 Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 12:14:17 -0500 Subject: [PATCH 3/8] Moved Add/Rem Edges to New Location --- src/Graphs/abstractgraph.jl | 32 ++++++++++++++++++++++++++++++++ src/NamedGraphs.jl | 1 - src/utilities.jl | 31 ------------------------------- 3 files changed, 32 insertions(+), 32 deletions(-) delete mode 100644 src/utilities.jl diff --git a/src/Graphs/abstractgraph.jl b/src/Graphs/abstractgraph.jl index 971aeb6..6b38c10 100644 --- a/src/Graphs/abstractgraph.jl +++ b/src/Graphs/abstractgraph.jl @@ -379,3 +379,35 @@ function mincut_partitions(graph::AbstractGraph, distmx=weights(graph)) parts = groupfind(first(mincut(graph, distmx))) return parts[1], parts[2] end + +"""Remove a list of edges from a graph g""" +function rem_edges(g::AbstractGraph, edges) + g_out = copy(g) + for e in edges + rem_edge!(g_out, e) + end + + return g_out +end + +function rem_edges!(g::AbstractGraph, edges) + for e in edges + rem_edge!(g, e) + end +end + +"""Add a list of edges to a graph g""" +function add_edges(g::AbstractGraph, edges) + g_out = copy(g) + for e in edges + add_edge!(g_out, e) + end + + return g_out +end + +function add_edges!(g::AbstractGraph, edges) + for e in edges + add_edge!(g, e) + end +end \ No newline at end of file diff --git a/src/NamedGraphs.jl b/src/NamedGraphs.jl index 02f0d82..0f68970 100644 --- a/src/NamedGraphs.jl +++ b/src/NamedGraphs.jl @@ -116,7 +116,6 @@ include(joinpath("steiner_tree", "steiner_tree.jl")) include(joinpath("traversals", "dfs.jl")) include("namedgraph.jl") include(joinpath("generators", "named_staticgraphs.jl")) -include("utilities.jl") # TODO: reexport Graphs.jl (except for `Graphs.contract`) export NamedGraph, diff --git a/src/utilities.jl b/src/utilities.jl deleted file mode 100644 index 1d92fa6..0000000 --- a/src/utilities.jl +++ /dev/null @@ -1,31 +0,0 @@ -"""Remove a list of edges from a graph g""" -function rem_edges(g::AbstractGraph, edges) - g_out = copy(g) - for e in edges - rem_edge!(g_out, e) - end - - return g_out -end - -function rem_edges!(g::AbstractGraph, edges) - for e in edges - rem_edge!(g, e) - end -end - -"""Add a list of edges to a graph g""" -function add_edges(g::AbstractGraph, edges) - g_out = copy(g) - for e in edges - add_edge!(g_out, e) - end - - return g_out -end - -function add_edges!(g::AbstractGraph, edges) - for e in edges - add_edge!(g, e) - end -end \ No newline at end of file From 3e92dba1e6f3c0303a47f4d5e2a6a031a9885dfa Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 12:23:53 -0500 Subject: [PATCH 4/8] Added Testing for Adding and Removing Edges --- test/test_add_rem_edges.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test/test_add_rem_edges.jl diff --git a/test/test_add_rem_edges.jl b/test/test_add_rem_edges.jl new file mode 100644 index 0000000..2606bdc --- /dev/null +++ b/test/test_add_rem_edges.jl @@ -0,0 +1,18 @@ +using Test +using NamedGraphs +using NamedGraphs: add_edges, add_edges!, rem_edges, rem_edges! +using Graphs + + +@testset "Adding and Removing Edge Lists" begin + + g = named_grid((2, 2)) + rem_edges!(g, [(1,1) => (1,2), (1,1) => (2,1)]) + @test !has_edge(g, (1,1) => (1,2)) && !has_edge(g, (1,1) => (2,1)) && has_edge(g, (1,2) => (2,2)) + + n = 10 + g = NamedGraph([(i, ) for i = 1:n]) + add_edges!(g, [(i, ) => (i + 1, ) for i = 1:n-1]) + @test is_connected(g) + +end \ No newline at end of file From 9798601c5fcfff13d62f966e6cc47c8dd68a9a9a Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 14:55:03 -0500 Subject: [PATCH 5/8] Formatting and Test Layout --- src/Graphs/abstractgraph.jl | 34 +++++++++++++++++----------------- test/test_add_rem_edges.jl | 21 ++++++++++----------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/Graphs/abstractgraph.jl b/src/Graphs/abstractgraph.jl index 6b38c10..e48607e 100644 --- a/src/Graphs/abstractgraph.jl +++ b/src/Graphs/abstractgraph.jl @@ -382,32 +382,32 @@ end """Remove a list of edges from a graph g""" function rem_edges(g::AbstractGraph, edges) - g_out = copy(g) - for e in edges - rem_edge!(g_out, e) - end + g_out = copy(g) + for e in edges + rem_edge!(g_out, e) + end - return g_out + return g_out end function rem_edges!(g::AbstractGraph, edges) - for e in edges - rem_edge!(g, e) - end + for e in edges + rem_edge!(g, e) + end end """Add a list of edges to a graph g""" function add_edges(g::AbstractGraph, edges) - g_out = copy(g) - for e in edges - add_edge!(g_out, e) - end + g_out = copy(g) + for e in edges + add_edge!(g_out, e) + end - return g_out + return g_out end function add_edges!(g::AbstractGraph, edges) - for e in edges - add_edge!(g, e) - end -end \ No newline at end of file + for e in edges + add_edge!(g, e) + end +end diff --git a/test/test_add_rem_edges.jl b/test/test_add_rem_edges.jl index 2606bdc..a536649 100644 --- a/test/test_add_rem_edges.jl +++ b/test/test_add_rem_edges.jl @@ -3,16 +3,15 @@ using NamedGraphs using NamedGraphs: add_edges, add_edges!, rem_edges, rem_edges! using Graphs - @testset "Adding and Removing Edge Lists" begin + g = named_grid((2, 2)) + rem_edges!(g, [(1, 1) => (1, 2), (1, 1) => (2, 1)]) + @test !has_edge(g, (1, 1) => (1, 2)) + @test !has_edge(g, (1, 1) => (2, 1)) + @test has_edge(g, (1, 2) => (2, 2)) - g = named_grid((2, 2)) - rem_edges!(g, [(1,1) => (1,2), (1,1) => (2,1)]) - @test !has_edge(g, (1,1) => (1,2)) && !has_edge(g, (1,1) => (2,1)) && has_edge(g, (1,2) => (2,2)) - - n = 10 - g = NamedGraph([(i, ) for i = 1:n]) - add_edges!(g, [(i, ) => (i + 1, ) for i = 1:n-1]) - @test is_connected(g) - -end \ No newline at end of file + n = 10 + g = NamedGraph([(i,) for i in 1:n]) + add_edges!(g, [(i,) => (i + 1,) for i in 1:(n - 1)]) + @test is_connected(g) +end From 25c3c37e722f1a53ecd6ba40386bb92e173e6d31 Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 15:47:09 -0500 Subject: [PATCH 6/8] Improved Code Structure --- src/Graphs/abstractgraph.jl | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Graphs/abstractgraph.jl b/src/Graphs/abstractgraph.jl index e48607e..4b0d494 100644 --- a/src/Graphs/abstractgraph.jl +++ b/src/Graphs/abstractgraph.jl @@ -381,33 +381,32 @@ function mincut_partitions(graph::AbstractGraph, distmx=weights(graph)) end """Remove a list of edges from a graph g""" -function rem_edges(g::AbstractGraph, edges) - g_out = copy(g) - for e in edges - rem_edge!(g_out, e) - end - - return g_out -end - function rem_edges!(g::AbstractGraph, edges) for e in edges rem_edge!(g, e) end + return g end -"""Add a list of edges to a graph g""" -function add_edges(g::AbstractGraph, edges) +function rem_edges(g::AbstractGraph, edges) g_out = copy(g) - for e in edges - add_edge!(g_out, e) - end - + rem_edges!(g, edges) return g_out end + +"""Add a list of edges to a graph g""" function add_edges!(g::AbstractGraph, edges) for e in edges add_edge!(g, e) end + return g end + +function add_edges(g::AbstractGraph, edges) + g_out = copy(g) + add_edges!(g, edges) + return g_out +end + + From 0c13c10746ff141ecdbd5f0ea051fa13145969e8 Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 16:37:27 -0500 Subject: [PATCH 7/8] Renamed Variable Appropriately --- src/Graphs/abstractgraph.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Graphs/abstractgraph.jl b/src/Graphs/abstractgraph.jl index 4b0d494..6150354 100644 --- a/src/Graphs/abstractgraph.jl +++ b/src/Graphs/abstractgraph.jl @@ -389,9 +389,9 @@ function rem_edges!(g::AbstractGraph, edges) end function rem_edges(g::AbstractGraph, edges) - g_out = copy(g) + g = copy(g) rem_edges!(g, edges) - return g_out + return g end @@ -404,9 +404,9 @@ function add_edges!(g::AbstractGraph, edges) end function add_edges(g::AbstractGraph, edges) - g_out = copy(g) + g = copy(g) add_edges!(g, edges) - return g_out + return g end From 078dd2c53f90dac3798da6cf1fb2615eb05c3245 Mon Sep 17 00:00:00 2001 From: Joseph Tindall Date: Thu, 2 Feb 2023 16:39:05 -0500 Subject: [PATCH 8/8] Formatting --- src/Graphs/abstractgraph.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Graphs/abstractgraph.jl b/src/Graphs/abstractgraph.jl index 6150354..63ec680 100644 --- a/src/Graphs/abstractgraph.jl +++ b/src/Graphs/abstractgraph.jl @@ -394,7 +394,6 @@ function rem_edges(g::AbstractGraph, edges) return g end - """Add a list of edges to a graph g""" function add_edges!(g::AbstractGraph, edges) for e in edges @@ -408,5 +407,3 @@ function add_edges(g::AbstractGraph, edges) add_edges!(g, edges) return g end - -