Files
gonum/graph/multi/multi.go
Dan Kortschak 2b0dcc4a5e graph/multi: test graph implementations with testgraph package
Fix bugs found by testgraph.
2018-11-21 11:35:03 +10:30

111 lines
2.7 KiB
Go

// Copyright ©2014 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package multi
import "gonum.org/v1/gonum/graph"
// Node here is a duplication of simple.Node
// to avoid needing to import both packages.
// Node is a simple graph node.
type Node int64
// ID returns the ID number of the node.
func (n Node) ID() int64 {
return int64(n)
}
// Edge is a collection of multigraph edges sharing end points.
type Edge struct {
F, T graph.Node
graph.Lines
}
// From returns the from-node of the edge.
func (e Edge) From() graph.Node { return e.F }
// To returns the to-node of the edge.
func (e Edge) To() graph.Node { return e.T }
// Line is a multigraph edge.
type Line struct {
F, T graph.Node
UID int64
}
// From returns the from-node of the line.
func (l Line) From() graph.Node { return l.F }
// To returns the to-node of the line.
func (l Line) To() graph.Node { return l.T }
// ID returns the ID of the line.
func (l Line) ID() int64 { return l.UID }
// WeightedEdge is a collection of weighted multigraph edges sharing end points.
type WeightedEdge struct {
F, T graph.Node
graph.WeightedLines
// WeightFunc calculates the aggregate
// weight of the lines in Lines. If
// WeightFunc is nil, the sum of weights
// is used as the edge weight.
// The graph.WeightedLines can be expected
// to be positioned at the first line of
// the iterator on entry and must be
// Reset before exit.
// WeightFunc must accept a nil input.
WeightFunc func(graph.WeightedLines) float64
}
// From returns the from-node of the edge.
func (e WeightedEdge) From() graph.Node { return e.F }
// To returns the to-node of the edge.
func (e WeightedEdge) To() graph.Node { return e.T }
// Weight returns the weight of the edge. Weight uses WeightFunc
// field to calculate the weight, so the WeightedLines field is
// expected to be positioned at the first line and is reset before
// Weight returns.
func (e WeightedEdge) Weight() float64 {
if e.WeightFunc != nil {
return e.WeightFunc(e.WeightedLines)
}
if e.WeightedLines == nil {
return 0
}
var w float64
for e.Next() {
w += e.WeightedLine().Weight()
}
e.WeightedLines.Reset()
return w
}
// WeightedLine is a weighted multigraph edge.
type WeightedLine struct {
F, T graph.Node
W float64
UID int64
}
// From returns the from-node of the line.
func (l WeightedLine) From() graph.Node { return l.F }
// To returns the to-node of the line.
func (l WeightedLine) To() graph.Node { return l.T }
// ID returns the ID of the line.
func (l WeightedLine) ID() int64 { return l.UID }
// Weight returns the weight of the edge.
func (l WeightedLine) Weight() float64 { return l.W }