diff --git a/modules/graph/src/main/scala/package.scala b/modules/graph/src/main/scala/package.scala index 1814248..7377bdf 100644 --- a/modules/graph/src/main/scala/package.scala +++ b/modules/graph/src/main/scala/package.scala @@ -7,7 +7,28 @@ import scalax.collection.immutable /** Graph-related operations */ package object graph: - private[graph] type SimplestGraph[N] = immutable.Graph[N, UnDiEdge[N]] + type SimplestGraph[N] = immutable.Graph[N, UnDiEdge[N]] + + /** Construct a simple graph from an adjancency list/'matrix'. + * + * @tparam N + * The node type + * @param adj + * The adjacency list / 'matrix' which encodes the edge relationships / node adjacencies + */ + def buildSimpleGraph[N](adj: List[(N, Set[N])]): SimplestGraph[N] = + val singleGraphs = adj.map(fromSingleNodeAndNeighbors[N].tupled) + monoidKForSimplestGraphByOuterElements.combineAllK(singleGraphs) + + /** Construct a simple graph from a set of nodes and set of edge endpoints. + * + * @tparam N + * The node type + * @param adj + * The endpoints of edges to build + */ + def buildSimpleGraph[N](nodes: Set[N], endpoints: Set[(N, N)]): SimplestGraph[N] = + immutable.Graph.from(nodes, endpoints.map(_ ~ _)) /** Start with an empty graph, and combine by taking union of node and edge sets. */ def monoidKForSimplestGraphByOuterElements: MonoidK[SimplestGraph] = new: @@ -20,15 +41,4 @@ package object graph: private def fromSingleNodeAndNeighbors[N](n: N, neighbors: Set[N]): SimplestGraph[N] = immutable.Graph.from(Set(n), neighbors.map(n ~ _)) - - /** Construct a simple graph from an adjancency list/'matrix'. - * - * @tparam N - * The node type - * @param adj - * The adjacency list / 'matrix' which encodes the edge relationships / node adjacencies - */ - def buildSimpleGraph[N](adj: List[(N, Set[N])]): SimplestGraph[N] = - val singleGraphs = adj.map(fromSingleNodeAndNeighbors[N].tupled) - monoidKForSimplestGraphByOuterElements.combineAllK(singleGraphs) end graph