gv2adj/cmd/internal/graph/directed.go

42 lines
1.4 KiB
Go
Raw Normal View History

/*
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/>.
*/
2025-05-07 02:24:28 +00:00
package graph
import (
"gonum.org/v1/gonum/graph/multi"
"gonum.org/v1/gonum/graph/simple"
2025-05-02 05:04:00 +00:00
"gonum.org/v1/gonum/mat"
)
2025-05-07 02:24:28 +00:00
// DirectedGraph embeds a multi.WeightedDirectedGraph (as opposed to
2025-05-05 04:09:01 +00:00
// simple.WeightedDirectedGraph) to handle self loops.
2025-05-09 03:57:28 +00:00
type WeightedDirectedGraph struct {
*multi.WeightedDirectedGraph
}
2025-05-07 02:24:28 +00:00
// NewDirectedGraph returns a graph with no nodes or edges.
2025-05-09 03:57:28 +00:00
func NewWeightedDirectedGraph() *WeightedDirectedGraph {
return &WeightedDirectedGraph{WeightedDirectedGraph: multi.NewWeightedDirectedGraph()}
}
2025-05-04 16:56:34 +00:00
// AdjacencyMatrix returns the graph's adjacency matrix.
2025-05-09 03:57:28 +00:00
func (g *WeightedDirectedGraph) AdjacencyMatrix() *mat.Dense {
adj := simple.NewDirectedMatrix(g.Nodes().Len(), 0, 0, 0)
2025-05-09 01:59:58 +00:00
matrix := toAdjMatrix(g, adj)
return matrix
2025-05-02 05:04:00 +00:00
}