Skip to content

Commit 7769bbf

Browse files
committed
gl quick start
1 parent 15d40d0 commit 7769bbf

4 files changed

Lines changed: 106 additions & 15 deletions

File tree

docs/gl/quick_start.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# GL: Quick Start
2+
3+
This guide provides a rapid introduction to the **GL** module. In just a few minutes, you will define a graph, populate it with vertices and weighted edges, and run Dijkstra's shortest path algorithm.
4+
5+
## Basic Usage Example
6+
7+
The following C++ program demonstrates the standard workflow of the CPP-GL library:
8+
9+
1. **Defining Traits:** Selecting the memory layout, directionality, and payloads.
10+
2. **Building Topology:** Adding vertices and edges.
11+
3. **Execution:** Running a decoupled algorithm.
12+
4. **Reconstruction:** Extracting and formatting the results.
13+
14+
```cpp
15+
#include <gl/graph.hpp> // (1)!
16+
#include <gl/algorithm.hpp>
17+
#include <gl/io.hpp>
18+
19+
#include <iostream>
20+
21+
int main() {
22+
using traits_t = gl::list_graph_traits< // (2)!
23+
gl::directed_t,
24+
gl::empty_properties,
25+
gl::weight_property<int>>;
26+
27+
gl::graph<traits_t> graph(5); // (3)!
28+
29+
graph.add_edge(0, 1)->weight = 10; // (4)!
30+
graph.add_edge(0, 2)->weight = 1;
31+
graph.add_edge(2, 3)->weight = 2;
32+
graph.add_edge(3, 1)->weight = 4;
33+
graph.add_edge(1, 4)->weight = 2;
34+
35+
const auto source_id = 0u;
36+
auto paths = gl::algorithm::dijkstra_shortest_paths(graph, source_id); // (5)!
37+
38+
const auto target_id = 4u;
39+
if (not gl::algorithm::is_reachable(paths.predecessors, target_id)) { // (6)!
40+
std::cerr << "Vertex " << target_id << " is unreachable.\n";
41+
return 1;
42+
}
43+
44+
auto path_to_target = gl::algorithm::reconstruct_path(paths.predecessors, target_id); // (7)!
45+
std::cout << "Shortest path distance to vertex " << target_id << ": "
46+
<< paths.distances[target_id] << "\nPath: "
47+
<< gl::io::range_formatter(path_to_target, " -> ", "", "") << '\n'; // (8)!
48+
49+
return 0;
50+
}
51+
```
52+
53+
1. Include the necessary core graph, algorithm, and I/O headers.
54+
2. Define the graph's memory layout (adjacency list), directionality, and element payloads via a single traits structure. <br/> **NOTE:** Alternatively, you can use one of the provided [generic graph type aliases](../cpp-gl/group__GL-Core.md#public-types).
55+
3. Instantiate the generic `graph` class, pre-allocating it with 5 vertices (IDs 0 through 4).
56+
4. The `add_edge` method returns an `edge_descriptor`. Use the overloaded `->` operator to directly access and assign the attached `weight` payload in a type-safe manner.
57+
5. Execute the external algorithm on the instantiated graph.
58+
6. Use built-in utilities to verify if the target vertex was successfully reached during the pathfinding execution.
59+
7. Walk backward through the resulting predecessor map to reconstruct the exact sequence of vertices forming the shortest path.
60+
8. Stream the reconstructed path seamlessly using the library's built-in formatting utilities, customizing the delimiter to `" -> "`.
61+
62+
**Output:**
63+
```text
64+
Shortest path distance to vertex 4: 9
65+
Path: 0 -> 2 -> 3 -> 1 -> 4
66+
```
67+
68+
### Understanding the Code
69+
70+
- **Traits (`gl::list_graph_traits`):** CPP-GL relies heavily on template abstraction. Instead of passing multiple arguments to the `gl::graph` constructor, you pass a single *Traits* struct as its template parameter. This strictly dictates whether the graph uses an adjacency list or matrix, if it is directed, what custom properties (like `gl::weight_property`) exist on its elements and what id type is used for its elements.
71+
- **Property Access (`->weight`):** Adding an edge returns a descriptor handle. You access the specific payload fields associated with that edge by using the overloaded `->` operator, ensuring highly performant and type-safe data manipulation.
72+
- **Algorithm Separation:** Search engines and algorithms (like `dijkstra_shortest_paths`) exist entirely outside the graph class. They accept the graph as a `const` reference, mathematically guaranteeing that algorithms will never accidentally mutate your topology.
73+
- **Formatting (`gl::io::range_formatter`):** The library includes lightweight utility formatters so you can easily stream sequences, paths, and standard ranges directly to the console with complete control over delimiters and brackets.

docs/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ CPP-GL is a highly customizable, intuitive, and concept-driven graph and hypergr
2525

2626
Designed strictly around modern C++ paradigms, the library heavily leverages templates and concepts to deliver an API that is exceptionally fast, generic, and type-safe. It relies solely on the C++ standard library, meaning it requires no external dependencies and integrates perfectly with modern C++ tools such as range-based loops, the `<ranges>` library, standard algorithms, stream operations and more.
2727

28+
---
29+
2830
## Module Architecture: GL vs. HGL
2931

3032
To accommodate different mathematical models without compromising API clarity or performance, the library is strictly partitioned into two primary modules:
@@ -41,6 +43,8 @@ To accommodate different mathematical models without compromising API clarity or
4143
- **Extensible Engines & Concrete Algorithms:** CPP-GL features a dual-layered algorithmic architecture. At its core, it provides highly generic search templates (such as BFS, DFS and Priority-First Search) that act as foundational engines, allowing users to build entirely new algorithms by injecting custom callbacks. Built upon these engines is a robust suite of concrete, ready-to-use algorithms for immediate application.
4244
- **Robust I/O Facilities:** Built-in serialization and formatting tools allow for seamless translation of in-memory graphs to and from standard streams and files, utilizing a shared global state for consistent formatting.
4345

