gv2adj/cmd/internal/graph/undirected.go

42 lines
1.3 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.
type UndirectedGraph struct {
*multi.WeightedUndirectedGraph
}
// NewUndirectedGraph returns a graph with no nodes or edges.
func NewUndirectedGraph() *UndirectedGraph {
return &UndirectedGraph{WeightedUndirectedGraph: multi.NewWeightedUndirectedGraph()}
}
// AdjacencyMatrix returns the graph's adjacency matrix.
func (g *UndirectedGraph) AdjacencyMatrix() *mat.Dense {
adj := simple.NewUndirectedMatrix(g.Nodes().Len(), 0, 0, 0)
matrix := toAdjMatrix(g, adj)
return matrix
}