Skip to content

Commit

Permalink
add alternative simple graph constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
vreuter committed Nov 16, 2024
1 parent eb95ab8 commit 112d1f9
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions modules/graph/src/main/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

0 comments on commit 112d1f9

Please sign in to comment.