undirected.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 simple
  5. import (
  6. "fmt"
  7. "golang.org/x/tools/container/intsets"
  8. "k8s.io/kubernetes/third_party/forked/gonum/graph"
  9. )
  10. // UndirectedGraph implements a generalized undirected graph.
  11. type UndirectedGraph struct {
  12. nodes map[int]graph.Node
  13. edges map[int]edgeHolder
  14. self, absent float64
  15. freeIDs intsets.Sparse
  16. usedIDs intsets.Sparse
  17. }
  18. // NewUndirectedGraph returns an UndirectedGraph with the specified self and absent
  19. // edge weight values.
  20. func NewUndirectedGraph(self, absent float64) *UndirectedGraph {
  21. return &UndirectedGraph{
  22. nodes: make(map[int]graph.Node),
  23. edges: make(map[int]edgeHolder),
  24. self: self,
  25. absent: absent,
  26. }
  27. }
  28. // NewNodeID returns a new unique ID for a node to be added to g. The returned ID does
  29. // not become a valid ID in g until it is added to g.
  30. func (g *UndirectedGraph) NewNodeID() int {
  31. if len(g.nodes) == 0 {
  32. return 0
  33. }
  34. if len(g.nodes) == maxInt {
  35. panic(fmt.Sprintf("simple: cannot allocate node: no slot"))
  36. }
  37. var id int
  38. if g.freeIDs.Len() != 0 && g.freeIDs.TakeMin(&id) {
  39. return id
  40. }
  41. if id = g.usedIDs.Max(); id < maxInt {
  42. return id + 1
  43. }
  44. for id = 0; id < maxInt; id++ {
  45. if !g.usedIDs.Has(id) {
  46. return id
  47. }
  48. }
  49. panic("unreachable")
  50. }
  51. // AddNode adds n to the graph. It panics if the added node ID matches an existing node ID.
  52. func (g *UndirectedGraph) AddNode(n graph.Node) {
  53. if _, exists := g.nodes[n.ID()]; exists {
  54. panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
  55. }
  56. g.nodes[n.ID()] = n
  57. g.edges[n.ID()] = &sliceEdgeHolder{self: n.ID()}
  58. g.freeIDs.Remove(n.ID())
  59. g.usedIDs.Insert(n.ID())
  60. }
  61. // RemoveNode removes n from the graph, as well as any edges attached to it. If the node
  62. // is not in the graph it is a no-op.
  63. func (g *UndirectedGraph) RemoveNode(n graph.Node) {
  64. if _, ok := g.nodes[n.ID()]; !ok {
  65. return
  66. }
  67. delete(g.nodes, n.ID())
  68. g.edges[n.ID()].Visit(func(neighbor int, edge graph.Edge) {
  69. g.edges[neighbor] = g.edges[neighbor].Delete(n.ID())
  70. })
  71. delete(g.edges, n.ID())
  72. g.freeIDs.Insert(n.ID())
  73. g.usedIDs.Remove(n.ID())
  74. }
  75. // SetEdge adds e, an edge from one node to another. If the nodes do not exist, they are added.
  76. // It will panic if the IDs of the e.From and e.To are equal.
  77. func (g *UndirectedGraph) SetEdge(e graph.Edge) {
  78. var (
  79. from = e.From()
  80. fid = from.ID()
  81. to = e.To()
  82. tid = to.ID()
  83. )
  84. if fid == tid {
  85. panic("simple: adding self edge")
  86. }
  87. if !g.Has(from) {
  88. g.AddNode(from)
  89. }
  90. if !g.Has(to) {
  91. g.AddNode(to)
  92. }
  93. g.edges[fid] = g.edges[fid].Set(tid, e)
  94. g.edges[tid] = g.edges[tid].Set(fid, e)
  95. }
  96. // RemoveEdge removes e from the graph, leaving the terminal nodes. If the edge does not exist
  97. // it is a no-op.
  98. func (g *UndirectedGraph) RemoveEdge(e graph.Edge) {
  99. from, to := e.From(), e.To()
  100. if _, ok := g.nodes[from.ID()]; !ok {
  101. return
  102. }
  103. if _, ok := g.nodes[to.ID()]; !ok {
  104. return
  105. }
  106. g.edges[from.ID()] = g.edges[from.ID()].Delete(to.ID())
  107. g.edges[to.ID()] = g.edges[to.ID()].Delete(from.ID())
  108. }
  109. // Node returns the node in the graph with the given ID.
  110. func (g *UndirectedGraph) Node(id int) graph.Node {
  111. return g.nodes[id]
  112. }
  113. // Has returns whether the node exists within the graph.
  114. func (g *UndirectedGraph) Has(n graph.Node) bool {
  115. _, ok := g.nodes[n.ID()]
  116. return ok
  117. }
  118. // Nodes returns all the nodes in the graph.
  119. func (g *UndirectedGraph) Nodes() []graph.Node {
  120. nodes := make([]graph.Node, len(g.nodes))
  121. i := 0
  122. for _, n := range g.nodes {
  123. nodes[i] = n
  124. i++
  125. }
  126. return nodes
  127. }
  128. // Edges returns all the edges in the graph.
  129. func (g *UndirectedGraph) Edges() []graph.Edge {
  130. var edges []graph.Edge
  131. seen := make(map[[2]int]struct{})
  132. for _, u := range g.edges {
  133. u.Visit(func(neighbor int, e graph.Edge) {
  134. uid := e.From().ID()
  135. vid := e.To().ID()
  136. if _, ok := seen[[2]int{uid, vid}]; ok {
  137. return
  138. }
  139. seen[[2]int{uid, vid}] = struct{}{}
  140. seen[[2]int{vid, uid}] = struct{}{}
  141. edges = append(edges, e)
  142. })
  143. }
  144. return edges
  145. }
  146. // From returns all nodes in g that can be reached directly from n.
  147. func (g *UndirectedGraph) From(n graph.Node) []graph.Node {
  148. if !g.Has(n) {
  149. return nil
  150. }
  151. nodes := make([]graph.Node, g.edges[n.ID()].Len())
  152. i := 0
  153. g.edges[n.ID()].Visit(func(neighbor int, edge graph.Edge) {
  154. nodes[i] = g.nodes[neighbor]
  155. i++
  156. })
  157. return nodes
  158. }
  159. // HasEdgeBetween returns whether an edge exists between nodes x and y.
  160. func (g *UndirectedGraph) HasEdgeBetween(x, y graph.Node) bool {
  161. _, ok := g.edges[x.ID()].Get(y.ID())
  162. return ok
  163. }
  164. // Edge returns the edge from u to v if such an edge exists and nil otherwise.
  165. // The node v must be directly reachable from u as defined by the From method.
  166. func (g *UndirectedGraph) Edge(u, v graph.Node) graph.Edge {
  167. return g.EdgeBetween(u, v)
  168. }
  169. // EdgeBetween returns the edge between nodes x and y.
  170. func (g *UndirectedGraph) EdgeBetween(x, y graph.Node) graph.Edge {
  171. // We don't need to check if neigh exists because
  172. // it's implicit in the edges access.
  173. if !g.Has(x) {
  174. return nil
  175. }
  176. edge, _ := g.edges[x.ID()].Get(y.ID())
  177. return edge
  178. }
  179. // Weight returns the weight for the edge between x and y if Edge(x, y) returns a non-nil Edge.
  180. // If x and y are the same node or there is no joining edge between the two nodes the weight
  181. // value returned is either the graph's absent or self value. Weight returns true if an edge
  182. // exists between x and y or if x and y have the same ID, false otherwise.
  183. func (g *UndirectedGraph) Weight(x, y graph.Node) (w float64, ok bool) {
  184. xid := x.ID()
  185. yid := y.ID()
  186. if xid == yid {
  187. return g.self, true
  188. }
  189. if n, ok := g.edges[xid]; ok {
  190. if e, ok := n.Get(yid); ok {
  191. return e.Weight(), true
  192. }
  193. }
  194. return g.absent, false
  195. }
  196. // Degree returns the degree of n in g.
  197. func (g *UndirectedGraph) Degree(n graph.Node) int {
  198. if _, ok := g.nodes[n.ID()]; !ok {
  199. return 0
  200. }
  201. return g.edges[n.ID()].Len()
  202. }