Compare commits

..

10 Commits

Author SHA1 Message Date
filifa
d8c1296dec add README 2025-05-10 00:07:48 -04:00
filifa
df30e34590 add precision 6 to output strings 2025-05-10 00:04:42 -04:00
filifa
21e3ff5e74 change wording of error 2025-05-09 23:00:53 -04:00
filifa
2b49d8dd51 return errors instead of panicking 2025-05-09 22:50:21 -04:00
filifa
4cac82441a update comments 2025-05-09 22:36:18 -04:00
filifa
99d144532e update flags 2025-05-09 22:09:39 -04:00
filifa
a0f7da8a09 add description 2025-05-09 22:09:23 -04:00
filifa
405b7c9414 remove my graph types 2025-05-09 00:32:59 -04:00
filifa
5bd49f2037 change type names 2025-05-08 23:57:28 -04:00
filifa
90c5547710 revert an earlier buggy simplification 2025-05-08 23:48:33 -04:00
7 changed files with 87 additions and 122 deletions

25
README.md Normal file
View File

@@ -0,0 +1,25 @@
# gv2adj
gv2adj computes adjacency matrices (either weighted or unweighted) for Graphviz
graphs (both directed and undirected).
## Usage
To get the adjacency matrix of a graph in `graph.gv`:
```
gv2adj -f graph.gv
```
To get the matrix, but using the values stored in the `len` attribute as edge weights:
```
gv2adj -f graph.gv --weight-attr len
```
Say the nodes in the graph are named `foo`, `bar`, and `baz`. To order the rows and columns of the matrix so they correspond to `bar`, `baz`, `foo`:
```
gv2adj -f graph.gv --order bar,baz,foo
```
## Quirks
* If the `--weight-attr` flag is supplied, but an edge does not have the given
attribute, NaN will be output for that edge.
* If there are multiple edges going from node `a` to node `b`, the matrix entry
will be the sum of the two.

View File

@@ -18,19 +18,23 @@ 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"
) )
// WeightedGraph is an interface so we can construct adjacency matrices for
// both directed and undirected graphs.
type WeightedGraph interface { type WeightedGraph interface {
graph.Weighted graph.Weighted
graph.WeightedMultigraph graph.WeightedMultigraph
graph.WeightedMultigraphBuilder graph.WeightedMultigraphBuilder
WeightedEdges() graph.WeightedEdges WeightedEdges() graph.WeightedEdges
AdjacencyMatrix() *mat.Dense
} }
// WeightedMatrix is an interface to handle the construction of both directed
// and undirected adjacency matrices.
type WeightedMatrix interface { type WeightedMatrix interface {
graph.Weighted graph.Weighted
@@ -38,10 +42,25 @@ 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 := mat.DenseCopyOf(adj.Matrix()) matrix := addSelfEdges(g, adj)
addSelfEdges(g, matrix)
return matrix return matrix
} }
@@ -50,6 +69,7 @@ func copyEdges(g WeightedGraph, adj WeightedMatrix) {
for edges := g.WeightedEdges(); edges.Next(); { for edges := g.WeightedEdges(); edges.Next(); {
e := edges.WeightedEdge() e := edges.WeightedEdge()
if e.From() == e.To() { if e.From() == e.To() {
// the simple Matrix classes don't handle self loops
continue continue
} }
@@ -57,14 +77,19 @@ func copyEdges(g WeightedGraph, adj WeightedMatrix) {
} }
} }
func addSelfEdges(g WeightedGraph, matrix mat.Mutable) { func addSelfEdges(g WeightedGraph, adj WeightedMatrix) *mat.Dense {
nodes := g.Nodes() matrix := mat.DenseCopyOf(adj.Matrix())
nodes := adj.Nodes()
for i := 0; nodes.Next(); i++ { for i := 0; nodes.Next(); i++ {
u := nodes.Node() id := nodes.Node().ID()
u := g.Node(id)
e := g.WeightedEdge(u.ID(), u.ID()) e := g.WeightedEdge(u.ID(), u.ID())
if e != nil { if e != nil {
matrix.Set(i, i, e.Weight()) matrix.Set(i, i, e.Weight())
} }
} }
return matrix
} }

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 DirectedGraph struct {
*multi.WeightedDirectedGraph
}
// NewDirectedGraph returns a graph with no nodes or edges.
func NewDirectedGraph() *DirectedGraph {
return &DirectedGraph{WeightedDirectedGraph: multi.NewWeightedDirectedGraph()}
}
// AdjacencyMatrix returns the graph's adjacency matrix.
func (g *DirectedGraph) AdjacencyMatrix() *mat.Dense {
adj := simple.NewDirectedMatrix(g.Nodes().Len(), 0, 0, 0)
matrix := toAdjMatrix(g, adj)
return matrix
}

View File

@@ -25,7 +25,7 @@ import (
"gonum.org/v1/gonum/graph/multi" "gonum.org/v1/gonum/graph/multi"
) )
// DOTWeightedGraph is a graph to unmarshal DOT graphs. // DOTWeightedGraph is a struct to unmarshal DOT graphs.
type DOTWeightedGraph struct { type DOTWeightedGraph struct {
igraph.WeightedGraph igraph.WeightedGraph
WeightAttribute string WeightAttribute string
@@ -33,11 +33,12 @@ 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.NewDirectedGraph(), WeightAttribute: weightAttr} return DOTWeightedGraph{WeightedGraph: multi.NewWeightedDirectedGraph(), WeightAttribute: weightAttr}
} }
// NewDOTUndirectedGraph returns a graph with no nodes or edges.
func NewDOTUndirectedGraph(weightAttr string) DOTWeightedGraph { func NewDOTUndirectedGraph(weightAttr string) DOTWeightedGraph {
return DOTWeightedGraph{WeightedGraph: igraph.NewUndirectedGraph(), WeightAttribute: weightAttr} return DOTWeightedGraph{WeightedGraph: multi.NewWeightedUndirectedGraph(), WeightAttribute: weightAttr}
} }