Skip to content

Commit 4109b7e

Browse files
committed
fixing isssues with documentation
1 parent 3f75e86 commit 4109b7e

1 file changed

Lines changed: 117 additions & 1 deletion

File tree

docs/Var/graph_helpers.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[⬅ Back to Table of Contents](../index.md)
2+
[⬅ Back to Var Table of Contents](var.md)
3+
[⬅ Back to Iterators, Mapping & Functional Helpers](iterators_mapping_functional.md)
4+
15
# Graph Helpers
26

37
This page documents all user-facing graph APIs for `var` when holding a graph, in a clear, tabular format with concise, non-redundant examples. Multi-step and real-world examples are at the end.
@@ -195,4 +199,116 @@ custom.reserve_edges_by_counts(counts);
195199
- All graph helpers are only available when `var` holds a graph.
196200
- Most methods return `var` or standard types; see API for details.
197201
- For advanced graph algorithms, see the full API or source.
198-
- `show(layout)` — If `layout` is true (default), the viewer will automatically arrange the graph layout. If false, the current node positions are preserved.
202+
- `show(layout)` — If `layout` is true (default), the viewer will automatically arrange the graph layout. If false, the current node positions are preserved.
203+
204+
## Example for you:
205+
```cpp
206+
// Test file for all graph_helpers.md documentation examples
207+
#include <pythonic/pythonic.hpp>
208+
#include <iostream>
209+
using namespace py;
210+
211+
int main()
212+
{
213+
// Basic Graph Creation & Manipulation
214+
{
215+
var g = graph(0);
216+
auto n0 = g.add_node("A");
217+
auto n1 = g.add_node("B");
218+
g.add_edge(n0, n1, 1.5);
219+
g.set_node_data(n0, "Alpha");
220+
std::cout << g.node_count() << std::endl;
221+
std::cout << g.edge_count() << std::endl;
222+
std::cout << g.has_edge(n0, n1) << std::endl;
223+
std::cout << g.get_edge_weight(n0, n1).value_or(-1) << std::endl;
224+
std::cout << g.get_node_data(n0) << std::endl;
225+
std::cout << g.dfs(n0) << std::endl;
226+
g.save_graph("/tmp/g.txt");
227+
g.to_dot("/tmp/g.dot");
228+
}
229+
// Graph Properties & Traversals
230+
{
231+
var g = graph(5);
232+
g.add_edge(0, 1);
233+
g.add_edge(1, 2);
234+
g.add_edge(2, 3);
235+
g.add_edge(3, 4);
236+
std::cout << g.is_connected() << std::endl;
237+
std::cout << g.has_cycle() << std::endl;
238+
g.add_edge(4, 0);
239+
std::cout << g.has_cycle() << std::endl;
240+
std::cout << g.out_degree(0) << std::endl;
241+
std::cout << g.in_degree(0) << std::endl;
242+
std::cout << g.neighbors(0) << std::endl;
243+
std::cout << g.get_edges(0) << std::endl;
244+
}
245+
// Shortest Paths & Algorithms
246+
{
247+
var g = graph(5);
248+
g.add_edge(0, 1, 4.0);
249+
g.add_edge(0, 2, 1.0);
250+
g.add_edge(1, 3, 1.0);
251+
g.add_edge(2, 1, 2.0);
252+
g.add_edge(2, 3, 5.0);
253+
g.add_edge(3, 4, 3.0);
254+
var result = g.get_shortest_path(0, 4);
255+
std::cout << result["path"] << std::endl;
256+
std::cout << result["distance"] << std::endl;
257+
var bf = g.bellman_ford(0);
258+
std::cout << bf["distances"] << std::endl;
259+
var fw = g.floyd_warshall();
260+
for (const auto &row : fw)
261+
std::cout << row << std::endl;
262+
}
263+
// Components, Topological Sort, MST
264+
{
265+
var dag = graph(4);
266+
dag.add_edge(0, 1, 1.0, 0.0, true);
267+
dag.add_edge(0, 2, 1.0, 0.0, true);
268+
dag.add_edge(1, 3, 1.0, 0.0, true);
269+
dag.add_edge(2, 3, 1.0, 0.0, true);
270+
std::cout << dag.topological_sort() << std::endl;
271+
var network = graph(6);
272+
network.add_edge(0, 1);
273+
network.add_edge(1, 2);
274+
network.add_edge(3, 4);
275+
network.add_edge(4, 5);
276+
std::cout << network.connected_components() << std::endl;
277+
var city_network = graph(4);
278+
city_network.add_edge(0, 1, 1.0);
279+
city_network.add_edge(0, 2, 4.0);
280+
city_network.add_edge(1, 2, 2.0);
281+
city_network.add_edge(1, 3, 5.0);
282+
city_network.add_edge(2, 3, 3.0);
283+
var mst = city_network.prim_mst();
284+
std::cout << mst["weight"] << std::endl;
285+
std::cout << mst["edges"] << std::endl;
286+
}
287+
// Serialization & Loading
288+
{
289+
var g = graph(3);
290+
g.add_edge(0, 1, 1.5);
291+
g.add_edge(1, 2, 2.5);
292+
g.set_node_data(0, "Start");
293+
g.save_graph("/tmp/my_graph.txt");
294+
var loaded = load_graph("/tmp/my_graph.txt");
295+
std::cout << loaded.str() << std::endl;
296+
g.to_dot("/tmp/my_graph.dot");
297+
}
298+
// Performance Optimization
299+
{
300+
var g = graph(1000);
301+
g.reserve_edges_per_node(10);
302+
for (size_t i = 0; i < 1000; i++)
303+
for (int j = 0; j < 10; j++)
304+
g.add_edge(i, (i + j + 1) % 1000, 1.0);
305+
var counts = list(5, 10, 3, 8, 2);
306+
var custom = graph(5);
307+
custom.reserve_edges_by_counts(counts);
308+
}
309+
return 0;
310+
}
311+
```
312+
## Next check
313+
314+
- [Math](../Math/math.md)

0 commit comments

Comments
 (0)