Skip to main content

Graph

Struct Graph 

Source
pub struct Graph {
    pub offsets: Vec<usize>,
    pub targets: Vec<usize>,
    pub weights: Vec<f64>,
    pub n_nodes: usize,
}
Expand description

A directed, weighted graph in CSR form.

Fields§

§offsets: Vec<usize>

offsets[i] .. offsets[i + 1] is the slice of out-edges of node i.

§targets: Vec<usize>

Destination node of each out-edge, grouped by source node.

§weights: Vec<f64>

Weight (e.g. length or travel time) of each out-edge.

§n_nodes: usize

Number of nodes.

Implementations§

Source§

impl Graph

Source

pub fn from_edges( from: &[usize], to: &[usize], weight: &[f64], n_nodes: usize, ) -> Self

Build a CSR graph from parallel from/to/weight edge vectors.

n_nodes is the number of distinct node indices; every value in from/to must be in 0 .. n_nodes.

Examples found in repository?
examples/grid.rs (line 45)
9fn main() {
10    // Nodes 0..9 laid out as
11    //   0 - 1 - 2
12    //   |   |   |
13    //   3 - 4 - 5
14    //   |   |   |
15    //   6 - 7 - 8
16    // Every street is two-way, so it is two directed edges.
17    let mut from = Vec::new();
18    let mut to = Vec::new();
19    let mut length = Vec::new();
20    let mut penalty = Vec::new();
21    let mut street = |a: usize, b: usize, len: f64, pen: f64| {
22        for (u, v) in [(a, b), (b, a)] {
23            from.push(u);
24            to.push(v);
25            length.push(len);
26            penalty.push(pen);
27        }
28    };
29    for row in 0..3 {
30        for col in 0..3 {
31            let n = row * 3 + col;
32            if col < 2 {
33                street(n, n + 1, 100.0, 1.0);
34            }
35            if row < 2 {
36                street(n, n + 3, 100.0, 1.0);
37            }
38        }
39    }
40    // A shortcut across the middle: shorter, but it carries a heavy penalty
41    // (say, a steep hill or a toll).
42    street(0, 4, 120.0, 10.0);
43    street(4, 8, 120.0, 10.0);
44
45    let g = Graph::from_edges(&from, &to, &length, 9);
46
47    let (path, _slots, cost) = shortest_path_route(&g, 0, 8).unwrap();
48    println!("shortest by length: {path:?}  {cost} m");
49
50    let routes = k_shortest_paths(&g, &from, &to, &length, 0, 8, 3);
51    for (i, r) in routes.iter().enumerate() {
52        println!("alternative {}: {:?}  {} m", i + 1, r.path, r.cost);
53    }
54
55    println!("\nPareto front of (length, penalty):");
56    let front = pareto_paths(
57        &from,
58        &to,
59        &length,
60        &penalty,
61        9,
62        0,
63        8,
64        f64::INFINITY,
65        usize::MAX,
66    );
67    for r in &front {
68        println!("  {:?}  {} m  penalty {}", r.path, r.cost, r.resource);
69    }
70
71    let budget = 5.0;
72    let best = csp_lagrangian(&from, &to, &length, &penalty, 9, 0, 8, budget, 50).unwrap();
73    println!(
74        "\ncheapest path with penalty <= {budget}: {:?}  {} m  (lower bound {}, {} Dijkstra runs)",
75        best.path, best.cost, best.lower_bound, best.iterations
76    );
77}
Source

pub fn neighbors(&self, node: usize) -> impl Iterator<Item = (usize, f64)> + '_

Out-edges of node as (target, weight) pairs.

Source

pub fn n_edges(&self) -> usize

Number of directed edges.

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl RefUnwindSafe for Graph

§

impl Send for Graph

§

impl Sync for Graph

§

impl Unpin for Graph

§

impl UnsafeUnpin for Graph

§

impl UnwindSafe for Graph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.