Data.Graph.Weighted
- Package
- purescript-hylograph-graph
- Repository
- afcondon/purescript-hylograph-graph
Data.Graph.Weighted
Weighted directed graph with efficient forward and reverse adjacency lookups. This is designed for flow graphs like Sankey diagrams where we need both outgoing edges (for forward traversal) and incoming edges (for backward traversal).
#WeightedDigraph Source
newtype WeightedDigraph node weightA weighted directed graph with efficient forward and reverse adjacency.
Maintains both forward (source -> targets) and reverse (target -> sources) adjacency maps for O(1) lookups in both directions.
#WeightedEdge Source
type WeightedEdge node weight = { source :: node, target :: node, weight :: weight }A weighted edge in a directed graph
#empty Source
empty :: forall node weight. WeightedDigraph node weightCreate an empty weighted digraph
#fromEdges Source
fromEdges :: forall node weight. Ord node => Array (WeightedEdge node weight) -> WeightedDigraph node weightBuild a weighted digraph from an array of edges.
This is the primary constructor. Nodes are inferred from edges.
For isolated nodes (no edges), use addEdge or extend this API.
#addEdge Source
addEdge :: forall node weight. Ord node => node -> node -> weight -> WeightedDigraph node weight -> WeightedDigraph node weightAdd an edge to the graph.
If the edge already exists, it is NOT replaced (first edge wins). Both source and target nodes are added to the node set.
#nodes Source
nodes :: forall node weight. WeightedDigraph node weight -> Array nodeGet all nodes in the graph
#edges Source
edges :: forall node weight. WeightedDigraph node weight -> Array (WeightedEdge node weight)Get all edges in the graph (in insertion order)
#outgoing Source
outgoing :: forall node weight. Ord node => node -> WeightedDigraph node weight -> Array { target :: node, weight :: weight }Get outgoing edges from a node (O(1) lookup)
Returns array of { target, weight } for edges source -> target
#incoming Source
incoming :: forall node weight. Ord node => node -> WeightedDigraph node weight -> Array { source :: node, weight :: weight }Get incoming edges to a node (O(1) lookup)
Returns array of { source, weight } for edges source -> target
#sources Source
sources :: forall node weight. Ord node => WeightedDigraph node weight -> Array nodeGet source nodes (nodes with no incoming edges)
In a flow graph, these are entry points.
#sinks Source
sinks :: forall node weight. Ord node => WeightedDigraph node weight -> Array nodeGet sink nodes (nodes with no outgoing edges)
In a flow graph, these are exit points.
#nodeCount Source
nodeCount :: forall node weight. WeightedDigraph node weight -> IntGet the number of nodes
#edgeCount Source
edgeCount :: forall node weight. WeightedDigraph node weight -> IntGet the number of edges
#hasNode Source
hasNode :: forall node weight. Ord node => node -> WeightedDigraph node weight -> BooleanCheck if a node exists in the graph
#hasEdge Source
hasEdge :: forall node weight. Ord node => node -> node -> WeightedDigraph node weight -> BooleanCheck if an edge exists (ignoring weight)
#toSimpleGraph Source
toSimpleGraph :: forall node weight. Ord node => WeightedDigraph node weight -> SimpleGraph nodeConvert to SimpleGraph (for algorithm reuse)
Drops weights and converts to adjacency-set representation.