46+
---
47+
4448
## Installation & Integration
4549

4650
CPP-GL is a header-only template library. You can integrate it into your project either by directly including the headers or via CMake.
@@ -87,3 +91,12 @@ target_link_libraries(my_project PRIVATE cpp-gl::cpp-gl)
8791
### Option C: Including the Headers Directly
8892

8993
If you do not wish to use CMake, you can simply download the desired version of the library from the [Releases Page](https://github.com/SpectraL519/cpp-gl/releases) and add the `include` directory of the library to your project via the `-I<cpp-gl-dir>/include` flag.
94+
95+
---
96+
97+
## Next Steps
98+
99+
Ready to write some code? Choose a module below to view its Quick Start guide and dive into the tutorials:
100+
101+
* [**Get Started with GL (Standard Graphs)**](gl/quick_start.md) - Master the core concepts, build custom topologies, and explore the robust suite of generic traversal engines and classical algorithms.
102+
* **Get Started with HGL (Hypergraphs)** - *(Coming Soon)*

include/gl/graph.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ template <traits::c_graph Graph>
14091409
return Graph(source);
14101410
}
14111411

1412-
/// @ingroup GL GL-Types
1412+
/// @ingroup GL GL-Core
14131413
/// @brief Convenience alias for defining a standard directed graph.
14141414
template <
14151415
traits::c_properties VertexProperties = empty_properties,
@@ -1419,7 +1419,7 @@ template <
14191419
using directed_graph =
14201420
graph<directed_graph_traits<VertexProperties, EdgeProperties, ImplTag, IdType>>;
14211421

1422-
/// @ingroup GL GL-Types
1422+
/// @ingroup GL GL-Core
14231423
/// @brief Convenience alias for defining a standard undirected graph.
14241424
template <
14251425
traits::c_properties VertexProperties = empty_properties,
@@ -1429,7 +1429,7 @@ template <
14291429
using undirected_graph =
14301430
graph<undirected_graph_traits<VertexProperties, EdgeProperties, ImplTag, IdType>>;
14311431

1432-
/// @ingroup GL GL-Types
1432+
/// @ingroup GL GL-Core
14331433
/// @brief Convenience alias for defining a graph utilizing an adjacency list.
14341434
template <
14351435
traits::c_graph_directional_tag DirectionalTag = directed_t,
@@ -1439,7 +1439,7 @@ template <
14391439
using list_graph =
14401440
graph<list_graph_traits<DirectionalTag, VertexProperties, EdgeProperties, IdType>>;
14411441

1442-
/// @ingroup GL GL-Types
1442+
/// @ingroup GL GL-Core
14431443
/// @brief Convenience alias for defining a graph utilizing an adjacency matrix.
14441444
template <
14451445
traits::c_graph_directional_tag DirectionalTag = directed_t,
@@ -1449,7 +1449,7 @@ template <
14491449
using matrix_graph =
14501450
graph<matrix_graph_traits<DirectionalTag, VertexProperties, EdgeProperties, IdType>>;
14511451

1452-
/// @ingroup GL GL-Types
1452+
/// @ingroup GL GL-Core
14531453
/// @brief Convenience alias for defining a graph utilizing a flattened adjacency list.
14541454
template <
14551455
traits::c_graph_directional_tag DirectionalTag = directed_t,
@@ -1459,7 +1459,7 @@ template <
14591459
using flat_list_graph =
14601460
graph<flat_list_graph_traits<DirectionalTag, VertexProperties, EdgeProperties, IdType>>;
14611461

1462-
/// @ingroup GL GL-Types
1462+
/// @ingroup GL GL-Core
14631463
/// @brief Convenience alias for defining a graph utilizing a flattened adjacency matrix.
14641464
template <
14651465
traits::c_graph_directional_tag DirectionalTag = directed_t,

mkdocs.yml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ repo_url: "https://github.com/SpectraL519/cpp-gl"
55
nav:
66
- Home: index.md
77
- GL:
8-
# TODO: Tutorial page
9-
- Overview: cpp-gl/group__GL.md
10-
- Core Graph Components: cpp-gl/group__GL-Core.md
11-
- Graph Algorithms: cpp-gl/group__GL-Algorithm.md
12-
- Graph Topologies: cpp-gl/group__GL-Topology.md
13-
- I/O Utility: cpp-gl/group__GL-IO.md
14-
- Traits & Concepts: cpp-gl/group__GL-Traits.md
15-
- Generic Types: cpp-gl/group__GL-Types.md
16-
- General Utilities: cpp-gl/group__GL-Util.md
8+
- Quick Start: gl/quick_start.md
9+
# - Architecture & Basics: gl/architecture.md
10+
# - Algorithms: gl/algorithms.md
11+
# - Input & Output: gl/io.md
12+
# - Advanced Configuration: gl/configuration.md
13+
- API Reference:
14+
- Overview: cpp-gl/group__GL.md
15+
- Core Components: cpp-gl/group__GL-Core.md
16+
- Algorithms: cpp-gl/group__GL-Algorithm.md
17+
- Topologies: cpp-gl/group__GL-Topology.md
18+
- I/O Utility: cpp-gl/group__GL-IO.md
19+
- Traits & Concepts: cpp-gl/group__GL-Traits.md
20+
- Generic Types: cpp-gl/group__GL-Types.md
21+
- General Utilities: cpp-gl/group__GL-Util.md
1722
# - HGL:
1823
- API Index:
1924
- Modules: cpp-gl/modules.md

0 commit comments

Comments
 (0)