[][src]Module rs_graph::attributes

Extend a graph with attributes.

Sometimes one needs additional attributes associated with the nodes or edges of a graph. The simplest way is to store them in a NodeVec or EdgeVec. If these attributes are tightly related to the graph, it is convenient to store the graph together with the attributes in a single struct, e.g.

use rs_graph::LinkedListGraph;

struct MyGraph {
    graph: LinkedListGraph,
    balances: Vec<f64>,
    bounds: Vec<f64>,
}

The problem is that MyGraph itself does not implement the graph traits Graph, Digraph and so on, and can therefore not used directly for methods that need values of this type.

The struct AttrGraph in this module can be used to automate the tedious task of implementing the traits.

Example

use rs_graph_derive::Graph;
use rs_graph::{LinkedListGraph, Graph, IndexGraph, IndexNetwork};

#[derive(Clone, Default)]
struct NodeData {
    balance: isize,
}

#[derive(Clone, Default)]
struct EdgeData {
    bound: isize,
    cost: f64,
}

#[derive(Clone, Default)]
struct BiEdgeData {
    flow: isize,
}

#[derive(Graph)]
struct MyGraph {
  graph: LinkedListGraph,
  #[nodeattrs(NodeData)] nodedata: Vec<NodeData>,
  #[edgeattrs(EdgeData)] edgedata: Vec<EdgeData>,
  #[biedgeattrs(BiEdgeData)] biedgedata: Vec<BiEdgeData>,
}

impl MyGraph {
  fn new() -> Self {
    MyGraph {
      graph: LinkedListGraph::new(),
      nodedata: vec![],
      edgedata: vec![],
      biedgedata: vec![]
    }
  }
}

let g = MyGraph::new();

For a different approach, see the rs-graph-derive crate.

Traits

AttributedGraph

A trait to split the graph and its attributes.

EdgeAttributes

Object with associated edge attributes.

NodeAttributes

Object with associated node attributes.