Satsuma is an open-source, highly efficient graph library for .NET written in C#. It treats “graphs” in the mathematical sense (nodes connected by arcs/edges) rather than as visual charts or plots. Core Concepts & Features
Mixed Graphs: Every graph in Satsuma can contain both directed and undirected arcs simultaneously, with no structural distinction required between directed and undirected graph types.
Elements as Structs: Nodes and arcs are modeled as lightweight Node and Arc structs rather than heavy objects, conserving significant memory.
Built-in Algorithms: Out-of-the-box support for Dijkstra, BFS, DFS, Bellman-Ford, A, Kruskal, Prim, Max Flow (Preflow), Network Simplex, and TSP Solvers*.
Adaptors: Built-in wrappers that temporarily alter a graph structure dynamically (e.g., creating subgraphs, reversing arcs, or hiding nodes) without changing the underlying graph. Installation
You can integrate Satsuma by installing the modern, cross-platform port maintained via NuGet: Install-Package Unchase.Satsuma.Core Use code with caution.
(Optional sub-packages like Unchase.Satsuma.Algorithms or Unchase.Satsuma.TSP are available depending on your functional requirements.) Getting Started: Code Examples 1. Creating and Building a Graph
You can use CustomGraph to build structures dynamically. Arcs are added by explicitly declaring them as Directed or Undirected.
using Satsuma; // Initialize a buildable graph instance CustomGraph g = new CustomGraph(); // Add lightweight nodes Node a = g.AddNode(); Node b = g.AddNode(); Node c = g.AddNode(); // Add arcs connecting the nodes Arc directedArc = g.AddArc(a, b, Directedness.Directed); // a -> b Arc undirectedEdge = g.AddArc(b, c, Directedness.Undirected); // b <-> c Console.WriteLine(\("Nodes: {g.NodeCount()}, Arcs: {g.ArcCount()}"); </code> Use code with caution. 2. Fluent Synthesis (Alternative)</p> <p>For faster creation, extension methods allow you to map numeric IDs directly into graph configurations:</p> <p><code>var fluentGraph = new CustomGraph() .WithNodes(1, 2, 3) .WithArcs( (1, 2, Directedness.Directed), (2, 3, Directedness.Undirected) ); </code> Use code with caution. 3. Traversing and Processing the Graph</p> <p>The library provides clean syntax to loop over nodes, find neighbors, or pull endpoints from an arc.</p> <p><code>// Loop through all nodes foreach (Node n in g.Nodes()) { // Print the endpoints of arcs connected to this node foreach (Arc arc in g.Arcs(n)) { Node u = g.U(arc); // Endpoint 1 Node v = g.V(arc); // Endpoint 2 Console.WriteLine(\)“Arc links Node {u} and Node {v}”); } } Use code with caution. 4. Running a Pathfinding Algorithm (Dijkstra)
Most algorithms require an execution class where you pass the target graph and the necessary weight or cost function.
using Satsuma; // Define a simple weight function for your arcs Func Use code with caution. Essential Execution Tips
Satsuma is a graph library for .NET, written in C#. · GitHub
Leave a Reply