SharpDag 0.1.4

dotnet add package SharpDag --version 0.1.4
                    
NuGet\Install-Package SharpDag -Version 0.1.4
                    
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="SharpDag" Version="0.1.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpDag" Version="0.1.4" />
                    
Directory.Packages.props
<PackageReference Include="SharpDag" />
                    
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 SharpDag --version 0.1.4
                    
#r "nuget: SharpDag, 0.1.4"
                    
#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 SharpDag@0.1.4
                    
#: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=SharpDag&version=0.1.4
                    
Install as a Cake Addin
#tool nuget:?package=SharpDag&version=0.1.4
                    
Install as a Cake Tool

SharpDag

SharpDag is a library that provides a simple, straightforward implementation of a directed acyclic graph (DAG). Two key features of SharpDag are:

  • Cycle Prevention - SharpDag will not allow you to add an edge between nodes that causes a cycle
  • Topological Sort - You can easily get an IEnumerable<T> of all nodes such that no node precedes any other node with an edge that points to it.

SharpDag is written in F#, but has a C#-friendly API wrapper as well.

C# API

The namespace SharpDag.CSharp contains types that work well in C#. Consider a graph that looks like this (assume arrows point downward):

    A
   / \
  /   \
 B     C
  \   /
   \ /
    D

You can represent this graph in SharpDag like this:

using SharpDag.CSharp;

var edges = new[] { 
    Edge.Untyped(src: "B", dest: "D"),
    Edge.Untyped(src: "C", dest: "D"),
    Edge.Untyped(src: "A", dest: "B"),
    Edge.Untyped(src: "A", dest: "C"),
};

var dag = Dag.FromUntypedEdges(edges);

var ordering = dag.TopologicalSort();
// returns "A", "B", "C", "D"

SharpDag does not allow cycles (that whole "acyclic" part of directed acyclic graph 😉). If you attempt to create a graph with a cycle, a DagCycleException will be thrown:

using SharpDag.CSharp;

// throws an exception since A -> B -> C -> A is a cycle
var dag = Dag.FromUntypedEdges(new[] {
    Edge.Untyped(src: "A", dest: "B"), 
    Edge.Untyped(src: "B", dest: "C"), 
    Edge.Untyped(src: "C", dest: "A"), 
});

F# API

The namespace SharpDag.FSharp contains types that work well in F#. Consider a graph that looks like this (assume arrows point downward):

    A
   / \
  /   \
 B     C
  \   /
   \ /
    D

F# code to represent this graph would look like this:

open SharpDag.FSharp

let dag =
    Dag.empty
    |> Dag.addEdge "B" "D" ()
    |> Dag.addEdge "C" "D" ()
    |> Dag.addEdge "A" "B" ()
    |> Dag.addEdge "A" "C" ()

let ordering = Dag.topologicalSort dag
// returns "A", "B", "C", "D"

"Typed" versus "Untyped"

All edges in SharpDag have a data value associated with them. Since the implementation language is F#, we can simply use the unit type to represent when we don't care about storing a data value with the edge (like the examples above).

Since C# doesn't have a similar concept to F#'s unit type, the C# API has various methods referring to either "typed" or "untyped" edges. The "untyped" APIs are a convenience to simplify creating graphs and edges that use F#'s unit type under the hood.

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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SharpDag:

Package Downloads
SqlOrder

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.4 121 12/19/2024
0.1.2 141 12/17/2024
0.1.0 245 11/28/2023