github.com/gopherd/gonum@v0.0.4/graph/path/weight.go (about)

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package path
     6  
     7  import (
     8  	"math"
     9  
    10  	"github.com/gopherd/gonum/graph"
    11  	"github.com/gopherd/gonum/graph/traverse"
    12  )
    13  
    14  // Weighted is a weighted graph. It is a subset of graph.Weighted.
    15  type Weighted interface {
    16  	// Weight returns the weight for the edge between
    17  	// x and y with IDs xid and yid if Edge(xid, yid)
    18  	// returns a non-nil Edge.
    19  	// If x and y are the same node or there is no
    20  	// joining edge between the two nodes the weight
    21  	// value returned is implementation dependent.
    22  	// Weight returns true if an edge exists between
    23  	// x and y or if x and y have the same ID, false
    24  	// otherwise.
    25  	Weight(xid, yid int64) (w float64, ok bool)
    26  }
    27  
    28  // Weighting is a mapping between a pair of nodes and a weight. It follows the
    29  // semantics of the Weighter interface.
    30  type Weighting func(xid, yid int64) (w float64, ok bool)
    31  
    32  // UniformCost returns a Weighting that returns an edge cost of 1 for existing
    33  // edges, zero for node identity and Inf for otherwise absent edges.
    34  func UniformCost(g traverse.Graph) Weighting {
    35  	return func(xid, yid int64) (w float64, ok bool) {
    36  		if xid == yid {
    37  			return 0, true
    38  		}
    39  		if e := g.Edge(xid, yid); e != nil {
    40  			return 1, true
    41  		}
    42  		return math.Inf(1), false
    43  	}
    44  }
    45  
    46  // Heuristic returns an estimate of the cost of travelling between two nodes.
    47  type Heuristic func(x, y graph.Node) float64
    48  
    49  // HeuristicCoster wraps the HeuristicCost method. A graph implementing the
    50  // interface provides a heuristic between any two given nodes.
    51  type HeuristicCoster interface {
    52  	HeuristicCost(x, y graph.Node) float64
    53  }