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

     1  // Copyright ©2014 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 graph
     6  
     7  // Line is an edge in a multigraph. A Line returns an ID that must
     8  // distinguish Lines sharing Node end points.
     9  type Line interface {
    10  	// From returns the from node of the edge.
    11  	From() Node
    12  
    13  	// To returns the to node of the edge.
    14  	To() Node
    15  
    16  	// ReversedLine returns the edge reversal of the receiver
    17  	// if a reversal is valid for the data type.
    18  	// When a reversal is valid an edge of the same type as
    19  	// the receiver with nodes of the receiver swapped should
    20  	// be returned, otherwise the receiver should be returned
    21  	// unaltered.
    22  	ReversedLine() Line
    23  
    24  	// ID returns the unique ID for the Line.
    25  	ID() int64
    26  }
    27  
    28  // WeightedLine is a weighted multigraph edge.
    29  type WeightedLine interface {
    30  	Line
    31  	Weight() float64
    32  }
    33  
    34  // Multigraph is a generalized multigraph.
    35  type Multigraph interface {
    36  	// Node returns the node with the given ID if it exists
    37  	// in the multigraph, and nil otherwise.
    38  	Node(id int64) Node
    39  
    40  	// Nodes returns all the nodes in the multigraph.
    41  	//
    42  	// Nodes must not return nil.
    43  	Nodes() Nodes
    44  
    45  	// From returns all nodes that can be reached directly
    46  	// from the node with the given ID.
    47  	//
    48  	// From must not return nil.
    49  	From(id int64) Nodes
    50  
    51  	// HasEdgeBetween returns whether an edge exists between
    52  	// nodes with IDs xid and yid without considering direction.
    53  	HasEdgeBetween(xid, yid int64) bool
    54  
    55  	// Lines returns the lines from u to v, with IDs uid and
    56  	// vid, if any such lines exist and nil otherwise. The
    57  	// node v must be directly reachable from u as defined by
    58  	// the From method.
    59  	//
    60  	// Lines must not return nil.
    61  	Lines(uid, vid int64) Lines
    62  }
    63  
    64  // WeightedMultigraph is a weighted multigraph.
    65  type WeightedMultigraph interface {
    66  	Multigraph
    67  
    68  	// WeightedLines returns the weighted lines from u to v
    69  	// with IDs uid and vid if any such lines exist and nil
    70  	// otherwise. The node v must be directly reachable
    71  	// from u as defined by the From method.
    72  	//
    73  	// WeightedLines must not return nil.
    74  	WeightedLines(uid, vid int64) WeightedLines
    75  }
    76  
    77  // UndirectedMultigraph is an undirected multigraph.
    78  type UndirectedMultigraph interface {
    79  	Multigraph
    80  
    81  	// LinesBetween returns the lines between nodes x and y
    82  	// with IDs xid and yid.
    83  	//
    84  	// LinesBetween must not return nil.
    85  	LinesBetween(xid, yid int64) Lines
    86  }
    87  
    88  // WeightedUndirectedMultigraph is a weighted undirected multigraph.
    89  type WeightedUndirectedMultigraph interface {
    90  	WeightedMultigraph
    91  
    92  	// WeightedLinesBetween returns the lines between nodes
    93  	// x and y with IDs xid and yid.
    94  	//
    95  	// WeightedLinesBetween must not return nil.
    96  	WeightedLinesBetween(xid, yid int64) WeightedLines
    97  }
    98  
    99  // DirectedMultigraph is a directed multigraph.
   100  type DirectedMultigraph interface {
   101  	Multigraph
   102  
   103  	// HasEdgeFromTo returns whether an edge exists
   104  	// in the multigraph from u to v with IDs uid
   105  	// and vid.
   106  	HasEdgeFromTo(uid, vid int64) bool
   107  
   108  	// To returns all nodes that can reach directly
   109  	// to the node with the given ID.
   110  	//
   111  	// To must not return nil.
   112  	To(id int64) Nodes
   113  }
   114  
   115  // WeightedDirectedMultigraph is a weighted directed multigraph.
   116  type WeightedDirectedMultigraph interface {
   117  	WeightedMultigraph
   118  
   119  	// HasEdgeFromTo returns whether an edge exists
   120  	// in the multigraph from u to v with IDs uid
   121  	// and vid.
   122  	HasEdgeFromTo(uid, vid int64) bool
   123  
   124  	// To returns all nodes that can reach directly
   125  	// to the node with the given ID.
   126  	//
   127  	// To must not return nil.
   128  	To(id int64) Nodes
   129  }
   130  
   131  // LineAdder is an interface for adding lines to a multigraph.
   132  type LineAdder interface {
   133  	// NewLine returns a new Line from the source to the destination node.
   134  	NewLine(from, to Node) Line
   135  
   136  	// SetLine adds a Line from one node to another.
   137  	// If the multigraph supports node addition the nodes
   138  	// will be added if they do not exist, otherwise
   139  	// SetLine will panic.
   140  	// Whether l, l.From() and l.To() are stored
   141  	// within the graph is implementation dependent.
   142  	SetLine(l Line)
   143  }
   144  
   145  // WeightedLineAdder is an interface for adding lines to a multigraph.
   146  type WeightedLineAdder interface {
   147  	// NewWeightedLine returns a new WeightedLine from
   148  	// the source to the destination node.
   149  	NewWeightedLine(from, to Node, weight float64) WeightedLine
   150  
   151  	// SetWeightedLine adds a weighted line from one node
   152  	// to another. If the multigraph supports node addition
   153  	// the nodes will be added if they do not exist,
   154  	// otherwise SetWeightedLine will panic.
   155  	// Whether l, l.From() and l.To() are stored
   156  	// within the graph is implementation dependent.
   157  	SetWeightedLine(l WeightedLine)
   158  }
   159  
   160  // LineRemover is an interface for removing lines from a multigraph.
   161  type LineRemover interface {
   162  	// RemoveLine removes the line with the given end
   163  	// and line IDs, leaving the terminal nodes. If
   164  	// the line does not exist it is a no-op.
   165  	RemoveLine(fid, tid, id int64)
   166  }
   167  
   168  // MultigraphBuilder is a multigraph that can have nodes and lines added.
   169  type MultigraphBuilder interface {
   170  	NodeAdder
   171  	LineAdder
   172  }
   173  
   174  // WeightedMultigraphBuilder is a multigraph that can have nodes and weighted lines added.
   175  type WeightedMultigraphBuilder interface {
   176  	NodeAdder
   177  	WeightedLineAdder
   178  }
   179  
   180  // UndirectedMultgraphBuilder is an undirected multigraph builder.
   181  type UndirectedMultigraphBuilder interface {
   182  	UndirectedMultigraph
   183  	MultigraphBuilder
   184  }
   185  
   186  // UndirectedWeightedMultigraphBuilder is an undirected weighted multigraph builder.
   187  type UndirectedWeightedMultigraphBuilder interface {
   188  	UndirectedMultigraph
   189  	WeightedMultigraphBuilder
   190  }
   191  
   192  // DirectedMultigraphBuilder is a directed multigraph builder.
   193  type DirectedMultigraphBuilder interface {
   194  	DirectedMultigraph
   195  	MultigraphBuilder
   196  }
   197  
   198  // DirectedWeightedMultigraphBuilder is a directed weighted multigraph builder.
   199  type DirectedWeightedMultigraphBuilder interface {
   200  	DirectedMultigraph
   201  	WeightedMultigraphBuilder
   202  }