directed.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. "gonum.org/v1/gonum/graph"
  8. "gonum.org/v1/gonum/graph/internal/uid"
  9. "gonum.org/v1/gonum/graph/iterator"
  10. )
  11. var (
  12. dg *DirectedGraph
  13. _ graph.Graph = dg
  14. _ graph.Directed = dg
  15. _ graph.NodeAdder = dg
  16. _ graph.NodeRemover = dg
  17. _ graph.EdgeAdder = dg
  18. _ graph.EdgeRemover = dg
  19. )
  20. // DirectedGraph implements a generalized directed graph.
  21. type DirectedGraph struct {
  22. nodes map[int64]graph.Node
  23. from map[int64]map[int64]graph.Edge
  24. to map[int64]map[int64]graph.Edge
  25. nodeIDs uid.Set
  26. }
  27. // NewDirectedGraph returns a DirectedGraph.
  28. func NewDirectedGraph() *DirectedGraph {
  29. return &DirectedGraph{
  30. nodes: make(map[int64]graph.Node),
  31. from: make(map[int64]map[int64]graph.Edge),
  32. to: make(map[int64]map[int64]graph.Edge),
  33. nodeIDs: uid.NewSet(),
  34. }
  35. }
  36. // AddNode adds n to the graph. It panics if the added node ID matches an existing node ID.
  37. func (g *DirectedGraph) AddNode(n graph.Node) {
  38. if _, exists := g.nodes[n.ID()]; exists {
  39. panic(fmt.Sprintf("simple: node ID collision: %d", n.ID()))
  40. }
  41. g.nodes[n.ID()] = n
  42. g.nodeIDs.Use(n.ID())
  43. }
  44. // Edge returns the edge from u to v if such an edge exists and nil otherwise.
  45. // The node v must be directly reachable from u as defined by the From method.
  46. func (g *DirectedGraph) Edge(uid, vid int64) graph.Edge {
  47. edge, ok := g.from[uid][vid]
  48. if !ok {
  49. return nil
  50. }
  51. return edge
  52. }
  53. // Edges returns all the edges in the graph.
  54. func (g *DirectedGraph) Edges() graph.Edges {
  55. var edges []graph.Edge
  56. for _, u := range g.nodes {
  57. for _, e := range g.from[u.ID()] {
  58. edges = append(edges, e)
  59. }
  60. }
  61. if len(edges) == 0 {
  62. return graph.Empty
  63. }
  64. return iterator.NewOrderedEdges(edges)
  65. }
  66. // From returns all nodes in g that can be reached directly from n.
  67. func (g *DirectedGraph) From(id int64) graph.Nodes {
  68. if _, ok := g.from[id]; !ok {
  69. return graph.Empty
  70. }
  71. from := make([]graph.Node, len(g.from[id]))
  72. i := 0
  73. for vid := range g.from[id] {
  74. from[i] = g.nodes[vid]
  75. i++
  76. }
  77. if len(from) == 0 {
  78. return graph.Empty
  79. }
  80. return iterator.NewOrderedNodes(from)
  81. }
  82. // HasEdgeBetween returns whether an edge exists between nodes x and y without
  83. // considering direction.
  84. func (g *DirectedGraph) HasEdgeBetween(xid, yid int64) bool {
  85. if _, ok := g.from[xid][yid]; ok {
  86. return true
  87. }
  88. _, ok := g.from[yid][xid]
  89. return ok
  90. }
  91. // HasEdgeFromTo returns whether an edge exists in the graph from u to v.
  92. func (g *DirectedGraph) HasEdgeFromTo(uid, vid int64) bool {
  93. if _, ok := g.from[uid][vid]; !ok {
  94. return false
  95. }
  96. return true
  97. }
  98. // NewEdge returns a new Edge from the source to the destination node.
  99. func (g *DirectedGraph) NewEdge(from, to graph.Node) graph.Edge {
  100. return &Edge{F: from, T: to}
  101. }
  102. // NewNode returns a new unique Node to be added to g. The Node's ID does
  103. // not become valid in g until the Node is added to g.
  104. func (g *DirectedGraph) NewNode() graph.Node {
  105. if len(g.nodes) == 0 {
  106. return Node(0)
  107. }
  108. if int64(len(g.nodes)) == uid.Max {
  109. panic("simple: cannot allocate node: no slot")
  110. }
  111. return Node(g.nodeIDs.NewID())
  112. }
  113. // Node returns the node with the given ID if it exists in the graph,
  114. // and nil otherwise.
  115. func (g *DirectedGraph) Node(id int64) graph.Node {
  116. return g.nodes[id]
  117. }
  118. // Nodes returns all the nodes in the graph.
  119. func (g *DirectedGraph) Nodes() graph.Nodes {
  120. if len(g.nodes) == 0 {
  121. return graph.Empty
  122. }
  123. nodes := make([]graph.Node, len(g.nodes))
  124. i := 0
  125. for _, n := range g.nodes {
  126. nodes[i] = n
  127. i++
  128. }
  129. return iterator.NewOrderedNodes(nodes)
  130. }
  131. // RemoveEdge removes the edge with the given end point IDs from the graph, leaving the terminal
  132. // nodes. If the edge does not exist it is a no-op.
  133. func (g *DirectedGraph) RemoveEdge(fid, tid int64) {
  134. if _, ok := g.nodes[fid]; !ok {
  135. return
  136. }
  137. if _, ok := g.nodes[tid]; !ok {
  138. return
  139. }
  140. delete(g.from[fid], tid)
  141. delete(g.to[tid], fid)
  142. }
  143. // RemoveNode removes the node with the given ID from the graph, as well as any edges attached
  144. // to it. If the node is not in the graph it is a no-op.
  145. func (g *DirectedGraph) RemoveNode(id int64) {
  146. if _, ok := g.nodes[id]; !ok {
  147. return
  148. }
  149. delete(g.nodes, id)
  150. for from := range g.from[id] {
  151. delete(g.to[from], id)
  152. }
  153. delete(g.from, id)
  154. for to := range g.to[id] {
  155. delete(g.from[to], id)
  156. }
  157. delete(g.to, id)
  158. g.nodeIDs.Release(id)
  159. }
  160. // SetEdge adds e, an edge from one node to another. If the nodes do not exist, they are added
  161. // and are set to the nodes of the edge otherwise.
  162. // It will panic if the IDs of the e.From and e.To are equal.
  163. func (g *DirectedGraph) SetEdge(e graph.Edge) {
  164. var (
  165. from = e.From()
  166. fid = from.ID()
  167. to = e.To()
  168. tid = to.ID()
  169. )
  170. if fid == tid {
  171. panic("simple: adding self edge")
  172. }
  173. if _, ok := g.nodes[fid]; !ok {
  174. g.AddNode(from)
  175. } else {
  176. g.nodes[fid] = from
  177. }
  178. if _, ok := g.nodes[tid]; !ok {
  179. g.AddNode(to)
  180. } else {
  181. g.nodes[tid] = to
  182. }
  183. if fm, ok := g.from[fid]; ok {
  184. fm[tid] = e
  185. } else {
  186. g.from[fid] = map[int64]graph.Edge{tid: e}
  187. }
  188. if tm, ok := g.to[tid]; ok {
  189. tm[fid] = e
  190. } else {
  191. g.to[tid] = map[int64]graph.Edge{fid: e}
  192. }
  193. }
  194. // To returns all nodes in g that can reach directly to n.
  195. func (g *DirectedGraph) To(id int64) graph.Nodes {
  196. if _, ok := g.to[id]; !ok {
  197. return graph.Empty
  198. }
  199. to := make([]graph.Node, len(g.to[id]))
  200. i := 0
  201. for uid := range g.to[id] {
  202. to[i] = g.nodes[uid]
  203. i++
  204. }
  205. if len(to) == 0 {
  206. return graph.Empty
  207. }
  208. return iterator.NewOrderedNodes(to)
  209. }