multigraph.go 5.6 KB

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