lucassklp / Dijkstra.Algorithm

C# library implementation of Dijkstra's algorithm with graph builder.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dijkstra Algorithm

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph. wikipedia

This is a fork of agabani's dijkstra C# implementation and I have to thanks him. I did nothing but improve his good work and publish on Nuget.

Installation

If you are using Package Manager:

Install-Package Dijkstra.Algorithm -Version 1.0.5

If you are using .NET CLI

dotnet add package Dijkstra.Algorithm --version 1.0.5

Example Usage

// Create graph
var builder = new GraphBuilder();

builder
    .AddNode("A")
    .AddNode("B")
    .AddNode("C")
    .AddNode("D")
    .AddNode("E");

builder
    .AddLink("A", "B", 6)
    .AddLink("A", "D", 1);

builder
    .AddLink("B", "A", 6)
    .AddLink("B", "C", 5)
    .AddLink("B", "D", 2)
    .AddLink("B", "E", 2);

builder
    .AddLink("C", "B", 5)
    .AddLink("C", "E", 5);

builder
    .AddLink("D", "A", 1)
    .AddLink("D", "B", 2)
    .AddLink("D", "E", 1);

builder
    .AddLink("E", "B", 2)
    .AddLink("E", "C", 5)
    .AddLink("E", "D", 1);

var graph = builder.Build();

// Find path
const string origin = "A", destination = "C";

var path = graph.Dijkstra(origin, destination);

// Assert results
Assert.Equal(path.Origin.Id, origin);
Assert.Equal(path.Destination.Id, destination);
Assert.Equal(path.Segments.Count, 3);
Assert.Equal(path.Segments.Sum(s => s.Weight), 7);

Assert.Equal(path.Segments.ElementAt(0).Origin.Id, "A");
Assert.Equal(path.Segments.ElementAt(0).Weight, 1);
Assert.Equal(path.Segments.ElementAt(0).Destination.Id, "D");

Assert.Equal(path.Segments.ElementAt(1).Origin.Id, "D");
Assert.Equal(path.Segments.ElementAt(1).Weight, 1);
Assert.Equal(path.Segments.ElementAt(1).Destination.Id, "E");

Assert.Equal(path.Segments.ElementAt(2).Origin.Id, "E");
Assert.Equal(path.Segments.ElementAt(2).Weight, 5);
Assert.Equal(path.Segments.ElementAt(2).Destination.Id, "C");

About

C# library implementation of Dijkstra's algorithm with graph builder.

License:MIT License


Languages

Language:C# 98.5%Language:PowerShell 1.2%Language:Batchfile 0.3%