Expand description
** Random Graph Generation Library **
§How it works
For a given space of elements, define a function that assigns every element a value greater equal zero. Such a function also gets the previously sampled elements as an argument which can influence the assigned values These values are then normalized resulting in a probability distribution for the element space. Based on this density elements will be sampled. After sampling an element the probabilities will be recomputed (can be turned off).
Sampling the edges is done in an identical fashion. First the space of edges is constructed, which is given by the combination of each previously sampled element.
§Example
Here is an example for generating a graph with 5 nodes that are represented by values from 0 to
10 and 10 vertices. The nodes are chosen uniformly and edges with shorter length are preferred.
The node weights are set to () and the edge weights are the absolute difference between the
two nodes values.
use gengraph::{sample_graph_from, Density};
let rng = rand::rng();
let dist = |x: &u8, y: &u8| if x > y {x - y} else {y - x};
let graph = sample_graph_from(
rand::rng(),
(0..10).collect::<Vec<u8>>(),
5,
10,
|_: &u8, _: &[u8]| 1.0,
|(x, y): &(u8, u8), _: &[(u8, u8)]| 1.0 / dist(x, y) as f64,
|_| (),
|(x, y): &(u8, u8)| dist(x, y),
);An option for constructing connected planar graph is also provided.
use gengraph::{sample_planar_connected_graph_from, Density};
let rng = rand::rng();
let dist = |x: &u8, y: &u8| if x > y {x - y} else {y - x};
let graph = sample_planar_connected_graph_from(
rand::rng(),
(0..10).collect::<Vec<u8>>(),
5,
10,
|_: &u8, _: &[u8]| 1.0,
|(x, y): &(u8, u8), _: &[(u8, u8)]| 1.0 / dist(x, y) as f64,
|_| (),
|(x, y): &(u8, u8)| dist(x, y),
|(x1, y1), (x2, y2)| {
x1 < x2 && x2 < y1 && y1 < y2
|| x2 < x1 && x1 < y2 && y2 < y1
|| y1 < y2 && y2 < x1 && x1 < x2
|| y2 < y1 && y1 < x2 && x2 < x1
},
);§Provided Graph Scenarios
This library provides methods to generate graphs representing certain scenarios. Provided scenarios:
- coast line
- mountain valley
§CLI
The cli tool provides an interface to generate multiple graphs representing one of the above mentioned scenarios. Use
$ cargo run -- -hfor more information about the cli tool.
Modules§
Structs§
- Density
Add - Density
Chain - Applies a function to the output of
Density::probability. SeeDensity::chain. - Density
Div - Density
Max - Density
Min - Density
Mul - Density
Multi Add - Density
Multi Div - Density
Multi Max - Density
Multi Min - Density
Multi Mul - Density
Multi Sub - Density
Sub - Density
Transformer - Transforms all input values to
Density::probabilityaccording to the given function. SeeDensity::transform.
Traits§
- Density
- Describes for each instance of
Sthe probability to take an instance ofSif it is considered.
Functions§
- filtered_
spanning_ tree_ on_ edge_ density - Implements Kruskal’s algorithm for finding a spanning tree with high probability and while
allows for rejecting edges based on previously taken edges.
This is archived by simple asserting befor taking an edge if the
filterreturnstrue. - graph_
from - Turns the
nodesandedgesinto aGraph. The node and edge weights are computed via the provided functionsnode_weightandedge_weight. - planar_
spanning_ tree_ on_ edge_ density - Implements Kruskal’s algorithm for finding a spanning tree with high probability and the addition the spanning tree should be planar. This is archived by simple asserting befor taking an edge if it crosses no other previously chosen edge. This algorithm has a runtime of O(E V) O(crossing) due to the check that a new edge does not cross an previously chosen edge (of which only < V can exist).
- sample_
connected_ graph_ from - Samples a graph from the given
Densitys and ensures that the graph is connected. - sample_
graph_ from - Samples a graph from the given
Densitys. - sample_
planar_ connected_ graph_ from - Samples a graph from the given
Densitys and ensures that the graph is planar according to the provided crossings function and connected. - spanning_
tree_ on_ edge_ density - Implements Kruskal’s algorithm for finding a spanning tree with high probability. This algorithm has a runtime of O(E V).