QuickGraph .NET standard. QuickGraph provides generic directed/undirected graph datastructures and algorithms for .Net. QuickGraph comes with algorithms such as depth first seach, breath first search, A* search, shortest path, k-shortest path, maximum flow, minimum spanning tree, least common ancestors, etc...
$ dotnet add package QuickGraph.NETStandardA comprehensive collection of examples demonstrating the capabilities of the QuickGraph.NETStandard library.
QuickGraph.NETStandard is a powerful graph data structure and algorithm library for .NET. These examples showcase various graph types, algorithms, visualization techniques, and real-world applications.
To use the visualization features, install Graphviz:
Windows:
Linux:
sudo apt-get install graphviz # Ubuntu/Debian
sudo yum install graphviz # Fedora/RHEL
macOS:
brew install graphviz
cd Examples
dotnet run
A menu-driven interface will guide you through all available examples.
Learn fundamental graph data structures:
AddVertex(), AddEdge(), OutEdges(), InDegree()AdjacentEdges(), AdjacentDegree(), connectivity checksOutEdges(), InEdges(), degree centralityMaster essential graph algorithms:
Create professional graph visualizations:
Apply graph theory to practical problems:
Examples/
??? Program.cs # Main menu application
??? BasicGraphs/
? ??? SimpleDirectedGraphExample.cs
? ??? UndirectedGraphExample.cs
? ??? BidirectionalGraphExample.cs
? ??? CustomEdgeTypesExample.cs
??? Algorithms/
? ??? ShortestPathExample.cs
? ??? BreadthFirstSearchExample.cs
? ??? DepthFirstSearchExample.cs
? ??? TopologicalSortExample.cs
? ??? StronglyConnectedComponentsExample.cs
? ??? MinimumSpanningTreeExample.cs
? ??? MaximumFlowExample.cs
? ??? CycleDetectionExample.cs
??? Visualization/
? ??? BasicVisualizationExample.cs
? ??? StyledVisualizationExample.cs
? ??? HierarchicalLayoutExample.cs
??? RealWorldScenarios/
??? SocialNetworkExample.cs
??? RoutePlanningExample.cs
??? TaskDependencyExample.cs
??? NetworkTopologyExample.cs
| Type | Description | When to Use |
|---|---|---|
AdjacencyGraph<TVertex, TEdge> | Directed graph | One-way relationships, dependencies |
UndirectedGraph<TVertex, TEdge> | Undirected graph | Symmetric relationships, networks |
BidirectionalGraph<TVertex, TEdge> | Directed with reverse lookup | Need both in/out edges efficiently |
// Create graph
var graph = new AdjacencyGraph<string, Edge<string>>();
// Add vertices
graph.AddVertex("A");
graph.AddVertexRange(new[] { "B", "C", "D" });
// Add edges
graph.AddEdge(new Edge<string>("A", "B"));
graph.AddVerticesAndEdge(new Edge<string>("B", "C")); // Adds vertices if needed
// Query
int vertexCount = graph.VertexCount;
int edgeCount = graph.EdgeCount;
bool hasEdge = graph.ContainsEdge("A", "B");
int outDegree = graph.OutDegree("A");
IEnumerable<Edge<string>> edges = graph.OutEdges("A");
// Algorithms
var shortestPath = graph.ShortestPathsDijkstra(costFunc, "start");
var topologicalOrder = graph.TopologicalSort();
var components = graph.StronglyConnectedComponents(out var componentMap);
public class WeightedEdge : Edge<string>
{
public double Weight { get; set; }
public WeightedEdge(string source, string target, double weight)
: base(source, target)
{
Weight = weight;
}
}
var graph = new AdjacencyGraph<string, WeightedEdge>();
graph.AddVerticesAndEdge(new WeightedEdge("A", "B", 5.0));
| Algorithm | Time Complexity | Space Complexity |
|---|---|---|
| BFS | O(V + E) | O(V) |
| DFS | O(V + E) | O(V) |
| Dijkstra | O((V + E) log V) | O(V) |
| Topological Sort | O(V + E) | O(V) |
| Kruskal's MST | O(E log V) | O(V) |
| Edmonds-Karp | O(V � E�) | O(V + E) |
| Strongly Connected Components | O(V + E) | O(V) |
All visualization examples create two files:
.dot file - Text format that can be edited or processed.png file - Visual representation (requires Graphviz)Without Graphviz: Copy the DOT content to https://dreampuf.github.io/GraphvizOnline/
var graph = new AdjacencyGraph<string, Edge<string>>();
var weights = new Dictionary<Edge<string>, double>();
// Add weighted edges
void AddWeightedEdge(string from, string to, double weight) {
var edge = new Edge<string>(from, to);
graph.AddVerticesAndEdge(edge);
weights[edge] = weight;
}
// Find shortest path
Func<Edge<string>, double> costFunc = e => weights[e];
var tryGetPath = graph.ShortestPathsDijkstra(costFunc, "start");
if (tryGetPath("end", out var path)) {
// Process path
}
var visited = new HashSet<string>();
var dfs = new DepthFirstSearchAlgorithm<string, Edge<string>>(graph);
dfs.DiscoverVertex += vertex => {
visited.Add(vertex);
Console.WriteLine($"Discovered: {vertex}");
};
dfs.FinishVertex += vertex => {
Console.WriteLine($"Finished: {vertex}");
};
dfs.Compute("start");
var viz = new GraphvizAlgorithm<string, Edge<string>>(graph);
viz.FormatVertex += (sender, args) => {
args.VertexFormatter.Label = args.Vertex;
args.VertexFormatter.Shape = GraphvizVertexShape.Box;
args.VertexFormatter.FillColor = GraphvizColor.LightBlue;
args.VertexFormatter.Style = GraphvizVertexStyle.Filled;
};
viz.Generate(new FileDotEngine(), "output_file");
Choose the Right Graph Type
AdjacencyGraph for directed relationshipsUndirectedGraph for symmetric relationshipsBidirectionalGraph when you need efficient reverse lookupsMemory Considerations
Performance
Visualization
Error Handling
Feel free to add more examples or improve existing ones! Common additions might include:
These examples are part of the QuickGraph.NETStandard project.