gv2adj/cmd/internal/graph/undirected.go

42 lines
1.4 KiB
Go
Raw Normal View History

2025-05-09 01:59:58 +00:00
/*
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.
2025-05-09 03:57:28 +00:00
type WeightedUndirectedGraph struct {
2025-05-09 01:59:58 +00:00
*multi.WeightedUndirectedGraph
}
// NewUndirectedGraph returns a graph with no nodes or edges.
2025-05-09 03:57:28 +00:00
func NewWeightedUndirectedGraph() *WeightedUndirectedGraph {
return &WeightedUndirectedGraph{WeightedUndirectedGraph: multi.NewWeightedUndirectedGraph()}
2025-05-09 01:59:58 +00:00
}
// AdjacencyMatrix returns the graph's adjacency matrix.
2025-05-09 03:57:28 +00:00
func (g *WeightedUndirectedGraph) AdjacencyMatrix() *mat.Dense {
2025-05-09 01:59:58 +00:00
adj := simple.NewUndirectedMatrix(g.Nodes().Len(), 0, 0, 0)
matrix := toAdjMatrix(g, adj)
return matrix
}