| 1 | // Copyright 2026 The Go 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 provides a common abstraction for directed graphs and standard |
| 6 | // graph algorithms. |
| 7 | // |
| 8 | // In general, this package does not provide or assume any concrete graph |
| 9 | // representation. It's up to the caller of this package to implement the |
| 10 | // [Graph] interface, either directly or as an adapter around another type. |
| 11 | package graph |
| 12 | |
| 13 | import "iter" |
| 14 | |
| 15 | // A Graph implements a directed graph where nodes in the graph are identified |
| 16 | // by the NodeID type. |
| 17 | // |
| 18 | // If a concrete graph type stores additional information about nodes and/or |
| 19 | // edges, it will conventionally provide methods of the form: |
| 20 | // |
| 21 | // Node(node NodeID) nodeInfo |
| 22 | // Edge(from, to NodeID) edgeInfo |
| 23 | type Graph[NodeID comparable] interface { |
| 24 | // Nodes yields all nodes in this graph. |
| 25 | Nodes() iter.Seq[NodeID] |
| 26 | |
| 27 | // NumNodes returns the total number of nodes in this graph. |
| 28 | NumNodes() int |
| 29 | |
| 30 | // Out yields the out-edges of node. Out must be deterministic, though |
| 31 | // otherwise there is no constraint on the order of the returned sequence. |
| 32 | Out(node NodeID) iter.Seq[NodeID] |
| 33 | } |