From 96bb8fff86659be4de85f904f429abfe17b8c7c4 Mon Sep 17 00:00:00 2001 From: damianodegaspari <144324904+damianodegaspari@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:03:09 +0100 Subject: [PATCH 1/4] Update core.jl Fixed the documentation of the functions indegree, outdegree and degree and removed the TODO comment in the source code that was asking for it. --- src/core.jl | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/core.jl b/src/core.jl index cb8abd175..f7276f245 100644 --- a/src/core.jl +++ b/src/core.jl @@ -44,15 +44,15 @@ julia> add_vertices!(g, 2) """ add_vertices!(g::AbstractGraph, n::Integer) = sum([add_vertex!(g) for i in 1:n]) -# TODO the behaviour of indegree (and as well outdegree and degree) is very -# badly documented for the case indegree(g, vs) where vs is not a single vertex -# but rather a collection of vertices """ indegree(g[, v]) -Return a vector corresponding to the number of edges which end at each vertex in -graph `g`. If `v` is specified, only return degrees for vertices in `v`. +Return a vector containing the indegrees of every vertex of the graph `g`, where +the indegree of a vertex is defined as the number of edges which end at that +vertex. If `v` is specified and is a single vertex, only return the indegree of +`v`. If `v` is specified and is a vector of vertices, only return the indegrees +of the vertices in `v`. # Examples ```jldoctest @@ -69,6 +69,14 @@ julia> indegree(g) 1 0 1 + +julia> indegree(g,[2,3]) +2-element Vector{Int64}: + 0 + 1 + +julia> indegree(g,2) +0 ``` """ indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v)) @@ -77,8 +85,11 @@ indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs] """ outdegree(g[, v]) -Return a vector corresponding to the number of edges which start at each vertex in -graph `g`. If `v` is specified, only return degrees for vertices in `v`. +Return a vector containing the outdegrees of every vertex of the graph `g`, where +the outdegree of a vertex is defined as the number of edges which start at that +vertex. If `v` is specified and is a single vertex, only return the outdegree of +`v`. If `v` is specified and is a vector of vertices, only return the outdegrees +of the vertices in `v`. # Examples ```jldoctest @@ -95,6 +106,14 @@ julia> outdegree(g) 0 1 1 + +julia> outdegree(g,[1,2]) +2-element Vector{Int64}: + 0 + 1 + +julia> outdegree(g,2) +1 ``` """ outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v)) @@ -103,10 +122,12 @@ outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs] """ degree(g[, v]) -Return a vector corresponding to the number of edges which start or end at each -vertex in graph `g`. If `v` is specified, only return degrees for vertices in `v`. -For directed graphs, this value equals the incoming plus outgoing edges. -For undirected graphs, it equals the connected edges. +Return a vector containing the degrees of every vertex of the graph `g`, where +the degree of a vertex is defined as the number of edges which start or end at +that vertex. For directed graphs, the degree of a vertex is equal to the sum of +its indegree and outdegree. If `v` is specified and is a single vertex, only +return the degree of `v`. If `v` is specified and is a vector of vertices, only +return the degrees of the vertices in `v`. # Examples ```jldoctest @@ -123,6 +144,14 @@ julia> degree(g) 1 1 2 + +julia> degree(g,[1,3]) +2-element Vector{Int64}: + 1 + 2 + +julia> degree(g,3) +2 ``` """ function degree end From 7b05f219af1b9a5ed70631c3c57ab97d153a46ba Mon Sep 17 00:00:00 2001 From: damianodegaspari <144324904+damianodegaspari@users.noreply.github.com> Date: Tue, 11 Mar 2025 23:26:11 +0100 Subject: [PATCH 2/4] update to the previous doc changes, this time formatting according to BlueStyle() using JuliaFormatter --- src/core.jl | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/core.jl b/src/core.jl index f7276f245..5780dcbe2 100644 --- a/src/core.jl +++ b/src/core.jl @@ -1,12 +1,12 @@ """ - AbstractPathState + AbstractPathState An abstract type that provides information from shortest paths calculations. """ abstract type AbstractPathState end """ - is_ordered(e) + is_ordered(e) Return true if the source vertex of edge `e` is less than or equal to the destination vertex. @@ -26,7 +26,7 @@ false is_ordered(e::AbstractEdge) = src(e) <= dst(e) """ - add_vertices!(g, n) + add_vertices!(g, n) Add `n` new vertices to the graph `g`. Return the number of vertices that were added successfully. @@ -44,9 +44,8 @@ julia> add_vertices!(g, 2) """ add_vertices!(g::AbstractGraph, n::Integer) = sum([add_vertex!(g) for i in 1:n]) - """ - indegree(g[, v]) + indegree(g[, v]) Return a vector containing the indegrees of every vertex of the graph `g`, where the indegree of a vertex is defined as the number of edges which end at that @@ -83,7 +82,7 @@ indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v)) indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs] """ - outdegree(g[, v]) + outdegree(g[, v]) Return a vector containing the outdegrees of every vertex of the graph `g`, where the outdegree of a vertex is defined as the number of edges which start at that @@ -120,7 +119,7 @@ outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v)) outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs] """ - degree(g[, v]) + degree(g[, v]) Return a vector containing the degrees of every vertex of the graph `g`, where the degree of a vertex is defined as the number of edges which start or end at @@ -166,48 +165,48 @@ end degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs] """ - Δout(g) + Δout(g) Return the maximum [`outdegree`](@ref) of vertices in `g`. """ Δout(g) = noallocextreme(outdegree, (>), typemin(Int), g) """ - δout(g) + δout(g) Return the minimum [`outdegree`](@ref) of vertices in `g`. """ δout(g) = noallocextreme(outdegree, (<), typemax(Int), g) """ - Δin(g) + Δin(g) Return the maximum [`indegree`](@ref) of vertices in `g`. """ Δin(g) = noallocextreme(indegree, (>), typemin(Int), g) """ - δin(g) + δin(g) Return the minimum [`indegree`](@ref) of vertices in `g`. """ δin(g) = noallocextreme(indegree, (<), typemax(Int), g) """ - Δ(g) + Δ(g) Return the maximum [`degree`](@ref) of vertices in `g`. """ Δ(g) = noallocextreme(degree, (>), typemin(Int), g) """ - δ(g) + δ(g) Return the minimum [`degree`](@ref) of vertices in `g`. """ δ(g) = noallocextreme(degree, (<), typemax(Int), g) """ - noallocextreme(f, comparison, initial, g) + noallocextreme(f, comparison, initial, g) Compute the extreme value of `[f(g,i) for i=i:nv(g)]` without gathering them all """ @@ -223,7 +222,7 @@ function noallocextreme(f, comparison, initial, g) end """ - degree_histogram(g, degfn=degree) + degree_histogram(g, degfn=degree) Return a `Dict` with values representing the number of vertices that have degree represented by the key. @@ -242,7 +241,7 @@ function degree_histogram(g::AbstractGraph{T}, degfn=degree) where {T} end """ - neighbors(g, v) + neighbors(g, v) Return a list of all neighbors reachable from vertex `v` in `g`. For directed graphs, the default is equivalent to [`outneighbors`](@ref); @@ -278,7 +277,7 @@ julia> neighbors(g, 3) neighbors(g::AbstractGraph, v::Integer) = outneighbors(g, v) """ - all_neighbors(g, v) + all_neighbors(g, v) Return a list of all inbound and outbound neighbors of `v` in `g`. For undirected graphs, this is equivalent to both [`outneighbors`](@ref) @@ -320,7 +319,7 @@ end @traitfn all_neighbors(g::::(!IsDirected), v::Integer) = neighbors(g, v) """ - common_neighbors(g, u, v) + common_neighbors(g, u, v) Return the neighbors common to vertices `u` and `v` in `g`. @@ -360,7 +359,7 @@ function common_neighbors(g::AbstractGraph, u::Integer, v::Integer) end """ - has_self_loops(g) + has_self_loops(g) Return true if `g` has any self loops. @@ -386,7 +385,7 @@ function has_self_loops(g::AbstractGraph) end """ - num_self_loops(g) + num_self_loops(g) Return the number of self loops in `g`. @@ -410,7 +409,7 @@ julia> num_self_loops(g) num_self_loops(g::AbstractGraph) = nv(g) == 0 ? 0 : sum(v -> has_edge(g, v, v), vertices(g)) """ - density(g) + density(g) Return the density of `g`. Density is defined as the ratio of the number of actual edges to the @@ -422,7 +421,7 @@ function density end @traitfn density(g::::(!IsDirected)) = (2 * ne(g)) / (nv(g) * (nv(g) - 1)) """ - squash(g) + squash(g) Return a copy of a graph with the smallest practical eltype that can accommodate all vertices. @@ -448,7 +447,7 @@ function squash(g::AbstractGraph) end """ - weights(g) + weights(g) Return the weights of the edges of a graph `g` as a matrix. Defaults to [`Graphs.DefaultDistance`](@ref). From a36b8bd0e428929fbd44a3f0d10206ba0281a431 Mon Sep 17 00:00:00 2001 From: damianodegaspari Date: Sun, 16 Mar 2025 22:58:49 +0100 Subject: [PATCH 3/4] Another try at getting formatting right. --- src/core.jl | 56 ++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/core.jl b/src/core.jl index 5780dcbe2..ef354bbe6 100644 --- a/src/core.jl +++ b/src/core.jl @@ -1,12 +1,12 @@ """ - AbstractPathState + AbstractPathState An abstract type that provides information from shortest paths calculations. """ abstract type AbstractPathState end """ - is_ordered(e) + is_ordered(e) Return true if the source vertex of edge `e` is less than or equal to the destination vertex. @@ -26,7 +26,7 @@ false is_ordered(e::AbstractEdge) = src(e) <= dst(e) """ - add_vertices!(g, n) + add_vertices!(g, n) Add `n` new vertices to the graph `g`. Return the number of vertices that were added successfully. @@ -45,7 +45,7 @@ julia> add_vertices!(g, 2) add_vertices!(g::AbstractGraph, n::Integer) = sum([add_vertex!(g) for i in 1:n]) """ - indegree(g[, v]) + indegree(g[, v]) Return a vector containing the indegrees of every vertex of the graph `g`, where the indegree of a vertex is defined as the number of edges which end at that @@ -79,10 +79,10 @@ julia> indegree(g,2) ``` """ indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v)) -indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs] +indegree(g::AbstractGraph, vs = vertices(g)) = [indegree(g, x) for x in vs] """ - outdegree(g[, v]) + outdegree(g[, v]) Return a vector containing the outdegrees of every vertex of the graph `g`, where the outdegree of a vertex is defined as the number of edges which start at that @@ -116,10 +116,10 @@ julia> outdegree(g,2) ``` """ outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v)) -outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs] +outdegree(g::AbstractGraph, vs = vertices(g)) = [outdegree(g, x) for x in vs] """ - degree(g[, v]) + degree(g[, v]) Return a vector containing the degrees of every vertex of the graph `g`, where the degree of a vertex is defined as the number of edges which start or end at @@ -162,51 +162,51 @@ function degree(g::AbstractGraph, v::Integer) return indegree(g, v) + outdegree(g, v) end -degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs] +degree(g::AbstractGraph, vs = vertices(g)) = [degree(g, x) for x in vs] """ - Δout(g) + Δout(g) Return the maximum [`outdegree`](@ref) of vertices in `g`. """ Δout(g) = noallocextreme(outdegree, (>), typemin(Int), g) """ - δout(g) + δout(g) Return the minimum [`outdegree`](@ref) of vertices in `g`. """ δout(g) = noallocextreme(outdegree, (<), typemax(Int), g) """ - Δin(g) + Δin(g) Return the maximum [`indegree`](@ref) of vertices in `g`. """ Δin(g) = noallocextreme(indegree, (>), typemin(Int), g) """ - δin(g) + δin(g) Return the minimum [`indegree`](@ref) of vertices in `g`. """ δin(g) = noallocextreme(indegree, (<), typemax(Int), g) """ - Δ(g) + Δ(g) Return the maximum [`degree`](@ref) of vertices in `g`. """ Δ(g) = noallocextreme(degree, (>), typemin(Int), g) """ - δ(g) + δ(g) Return the minimum [`degree`](@ref) of vertices in `g`. """ δ(g) = noallocextreme(degree, (<), typemax(Int), g) """ - noallocextreme(f, comparison, initial, g) + noallocextreme(f, comparison, initial, g) Compute the extreme value of `[f(g,i) for i=i:nv(g)]` without gathering them all """ @@ -222,7 +222,7 @@ function noallocextreme(f, comparison, initial, g) end """ - degree_histogram(g, degfn=degree) + degree_histogram(g, degfn=degree) Return a `Dict` with values representing the number of vertices that have degree represented by the key. @@ -230,8 +230,8 @@ represented by the key. Degree function (for example, [`indegree`](@ref) or [`outdegree`](@ref)) may be specified by overriding `degfn`. """ -function degree_histogram(g::AbstractGraph{T}, degfn=degree) where {T} - hist = Dict{T,Int}() +function degree_histogram(g::AbstractGraph{T}, degfn = degree) where {T} + hist = Dict{T, Int}() for v in vertices(g) # minimize allocations by for d in degfn(g, v) # iterating over vertices hist[d] = get(hist, d, 0) + 1 @@ -241,7 +241,7 @@ function degree_histogram(g::AbstractGraph{T}, degfn=degree) where {T} end """ - neighbors(g, v) + neighbors(g, v) Return a list of all neighbors reachable from vertex `v` in `g`. For directed graphs, the default is equivalent to [`outneighbors`](@ref); @@ -277,7 +277,7 @@ julia> neighbors(g, 3) neighbors(g::AbstractGraph, v::Integer) = outneighbors(g, v) """ - all_neighbors(g, v) + all_neighbors(g, v) Return a list of all inbound and outbound neighbors of `v` in `g`. For undirected graphs, this is equivalent to both [`outneighbors`](@ref) @@ -319,7 +319,7 @@ end @traitfn all_neighbors(g::::(!IsDirected), v::Integer) = neighbors(g, v) """ - common_neighbors(g, u, v) + common_neighbors(g, u, v) Return the neighbors common to vertices `u` and `v` in `g`. @@ -359,7 +359,7 @@ function common_neighbors(g::AbstractGraph, u::Integer, v::Integer) end """ - has_self_loops(g) + has_self_loops(g) Return true if `g` has any self loops. @@ -385,7 +385,7 @@ function has_self_loops(g::AbstractGraph) end """ - num_self_loops(g) + num_self_loops(g) Return the number of self loops in `g`. @@ -409,7 +409,7 @@ julia> num_self_loops(g) num_self_loops(g::AbstractGraph) = nv(g) == 0 ? 0 : sum(v -> has_edge(g, v, v), vertices(g)) """ - density(g) + density(g) Return the density of `g`. Density is defined as the ratio of the number of actual edges to the @@ -421,7 +421,7 @@ function density end @traitfn density(g::::(!IsDirected)) = (2 * ne(g)) / (nv(g) * (nv(g) - 1)) """ - squash(g) + squash(g) Return a copy of a graph with the smallest practical eltype that can accommodate all vertices. @@ -433,7 +433,7 @@ function squash(g::AbstractGraph) # TODO this version check can be removed when we increase the required Julia version deprecation_msg = "squash(::AbstractGraph) is deprecated in favor of methods that specialize on the graph type." if VERSION >= v"1.5.2" - Base.depwarn(deprecation_msg, :squash; force=true) + Base.depwarn(deprecation_msg, :squash; force = true) else Base.depwarn(deprecation_msg, :squash) end @@ -447,7 +447,7 @@ function squash(g::AbstractGraph) end """ - weights(g) + weights(g) Return the weights of the edges of a graph `g` as a matrix. Defaults to [`Graphs.DefaultDistance`](@ref). From de43b2eb9921a47a7c5121cda4f4df3309aad307 Mon Sep 17 00:00:00 2001 From: damianodegaspari Date: Sun, 16 Mar 2025 22:59:07 +0100 Subject: [PATCH 4/4] Another try at getting formatting right --- src/core.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core.jl b/src/core.jl index ef354bbe6..3c6659faa 100644 --- a/src/core.jl +++ b/src/core.jl @@ -79,7 +79,7 @@ julia> indegree(g,2) ``` """ indegree(g::AbstractGraph, v::Integer) = length(inneighbors(g, v)) -indegree(g::AbstractGraph, vs = vertices(g)) = [indegree(g, x) for x in vs] +indegree(g::AbstractGraph, vs=vertices(g)) = [indegree(g, x) for x in vs] """ outdegree(g[, v]) @@ -116,7 +116,7 @@ julia> outdegree(g,2) ``` """ outdegree(g::AbstractGraph, v::Integer) = length(outneighbors(g, v)) -outdegree(g::AbstractGraph, vs = vertices(g)) = [outdegree(g, x) for x in vs] +outdegree(g::AbstractGraph, vs=vertices(g)) = [outdegree(g, x) for x in vs] """ degree(g[, v]) @@ -162,7 +162,7 @@ function degree(g::AbstractGraph, v::Integer) return indegree(g, v) + outdegree(g, v) end -degree(g::AbstractGraph, vs = vertices(g)) = [degree(g, x) for x in vs] +degree(g::AbstractGraph, vs=vertices(g)) = [degree(g, x) for x in vs] """ Δout(g) @@ -230,8 +230,8 @@ represented by the key. Degree function (for example, [`indegree`](@ref) or [`outdegree`](@ref)) may be specified by overriding `degfn`. """ -function degree_histogram(g::AbstractGraph{T}, degfn = degree) where {T} - hist = Dict{T, Int}() +function degree_histogram(g::AbstractGraph{T}, degfn=degree) where {T} + hist = Dict{T,Int}() for v in vertices(g) # minimize allocations by for d in degfn(g, v) # iterating over vertices hist[d] = get(hist, d, 0) + 1 @@ -433,7 +433,7 @@ function squash(g::AbstractGraph) # TODO this version check can be removed when we increase the required Julia version deprecation_msg = "squash(::AbstractGraph) is deprecated in favor of methods that specialize on the graph type." if VERSION >= v"1.5.2" - Base.depwarn(deprecation_msg, :squash; force = true) + Base.depwarn(deprecation_msg, :squash; force=true) else Base.depwarn(deprecation_msg, :squash) end