From 00b2ef2fb4f686e4e50eb0605c29a91f23bc25ed Mon Sep 17 00:00:00 2001 From: Lars Kastner Date: Mon, 11 Sep 2023 14:52:42 +0200 Subject: [PATCH] Combinatorics: Print graphs nicely --- src/Combinatorics/Graphs/functions.jl | 40 +++++++++++++++---- .../Polyhedron/standard_constructions.jl | 6 +-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Combinatorics/Graphs/functions.jl b/src/Combinatorics/Graphs/functions.jl index 97e2abfd1afd..68d7435b8d65 100644 --- a/src/Combinatorics/Graphs/functions.jl +++ b/src/Combinatorics/Graphs/functions.jl @@ -52,16 +52,12 @@ julia> G = ZZ[0 0; 1 0] [1 0] julia> graph_from_adjacency_matrix(Directed, G) -Graph{Directed}(pm::graph::Graph -{} -{0} -) +Directed graph with 2 nodes and the following edges: +(2, 1) julia> graph_from_adjacency_matrix(Undirected, G) -Graph{Undirected}(pm::graph::Graph -{1} -{0} -) +Undirected graph with 2 nodes and the following edges: +(2, 1) ``` """ @@ -1051,3 +1047,31 @@ Polyhedron in ambient dimension 6 ``` """ fractional_matching_polytope(G::Graph{Undirected}) = polyhedron(Polymake.polytope.fractional_matching_polytope(pm_object(G))) + + +################################################################################ +################################################################################ +## Printing +################################################################################ +################################################################################ +_to_string(::Type{Polymake.Directed}) = "Directed" +_to_string(::Type{Polymake.Undirected}) = "Undirected" + +function Base.show(io::IO, ::MIME"text/plain", G::Graph{T}) where {T <: Union{Polymake.Directed, Polymake.Undirected}} + if nedges(G) > 0 + println(io, "$(_to_string(T)) graph with $(nvertices(G)) nodes and the following edges:") # at least one new line is needed + for e in edges(G) + print(io, "($(src(e)), $(dst(e)))") + end + else + print(io, "$(_to_string(T)) graph with $(nvertices(G)) nodes and no edges") + end +end + +function Base.show(io::IO, G::Graph{T}) where {T <: Union{Polymake.Directed, Polymake.Undirected}} + if get(io, :supercompact, false) + print(io, "$(_to_string(T)) graph") + else + print(io, "$(_to_string(T)) graph with $(nvertices(G)) nodes and $(nedges(G)) edges") + end +end diff --git a/src/PolyhedralGeometry/Polyhedron/standard_constructions.jl b/src/PolyhedralGeometry/Polyhedron/standard_constructions.jl index c78d007b4014..3dbab25fb135 100644 --- a/src/PolyhedralGeometry/Polyhedron/standard_constructions.jl +++ b/src/PolyhedralGeometry/Polyhedron/standard_constructions.jl @@ -2207,11 +2207,7 @@ The following produces first the standard cube in $3$ dimensions, and then a bipyramid over the convex hull of the unit vectors. ```jldoctest julia> G = Graph{Undirected}(3) -Graph{Undirected}(pm::graph::Graph -{} -{} -{} -) +Undirected graph with 3 nodes and no edges julia> S = stable_set_polytope(G) Polyhedron in ambient dimension 3