rename things
This commit is contained in:
53
cmd/internal/graph/dot/graph.go
Normal file
53
cmd/internal/graph/dot/graph.go
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
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 dot
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
igraph "scm.dairydemon.net/filifa/gv2adj/cmd/internal/graph"
|
||||
|
||||
"gonum.org/v1/gonum/graph"
|
||||
"gonum.org/v1/gonum/graph/multi"
|
||||
)
|
||||
|
||||
// DOTDirectedGraph is a graph representing an absorbing Markov chain.
|
||||
type DOTDirectedGraph struct {
|
||||
*igraph.DirectedGraph
|
||||
}
|
||||
|
||||
// NewDOTDirectedGraph returns an absorbing Markov chain with no nodes or
|
||||
// edges.
|
||||
func NewDOTDirectedGraph() *DOTDirectedGraph {
|
||||
return &DOTDirectedGraph{DirectedGraph: igraph.NewDirectedGraph()}
|
||||
}
|
||||
|
||||
// NewLine returns a DOT-aware weighted line.
|
||||
func (g *DOTDirectedGraph) NewLine(from, to graph.Node) graph.Line {
|
||||
e := g.DirectedGraph.NewWeightedLine(from, to, math.NaN()).(multi.WeightedLine)
|
||||
return &weightedLine{WeightedLine: e}
|
||||
}
|
||||
|
||||
// NewNode returns a DOT-aware Node.
|
||||
func (g *DOTDirectedGraph) NewNode() graph.Node {
|
||||
return &Node{Node: g.DirectedGraph.NewNode()}
|
||||
}
|
||||
|
||||
// SetLine adds a DOT-aware weighted line to the Markov chain.
|
||||
func (g *DOTDirectedGraph) SetLine(e graph.Line) {
|
||||
g.DirectedGraph.SetWeightedLine(e.(*weightedLine))
|
||||
}
|
||||
65
cmd/internal/graph/dot/nodes_edges.go
Normal file
65
cmd/internal/graph/dot/nodes_edges.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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 dot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"gonum.org/v1/gonum/graph"
|
||||
"gonum.org/v1/gonum/graph/encoding"
|
||||
"gonum.org/v1/gonum/graph/multi"
|
||||
)
|
||||
|
||||
// Node is a DOT-aware graph.Node.
|
||||
type Node struct {
|
||||
graph.Node
|
||||
dotID string
|
||||
}
|
||||
|
||||
// SetDOTID sets the node's DOT ID. It enables storing the node name read from
|
||||
// a DOT file.
|
||||
func (n *Node) SetDOTID(id string) {
|
||||
n.dotID = id
|
||||
}
|
||||
|
||||
// DOTID returns the node's DOT ID.
|
||||
func (n *Node) DOTID() string {
|
||||
return n.dotID
|
||||
}
|
||||
|
||||
// weightedLine is a DOT-aware multi.WeightedLine. By being a
|
||||
// multi.WeightedLine, it allows for self-loops, which are important for
|
||||
// absorbing Markov chains.
|
||||
type weightedLine struct {
|
||||
multi.WeightedLine
|
||||
}
|
||||
|
||||
// SetAttribute enables storing the weight read from a DOT file. It errors if
|
||||
// an attribute is read that can't be stored in a weightedLine.
|
||||
func (e *weightedLine) SetAttribute(attr encoding.Attribute) error {
|
||||
var err error
|
||||
|
||||
switch attr.Key {
|
||||
case "weight":
|
||||
e.W, err = strconv.ParseFloat(attr.Value, 64)
|
||||
default:
|
||||
err = errors.New("unknown key:" + attr.Key)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
84
cmd/internal/graph/graph.go
Normal file
84
cmd/internal/graph/graph.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
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"
|
||||
"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()}
|
||||
}
|
||||
|
||||
// AbsorbingNodes returns all the nodes in the Markov chain identified as
|
||||
// absorbing nodes.
|
||||
func (g *DirectedGraph) AbsorbingNodes() []graph.Node {
|
||||
absorbingNodes := make([]graph.Node, 0)
|
||||
for nodes := g.Nodes(); nodes.Next(); {
|
||||
u := nodes.Node()
|
||||
successors := g.From(u.ID())
|
||||
if successors.Len() != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
successors.Next()
|
||||
v := successors.Node()
|
||||
if u == v {
|
||||
absorbingNodes = append(absorbingNodes, u)
|
||||
}
|
||||
}
|
||||
|
||||
return absorbingNodes
|
||||
}
|
||||
|
||||
// AdjacencyMatrix returns the graph's adjacency matrix.
|
||||
func (g *DirectedGraph) AdjacencyMatrix() *mat.Dense {
|
||||
adj := simple.NewDirectedMatrix(g.Nodes().Len(), 0, 0, 0)
|
||||
for edges := g.WeightedEdges(); edges.Next(); {
|
||||
e := edges.WeightedEdge()
|
||||
if e.From() == e.To() {
|
||||
continue
|
||||
}
|
||||
|
||||
adj.SetWeightedEdge(e)
|
||||
}
|
||||
|
||||
a := mat.DenseCopyOf(adj.Matrix())
|
||||
|
||||
nodes := adj.Nodes()
|
||||
for i := 0; nodes.Next(); i++ {
|
||||
id := nodes.Node().ID()
|
||||
u := g.Node(id)
|
||||
|
||||
e := g.WeightedEdge(u.ID(), u.ID())
|
||||
if e != nil {
|
||||
a.Set(i, i, e.Weight())
|
||||
}
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
Reference in New Issue
Block a user