Graffs 0.1.0

dotnet add package Graffs --version 0.1.0
                    
NuGet\Install-Package Graffs -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Graffs" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Graffs" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Graffs" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Graffs --version 0.1.0
                    
#r "nuget: Graffs, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Graffs@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Graffs&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Graffs&version=0.1.0
                    
Install as a Cake Tool

Graffs

A high-performance directed graph library for .NET 8.

Features

  • Graph Implementations: Adjacency list, adjacency matrix, and compact CSR format
  • Topological Sorting: Kahn's algorithm and DFS-based sorting with cycle detection
  • Cycle Detection: Johnson's algorithm for finding all elementary cycles
  • Connectivity Analysis: Strongly connected components (Tarjan's) and weakly connected components (Union-Find)
  • Path Finding: BFS shortest path and DFS all simple paths
  • Graph Slicing: Dependency chain analysis and parallel execution level partitioning
  • Fluent Builders: Easy graph construction with GraphBuilder<T> and DependencyGraphBuilder<T>

Installation

dotnet add package Graffs --version 0.1.0

Core Concepts

Nodes and Node Identity

Graffs graphs are generic over node type (TNode). Nodes can be any type that is non-null and properly implements equality (via Equals and GetHashCode). The graph uses node equality to identify and distinguish nodes.

Simple nodes - Use primitive types like string or int directly as node identifiers:

// Strings as node keys
var graph = new GraphBuilder<string>()
    .AddEdge("A", "B")
    .AddEdge("B", "C")
    .Build();

// Integers as node keys
var graph = new GraphBuilder<int>()
    .AddEdge(1, 2)
    .AddEdge(2, 3)
    .Build();

Rich nodes - Use custom types implementing IGraphNode<TKey> for nodes that carry additional data:

// IGraphNode<TKey> provides a standard way to expose node identity
public interface IGraphNode<out TKey> where TKey : notnull
{
    TKey Id { get; }              // Unique identifier for this node
    string? DisplayName { get; }  // Optional human-readable name
}

// Example: A task node with metadata
public class TaskNode : IGraphNode<string>
{
    public string Id { get; }
    public string? DisplayName { get; }
    public TimeSpan EstimatedDuration { get; }

    public TaskNode(string id, string? displayName = null, TimeSpan? duration = null)
    {
        Id = id;
        DisplayName = displayName;
        EstimatedDuration = duration ?? TimeSpan.Zero;
    }

    // Important: Override Equals/GetHashCode based on Id for graph operations
    public override bool Equals(object? obj) => obj is TaskNode other && Id == other.Id;
    public override int GetHashCode() => Id.GetHashCode();
}

// Use rich nodes in a graph
var compile = new TaskNode("compile", "Compile Source", TimeSpan.FromMinutes(2));
var test = new TaskNode("test", "Run Tests", TimeSpan.FromMinutes(5));
var deploy = new TaskNode("deploy", "Deploy App", TimeSpan.FromMinutes(1));

var graph = new DependencyGraphBuilder<TaskNode>()
    .DependsOn(test, compile)    // test depends on compile
    .DependsOn(deploy, test)     // deploy depends on test
    .Build();

Note: IGraphNode<TKey> is optional. It provides a standardized interface for node identification and display, which can be useful for debugging, logging, or when building tooling around graphs. Your nodes work with Graffs as long as they implement proper equality semantics.

Quick Start

using Graffs;
using Graffs.Builders;
using Graffs.Algorithms;

// Build a dependency graph using string keys
var graph = new DependencyGraphBuilder<string>()
    .DependsOn("C", "A")  // C depends on A (edge: A → C)
    .DependsOn("C", "B")  // C depends on B (edge: B → C)
    .DependsOn("D", "C")  // D depends on C (edge: C → D)
    .Build();

// Topological sort - returns nodes in dependency order
var result = TopologicalSort.KahnSort(graph);
// Result: [A, B, C, D] or [B, A, C, D] (dependencies come before dependents)

// Check for cycles
bool hasCycles = CycleDetection.HasCycles(graph);

// Find strongly connected components
var sccResult = ConnectivityAnalysis.FindStronglyConnectedComponents(graph);

Graph Implementations

Implementation Best For Edge Query Memory
AdjacencyListGraph<T> Sparse graphs O(1) avg O(V + E)
AdjacencyMatrixGraph<T> Dense graphs O(1) O(V²)
CompactGraph<T> Memory-constrained O(log E) O(V + E)

Algorithms

Topological Sort

// Kahn's algorithm (BFS-based)
var result = TopologicalSort.KahnSort(graph);

// DFS-based with cycle detection
var result = TopologicalSort.DfsSort(graph);

// Stable sort with custom ordering
var result = TopologicalSort.StableSort(graph, StringComparer.Ordinal);

Cycle Detection

// Quick check
bool hasCycles = CycleDetection.HasCycles(graph);

// Find all cycles
var result = CycleDetection.FindAllCycles(graph);
foreach (var cycle in result.Cycles)
{
    Console.WriteLine(cycle.ToPathString());
}

Connectivity Analysis

// Strongly connected components
var scc = ConnectivityAnalysis.FindStronglyConnectedComponents(graph);

// Weakly connected components
var wcc = ConnectivityAnalysis.FindWeaklyConnectedComponents(graph);

// Reachability
var reachable = ConnectivityAnalysis.FindReachableNodes(graph, startNode);

Path Finding

// Shortest path
var path = PathFinding.FindShortestPath(graph, source, target);

// All simple paths
var paths = PathFinding.FindAllSimplePaths(graph, source, target, maxPaths: 100);

Graph Slicing

// Analyze dependency structure for parallel execution
var slicing = GraphSlicer.AnalyzeDependencyStructure(graph);
foreach (var level in slicing.Levels)
{
    // Nodes at each level can be processed in parallel
    Console.WriteLine($"Level {level.LevelIndex}: {string.Join(", ", level.Nodes)}");
}

License

MIT License - see LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Graffs:

Package Downloads
TinyPreprocessor

A lightweight, extensible preprocessing library for resolving dependencies, merging resources, and generating source maps. Supports custom directive parsers, resource resolvers, and merge strategies.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 291 12/30/2025