https://github.com/mikegoatly/graphalo
A very simple graph data structure library
https://github.com/mikegoatly/graphalo
depth-first-search dijkstra-algorithm graph graph-algorithms
Last synced: 8 months ago
JSON representation
A very simple graph data structure library
- Host: GitHub
- URL: https://github.com/mikegoatly/graphalo
- Owner: mikegoatly
- License: mit
- Created: 2020-10-28T21:27:02.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-11-15T09:14:33.000Z (about 1 year ago)
- Last Synced: 2025-04-28T14:14:50.357Z (8 months ago)
- Topics: depth-first-search, dijkstra-algorithm, graph, graph-algorithms
- Language: C#
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# Graphalo
A *very* simple graph data structure library.
## Installation
``` powershell
Install-Package Graphalo
```
## Example usage:
``` csharp
var graph = new DirectedGraph();
// You can add vertices directly...
graph.AddVertex("A");
// Alternatively, adding an edge will automatically create missing vertices
graph.AddEdge(new Edge("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](https://en.wikipedia.org/wiki/Depth-first_search)
Returns the deepest vertices first, working back to the root(s). Does not support cyclic graphs.
``` csharp
var graph = new DirectedGraph();
// 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](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
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.
``` csharp
// B-
// /5 \2
// A C
// \2 /1
// D-
var graph = new DirectedGraph();
graph.AddEdge(new Edge("A", "B", 5));
graph.AddEdge(new Edge("B", "C", 2));
graph.AddEdge(new Edge("A", "D", 2));
graph.AddEdge(new Edge("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
```