diff --git a/cmd/internal/graph/common.go b/cmd/internal/graph/common.go index 39dca4e..a3599ca 100644 --- a/cmd/internal/graph/common.go +++ b/cmd/internal/graph/common.go @@ -18,6 +18,8 @@ package graph import ( "gonum.org/v1/gonum/graph" + "gonum.org/v1/gonum/graph/multi" + "gonum.org/v1/gonum/graph/simple" "gonum.org/v1/gonum/mat" ) @@ -27,8 +29,6 @@ type WeightedGraph interface { graph.WeightedMultigraphBuilder WeightedEdges() graph.WeightedEdges - - AdjacencyMatrix() *mat.Dense } type WeightedMatrix interface { @@ -38,6 +38,22 @@ type WeightedMatrix interface { Matrix() mat.Matrix } +// AdjacencyMatrix returns the graph's adjacency matrix. +func AdjacencyMatrix(g WeightedGraph) *mat.Dense { + var adj WeightedMatrix + switch g.(type) { + case *multi.WeightedDirectedGraph: + adj = simple.NewDirectedMatrix(g.Nodes().Len(), 0, 0, 0) + case *multi.WeightedUndirectedGraph: + adj = simple.NewUndirectedMatrix(g.Nodes().Len(), 0, 0, 0) + default: + panic("not a graph type we handle") + } + + matrix := toAdjMatrix(g, adj) + return matrix +} + func toAdjMatrix(g WeightedGraph, adj WeightedMatrix) *mat.Dense { copyEdges(g, adj) matrix := addSelfEdges(g, adj) diff --git a/cmd/internal/graph/directed.go b/cmd/internal/graph/directed.go deleted file mode 100644 index 702f593..0000000 --- a/cmd/internal/graph/directed.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright © 2025 filifa - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ -package graph - -import ( - "gonum.org/v1/gonum/graph/multi" - "gonum.org/v1/gonum/graph/simple" - "gonum.org/v1/gonum/mat" -) - -// DirectedGraph embeds a multi.WeightedDirectedGraph (as opposed to -// simple.WeightedDirectedGraph) to handle self loops. -type WeightedDirectedGraph struct { - *multi.WeightedDirectedGraph -} - -// NewDirectedGraph returns a graph with no nodes or edges. -func NewWeightedDirectedGraph() *WeightedDirectedGraph { - return &WeightedDirectedGraph{WeightedDirectedGraph: multi.NewWeightedDirectedGraph()} -} - -// AdjacencyMatrix returns the graph's adjacency matrix. -func (g *WeightedDirectedGraph) AdjacencyMatrix() *mat.Dense { - adj := simple.NewDirectedMatrix(g.Nodes().Len(), 0, 0, 0) - matrix := toAdjMatrix(g, adj) - return matrix -} diff --git a/cmd/internal/graph/dot/graph.go b/cmd/internal/graph/dot/graph.go index 952f79f..119ea16 100644 --- a/cmd/internal/graph/dot/graph.go +++ b/cmd/internal/graph/dot/graph.go @@ -33,11 +33,11 @@ type DOTWeightedGraph struct { // NewDOTDirectedGraph returns a graph with no nodes or edges. func NewDOTDirectedGraph(weightAttr string) DOTWeightedGraph { - return DOTWeightedGraph{WeightedGraph: igraph.NewWeightedDirectedGraph(), WeightAttribute: weightAttr} + return DOTWeightedGraph{WeightedGraph: multi.NewWeightedDirectedGraph(), WeightAttribute: weightAttr} } func NewDOTUndirectedGraph(weightAttr string) DOTWeightedGraph { - return DOTWeightedGraph{WeightedGraph: igraph.NewWeightedUndirectedGraph(), WeightAttribute: weightAttr} + return DOTWeightedGraph{WeightedGraph: multi.NewWeightedUndirectedGraph(), WeightAttribute: weightAttr} } // NewLine returns a DOT-aware weighted line. diff --git a/cmd/internal/graph/undirected.go b/cmd/internal/graph/undirected.go deleted file mode 100644 index faf7b60..0000000 --- a/cmd/internal/graph/undirected.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright © 2025 filifa - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ -package graph - -import ( - "gonum.org/v1/gonum/graph/multi" - "gonum.org/v1/gonum/graph/simple" - "gonum.org/v1/gonum/mat" -) - -// UndirectedGraph embeds a multi.WeightedUndirectedGraph (as opposed to -// simple.WeightedUndirectedGraph) to handle self loops. -type WeightedUndirectedGraph struct { - *multi.WeightedUndirectedGraph -} - -// NewUndirectedGraph returns a graph with no nodes or edges. -func NewWeightedUndirectedGraph() *WeightedUndirectedGraph { - return &WeightedUndirectedGraph{WeightedUndirectedGraph: multi.NewWeightedUndirectedGraph()} -} - -// AdjacencyMatrix returns the graph's adjacency matrix. -func (g *WeightedUndirectedGraph) AdjacencyMatrix() *mat.Dense { - adj := simple.NewUndirectedMatrix(g.Nodes().Len(), 0, 0, 0) - matrix := toAdjMatrix(g, adj) - return matrix -} diff --git a/cmd/root.go b/cmd/root.go index b641dbe..0ba93f1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -21,6 +21,7 @@ import ( "fmt" "os" + igraph "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph" idot "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph/dot" "github.com/spf13/cobra" @@ -79,7 +80,7 @@ func parse(cmd *cobra.Command, args []string) { panic(err) } - matrix, err := orderedAdjMatrix(graph) + matrix, err := orderedAdjMatrix(graph.WeightedGraph) if err != nil { panic(err) } @@ -87,8 +88,8 @@ func parse(cmd *cobra.Command, args []string) { outputMatrix(matrix) } -func orderedAdjMatrix(g idot.DOTWeightedGraph) (*mat.Dense, error) { - matrix := g.AdjacencyMatrix() +func orderedAdjMatrix(g igraph.WeightedGraph) (*mat.Dense, error) { + matrix := igraph.AdjacencyMatrix(g) if len(nodeOrder) == 0 { return matrix, nil }