remove my graph types

This commit is contained in:
filifa 2025-05-09 00:32:59 -04:00
parent 5bd49f2037
commit 405b7c9414
5 changed files with 24 additions and 89 deletions

View File

@ -18,6 +18,8 @@ package graph
import ( import (
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/multi"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/mat" "gonum.org/v1/gonum/mat"
) )
@ -27,8 +29,6 @@ type WeightedGraph interface {
graph.WeightedMultigraphBuilder graph.WeightedMultigraphBuilder
WeightedEdges() graph.WeightedEdges WeightedEdges() graph.WeightedEdges
AdjacencyMatrix() *mat.Dense
} }
type WeightedMatrix interface { type WeightedMatrix interface {
@ -38,6 +38,22 @@ type WeightedMatrix interface {
Matrix() mat.Matrix 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 { func toAdjMatrix(g WeightedGraph, adj WeightedMatrix) *mat.Dense {
copyEdges(g, adj) copyEdges(g, adj)
matrix := addSelfEdges(g, adj) matrix := addSelfEdges(g, adj)

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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
}

View File

@ -33,11 +33,11 @@ type DOTWeightedGraph struct {
// NewDOTDirectedGraph returns a graph with no nodes or edges. // NewDOTDirectedGraph returns a graph with no nodes or edges.
func NewDOTDirectedGraph(weightAttr string) DOTWeightedGraph { func NewDOTDirectedGraph(weightAttr string) DOTWeightedGraph {
return DOTWeightedGraph{WeightedGraph: igraph.NewWeightedDirectedGraph(), WeightAttribute: weightAttr} return DOTWeightedGraph{WeightedGraph: multi.NewWeightedDirectedGraph(), WeightAttribute: weightAttr}
} }
func NewDOTUndirectedGraph(weightAttr string) DOTWeightedGraph { 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. // NewLine returns a DOT-aware weighted line.

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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
}

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"os" "os"
igraph "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph"
idot "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph/dot" idot "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph/dot"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -79,7 +80,7 @@ func parse(cmd *cobra.Command, args []string) {
panic(err) panic(err)
} }
matrix, err := orderedAdjMatrix(graph) matrix, err := orderedAdjMatrix(graph.WeightedGraph)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -87,8 +88,8 @@ func parse(cmd *cobra.Command, args []string) {
outputMatrix(matrix) outputMatrix(matrix)
} }
func orderedAdjMatrix(g idot.DOTWeightedGraph) (*mat.Dense, error) { func orderedAdjMatrix(g igraph.WeightedGraph) (*mat.Dense, error) {
matrix := g.AdjacencyMatrix() matrix := igraph.AdjacencyMatrix(g)
if len(nodeOrder) == 0 { if len(nodeOrder) == 0 {
return matrix, nil return matrix, nil
} }