A modern, header-only C++11 library for graph construction and pathfinding algorithms. Designed for high performance with thread-safe concurrent searches and generic cost types.
- High Performance: O(m+n) space complexity, optimized priority queues, 35% faster search contexts
- Thread-Safe: Concurrent searches using external SearchContext, no race conditions
- Generic: Custom cost types, comparators, and state indexing - works with any data structure
- Complete Algorithm Suite: A*, Dijkstra, BFS, DFS with unified framework
- Robust: Comprehensive exception handling, structure validation, memory safety via RAII
- Well-Documented: Extensive API reference, tutorials, and working examples
struct GameState {
int x, y, health, ammo;
int64_t GetId() const { return y*1000 + x; } // Auto-detected by DefaultIndexer
};
Graph<GameState> game_world;
struct TravelCost {
double time, distance, comfort;
bool operator<(const TravelCost& other) const { /* lexicographic comparison */ }
};
Graph<Location, TravelCost> multi_criteria_graph;
graph.reserve(10000); // Pre-allocate vertices
context.PreAllocate(10000); // Pre-allocate search state
graph.AddVertices(state_list); // Batch operations
- CRTP Strategy Pattern: Zero-overhead polymorphism for search algorithms
- RAII Memory Management: Automatic cleanup with
std::unique_ptr
, no memory leaks - Template Metaprogramming: Compile-time optimization and type safety
- STL Compatibility: Full iterator support, range-based for loops
template<typename State, typename Transition = double, typename StateIndexer = DefaultIndexer<State>>
class Graph;
- State: Your vertex data (locations, game states, etc.)
- Transition: Edge weights (distance, time, cost, custom types)
- StateIndexer: Automatic ID generation from states
Optimal space complexity O(m+n) using adjacency lists:
Operation | Time Complexity | Space |
---|---|---|
Add/Find Vertex | O(1) average | O(1) |
Add Edge | O(1) | O(1) |
Search Algorithms | O((m+n) log n) | O(n) |
Thread-Safe Search | O((m+n) log n) | O(n) per context |
Concurrent read-only searches are fully supported:
// Each thread gets independent search context
void worker_thread(const Graph<Location>& map) {
SearchContext<Location> context; // Thread-local state
auto path = Dijkstra::Search(map, context, start, goal); // Thread-safe
}
Graph modifications require external synchronization (by design for performance).
- C++11 compatible compiler (GCC 4.8+, Clang 3.4+, MSVC 2015+)
- CMake 3.10+ (for build system and examples)
- Optional: Doxygen for API documentation
git clone https://github.com/rxdu/libgraph.git
cp -r libgraph/include/graph /your/project/include/
#include "graph/graph.hpp"
#include "graph/search/dijkstra.hpp" // Ready to use!
# Add to your CMakeLists.txt
add_subdirectory(third_party/libgraph)
target_link_libraries(your_target PRIVATE xmotion::graph)
mkdir build && cd build
cmake ..
sudo make install
# Use in your project
find_package(graph REQUIRED)
target_link_libraries(your_app PRIVATE xmotion::graph)
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release .. && make -j4 && cpack
sudo dpkg -i graph_3.0.1_amd64.deb # Adjust version as needed
git clone --recursive https://github.com/rxdu/libgraph.git
mkdir build && cd build
cmake -DBUILD_TESTING=ON ..
cmake --build .
# Run examples
./bin/simple_graph_demo
./bin/thread_safe_search_demo
# Run comprehensive tests (199 tests, 100% pass rate)
./bin/utests
#include "graph/graph.hpp"
#include "graph/search/dijkstra.hpp"
struct Location { int id; std::string name; };
// Create graph and add vertices
Graph<Location> map;
map.AddVertex({0, "Home"});
map.AddVertex({1, "Work"});
map.AddVertex({2, "Store"});
// Add weighted edges (distances)
map.AddEdge({0, "Home"}, {1, "Work"}, 5.0);
map.AddEdge({0, "Home"}, {2, "Store"}, 3.0);
map.AddEdge({2, "Store"}, {1, "Work"}, 4.0);
// Find optimal path
auto path = Dijkstra::Search(map, {0, "Home"}, {1, "Work"});
// Result: Home -> Store -> Work (7km total, shorter than direct 5km route)
Complete Getting Started Guide →
- Getting Started Guide - From zero to working graph in 20 minutes
- Complete API Reference - All classes, methods, and examples
- Tutorial Series - Progressive learning path
- Architecture Overview - System design and template patterns
- Advanced Features - Custom costs, validation, thread safety
- Search Algorithms Guide - Deep dive into A*, Dijkstra, BFS, DFS
- Performance Testing - Benchmarking and optimization
- Thread Safety Design - Concurrent search architecture
- Search Framework - Modern strategy pattern implementation
- Complete Documentation - Comprehensive guides and tutorials
- Report Issues - Bug reports and feature requests
- Discussions - Questions and community support
- Fork & Pull Request workflow for contributions
- Follow existing code style and patterns
- Add tests for new functionality
- Update documentation for public API changes
This library is distributed under MIT License.
@misc{libgraph2025,
title={libgraph: High-Performance C++ Graph Library},
author={Ruixiang Du and contributors},
year={2025},
url={https://github.com/rxdu/libgraph}
}
→ Complete Roadmap & TODO List
Built for the C++ community