IRI.Maptor.Sta.Graph
2.11.0
dotnet add package IRI.Maptor.Sta.Graph --version 2.11.0
NuGet\Install-Package IRI.Maptor.Sta.Graph -Version 2.11.0
<PackageReference Include="IRI.Maptor.Sta.Graph" Version="2.11.0" />
<PackageVersion Include="IRI.Maptor.Sta.Graph" Version="2.11.0" />
<PackageReference Include="IRI.Maptor.Sta.Graph" />
paket add IRI.Maptor.Sta.Graph --version 2.11.0
#r "nuget: IRI.Maptor.Sta.Graph, 2.11.0"
#:package IRI.Maptor.Sta.Graph@2.11.0
#addin nuget:?package=IRI.Maptor.Sta.Graph&version=2.11.0
#tool nuget:?package=IRI.Maptor.Sta.Graph&version=2.11.0
IRI.Maptor.Sta.Graph
The IRI.Maptor.Sta.Graph library provides a set of graph data structures and algorithms inspired by the Graph chapter of the classic CLRS (Introduction to Algorithms) book.
It supports directed and undirected graphs, weighted graphs, and implements core algorithms for traversal, shortest paths, and more.
✅ Features
- Graph representation: Adjacency List and Adjacency Matrix
- Traversal algorithms:
- Breadth-First Search (BFS)
- Depth-First Search (DFS)
- Shortest path algorithms:
- Dijkstra (non-negative weights)
- Bellman-Ford (handles negative weights, detects negative cycles)
- Floyd Warshall (all-pairs shortest path)
- Minimum spanning tree:
- Prim
- Strongly Connected Components (SCC)
- Utility functions for graph creation and manipulation
✅ Installation
Add the library to your .NET project:
dotnet add package IRI.Maptor.Sta.Graph
Or via NuGet package manager:
Install-Package IRI.Maptor.Sta.Graph
✅ Basic Usage
1. Create a Simple Graph
using IRI.Maptor.Sta.Graph;
// TNode can be any comparable key (e.g., string, int)
// TWeight must be IComparable (e.g., int, double)
var g = new AdjacencyList<string, int>();
// Directed edges
g.AddDirectedEdge("A", "B", 1);
g.AddDirectedEdge("A", "C", 1);
g.AddDirectedEdge("B", "D", 1);
g.AddDirectedEdge("C", "D", 1);
Breadth-First Search (BFS)
// Start BFS from source "A"
var bfs = new BreadthFirstSearch<string, int>(g, startNode: "A");
// Distance (level) from A to D
double level = bfs.GetLevel("D"); // 2
// Reconstruct a shortest path from A to D
var path = bfs.GetPathTo("D"); // ["A", "B", "D"] or ["A", "C", "D"]
// The BFS tree discovered from A (as an adjacency list)
var bfsTree = bfs.SearchResult;
Depth-First Search (DFS) & Topological Sort
// Build a small DAG
var dag = new AdjacencyList<string, int>();
dag.AddDirectedEdge("5", "2", 1);
dag.AddDirectedEdge("5", "0", 1);
dag.AddDirectedEdge("4", "0", 1);
dag.AddDirectedEdge("4", "1", 1);
dag.AddDirectedEdge("2", "3", 1);
dag.AddDirectedEdge("3", "1", 1);
// Run DFS (start node can be any existing node)
var dfs = new DepthFirstSearch<string, int>(dag, startNode: "5");
// Topological order (method name as implemented in the code)
var topo = dfs.CalculateTopologicalSort(); // e.g., ["4","5","2","3","1","0"]
// Get nodes sorted by finish time, if needed:
var finishOrder = dfs.GetSortedNodes(SortType.BasedOnFinishTime);
// Is the original graph cyclic?
bool hasCycle = dfs.IsOriginalGraphCyclic; // should be false for a DAG
Strongly Connected Components (SCC)
// For a directed graph g:
var components = Graph.GetStronglyConnectedComponents<string, int>(g);
// components is List<List<string>>;
// each inner list is one strongly connected component.
Bellman-Ford Algorithm
The Bellman-Ford algorithm finds shortest paths from a source node and can handle negative edge weights. It also detects negative cycles.
// Create a graph with potentially negative weights
var graph = new AdjacencyList<string, double>();
graph.AddDirectedEdge("A", "B", 4.0);
graph.AddDirectedEdge("A", "C", 2.0);
graph.AddDirectedEdge("B", "C", -3.0); // Negative weight allowed
graph.AddDirectedEdge("B", "D", 1.0);
graph.AddDirectedEdge("C", "D", 5.0);
// Run Bellman-Ford from source "A"
var bellmanFord = new BellmanFordProblem<string, double>(graph, "A");
// Check for negative cycles
if (bellmanFord.HasNegativeCycle)
{
Console.WriteLine("Graph contains a negative cycle!");
}
else
{
// Get shortest distance to "D" (returns infinity if no path exists)
double distance = bellmanFord.GetDistance("D");
// Or use TryGetDistance to check if path exists
if (bellmanFord.TryGetDistance("D", out double validDistance))
{
Console.WriteLine($"Distance to D: {validDistance}");
}
// Get shortest path to "D"
List<string>? path = bellmanFord.GetShortestPath("D");
}
// Using Matrix input (similar to Dijkstra)
var matrix = new Matrix(/* adjacency matrix */);
var bellmanFordMatrix = new BellmanFordMatrixProblem(matrix, sourceNodeIndex: 0);
double distance = bellmanFordMatrix.GetDistance(targetNodeIndex: 3);
List<int>? path = bellmanFordMatrix.GetShortestPath(targetNodeIndex: 3);
Minimum Spanning Tree (MST)
Build an undirected weighted graph and compute MSTs with Kruskal or Prim.
var ug = new AdjacencyList<string, int>();
ug.AddUndirectedEdge("A", "B", 4);
ug.AddUndirectedEdge("A", "C", 1);
ug.AddUndirectedEdge("B", "C", 3);
ug.AddUndirectedEdge("B", "D", 2);
ug.AddUndirectedEdge("C", "D", 5);
// Kruskal
var mstKruskal = MinimumSpanningTree.CalculateByKruskal<string, int>(ug);
// Prim
var prim = new PrimAlgorithm<string, int>(ug);
var mstPrim = prim.GetMinimumSpanningTree();
✅ Algorithms Implemented
| Algorithm | Description |
|---|---|
| BFS | Breadth-First Search traversal |
| DFS | Depth-First Search traversal |
| Dijkstra | Single-source shortest path (non-negative weights) |
| Bellman-Ford | Single-source shortest path (handles negative weights, detects negative cycles) |
| FloydWarshall | AllPairs shortest path (handles negative weights) |
| Kruskal | Minimum Spanning Tree |
| Prim | Minimum Spanning Tree |
| SCC | Strongly Connected Components |
✅ References
- Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein (CLRS)
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- IRI.Maptor.Sta.Common (>= 2.11.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on IRI.Maptor.Sta.Graph:
| Package | Downloads |
|---|---|
|
IRI.Maptor.Sta.Spatial
A .NET standard library to work with spatial types, structures and algorithms (GeoJson, Geometry, KdTree, RTree, Delaunay, Simplification, etc.) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.11.0 | 46 | 7/4/2026 |
| 2.11.0-alpha | 48 | 7/4/2026 |
| 2.10.0-alpha | 99 | 5/7/2026 |
| 2.9.1 | 116 | 4/16/2026 |
| 2.9.1-alpha | 105 | 4/19/2026 |
| 2.9.0 | 133 | 2/3/2026 |
| 2.9.0-alpha | 110 | 2/3/2026 |
| 2.8.13 | 138 | 12/26/2025 |
| 2.8.13-alpha | 136 | 12/26/2025 |
| 2.8.12 | 451 | 12/11/2025 |
| 2.8.12-alpha | 240 | 12/11/2025 |
| 2.8.11 | 447 | 11/19/2025 |
| 2.8.11-alpha | 423 | 11/19/2025 |
| 2.8.10 | 183 | 11/8/2025 |
| 2.8.10-alpha | 176 | 11/8/2025 |
| 2.8.9 | 226 | 10/31/2025 |
| 2.8.9-alpha | 188 | 10/31/2025 |
| 2.8.8 | 142 | 10/18/2025 |
| 2.8.8-alpha | 221 | 10/19/2025 |
| 2.8.7-alpha | 221 | 10/4/2025 |