Git Product home page Git Product logo

graphalo's Introduction

Build and test

Graphalo

A very simple graph data structure library.

Installation

Install-Package Graphalo

Example usage:

var graph = new DirectedGraph<string>();

// You can add vertices directly...
graph.AddVertex("A");

// Alternatively, adding an edge will automatically create missing vertices
graph.AddEdge(new Edge<string>("B", "C"));

// Get all vertices without edges coming out of them
graph.AllVertices.Where(v => v.HasOutEdges == false);

// Order all vertices by their degree (count of in and out edges), largest first
graph.AllVertices.OrderByDescending(v => v.Degree);

Searching

Depth first search

Reference: Wikipedia

Returns the deepest vertices first, working back to the root(s). Does not support cyclic graphs.

var graph = new DirectedGraph<string>();

// Search across the entire graph (in the case of multiple disconnected 
// graphs being contained in the same structure)
foreach (var vertex in graph.Search(SearchKind.DepthFirst))
{
	Console.WriteLine(vertex);
}

// Or search from a specific starting vertex - only the connected vertices will be returned
foreach (var vertex in graph.Search(SearchKind.DepthFirst, "A"))
{
	Console.WriteLine(vertex);
}

Traversal

Dijkstra's Algorithm

Reference: Wikipedia

Attempts to find the shortest path between two vertices, taking into account the weights of each edge. Cycles in the graph are supported and will not cause infinite loops.

//   B-
//  /5 \2
// A    C
//  \2 /1
//   D-
var graph = new DirectedGraph<string>();
graph.AddEdge(new Edge<string>("A", "B", 5));
graph.AddEdge(new Edge<string>("B", "C", 2));
graph.AddEdge(new Edge<string>("A", "D", 2));
graph.AddEdge(new Edge<string>("D", "C", 1));

var traversalResult = graph.Traverse(TraversalKind.Dijkstra, "A", "C");

// traversalResult.Success will be true
foreach (var vertex in traversalResult.Results)
{
	Console.Write(vertex);
	Console.Write(" ");
}

// Output: A D C (The route via D is the cheapest)

// Attempt a tranversal to an unreachable node:
traversalResult = graph.Traverse(TraversalKind.Dijkstra, "B", "D");
// traversalResult.Success will be false

graphalo's People

Contributors

mikegoatly avatar

Stargazers

 avatar

Watchers

 avatar  avatar

graphalo's Issues

Inability to work with the edges in Dijkstra result

I really like this library as it is very lightweight and provides me with the needed functionality. However, I've found a few missing features that I wish to report.

When the Dijkstra search result is retrieved, it is very important to provide both the nodes and edges required to reach the end goal. While it is helpful to output the nodes in order to reach the goal, one doesn't know which edges were taken. If you have more than one edge from one node to another, you need to know which one was taken, especially if the edge weight is dynamically calculated.

In my case, I had some additional code in each custom edge (which is an excellent feature) and wanted to execute them in order, which I wasn't able to do.

Possibility to ignore cyclic results in depth-first-search instead of failing hard

Another functionality I'd like to propose is to have a bool switch that would simply return instead of failing hard with CyclicGraphsNotSupportedException when the cycle is found. This would indirectly make the search behave on the graphs as if they were trees. This feature would still allow people to find all reachable and connected nodes from a single source node in a connected cyclical graph, which was the requirement I had.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.