Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added graph_from_edges function #3300

Merged
merged 8 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions src/Combinatorics/Graphs/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1081,3 +1081,53 @@
print(io, "$(_to_string(T)) graph with $(nvertices(G)) nodes and $(nedges(G)) edges")
end
end

function graph_from_edges(::Type{T},
edges::Vector{Edge},
n_vertices::Int=-1) where {T <: Union{Directed, Undirected}}

n_needed = maximum(reduce(append!,[[src(e),dst(e)] for e in edges]))
@req (n_vertices >= n_needed || n_vertices < 0) "n_vertices must be at least the maximum vertex in the edges"

g = Graph{T}(max(n_needed, n_vertices))
for e in edges
add_edge!(g, src(e), dst(e))
end

return g
end

graph_from_edges(::Type{T},

Check warning on line 1100 in src/Combinatorics/Graphs/functions.jl

View check run for this annotation

Codecov / codecov/patch

src/Combinatorics/Graphs/functions.jl#L1100

Added line #L1100 was not covered by tests
edges::EdgeIterator,
n_vertices::Int=-1) where {T <: Union{Directed, Undirected}} = graph_from_edges(T, collect(edges), n_vertices)

@doc raw"""
graph_from_edges(::Type{T}, edges::Vector{Vector{Int}}) where {T <:Union{Directed, Undirected}}

Creates a graph from a vector of edges. Optionally, you could input the number of vertices, but if this number is lower than the maximum vertex in the edges, this argument will be ignored.

# Examples 1
```jldoctest
julia> G = graph_from_edges(Undirected, [[1,3],[3,5],[4,5],[2,4],[2,3]])
Undirected graph with 5 nodes and the following edges:
(3, 1)(3, 2)(4, 2)(5, 3)(5, 4)

```

```
# Examples 2
```jldoctest
julia> G = graph_from_edges(Undirected,[[1,3]])
Undirected graph with 3 nodes and the following edges:
(3, 1))

```
"""
graph_from_edges(::Type{T},
edges::Vector{Vector{Int}},
n_vertices::Int=-1) where {T <: Union{Directed, Undirected}} = graph_from_edges(T, [Edge(e[1], e[2]) for e in edges], n_vertices)

graph_from_edges(
edges::Vector{Vector{Int}},
n_vertices::Int=-1) = graph_from_edges(Undirected, [Edge(e[1], e[2]) for e in edges], n_vertices)

1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ export graded_polynomial_ring
export grading_group
export graph
export graph_from_adjacency_matrix
export graph_from_edges
export grassmann_pluecker_ideal
export grid_morphism
export groebner_basis
Expand Down
15 changes: 15 additions & 0 deletions test/Combinatorics/Graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,19 @@
g = Graph{Undirected}(1)
@test !add_edge!(g,1,2)
end

@testset "grap_from_edges" begin
x1 = [[5,6],[7,8],[11,12]]
G1 = graph_from_edges(x1)

@test nv(G1) == 12
@test ne(G1) == 3

x2 = [[11,3],[3,5],[4,5],[2,4],[2,3]]
G2 = graph_from_edges(Undirected, x2, 13)

@test nv(G2) == 13
@test ne(G2) == 5

end
end
Loading