Module

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 weight

A 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 weight

Create an empty weighted digraph

#fromEdges Source

fromEdges :: forall node weight. Ord node => Array (WeightedEdge node weight) -> WeightedDigraph node weight

Build 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 weight

Add 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 node

Get 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 node

Get 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 node

Get sink nodes (nodes with no outgoing edges)

In a flow graph, these are exit points.

#nodeCount Source

nodeCount :: forall node weight. WeightedDigraph node weight -> Int

Get the number of nodes

#edgeCount Source

edgeCount :: forall node weight. WeightedDigraph node weight -> Int

Get the number of edges

#hasNode Source

hasNode :: forall node weight. Ord node => node -> WeightedDigraph node weight -> Boolean

Check if a node exists in the graph

#hasEdge Source

hasEdge :: forall node weight. Ord node => node -> node -> WeightedDigraph node weight -> Boolean

Check if an edge exists (ignoring weight)

#toSimpleGraph Source

toSimpleGraph :: forall node weight. Ord node => WeightedDigraph node weight -> SimpleGraph node

Convert to SimpleGraph (for algorithm reuse)

Drops weights and converts to adjacency-set representation.