compute_layers

Function compute_layers 

Source
fn compute_layers(
    graph: &Graph<InvestmentSet, GraphEdge, Directed>,
    order: &[NodeIndex],
) -> Vec<InvestmentSet>
Expand description

Compute layers of investment sets from the topological order

This function works by computing the rank of each node in the graph based on the longest path from any root node to that node. Any nodes with the same rank are independent and can be solved in parallel. Nodes with different rank must be solved in order from highest rank (leaf nodes) to lowest rank (root nodes).

This function computes the ranks of each node, groups nodes by rank, and then produces a final ordered Vec of InvestmentSets which gives the order in which to solve the investment decisions.

Investment sets with the same rank (i.e., can be solved in parallel) are grouped into InvestmentSet::Layer. Investment sets that are alone in their rank remain as-is (i.e. either Single or Cycle). Layers can contain a mix of Single and Cycle investment sets.

For example, given the following graph:

    A
   / \
  B   C
 / \   \
D   E   F

Rank 0: A -> InvestmentSet::Single Rank 1: B, C -> InvestmentSet::Layer Rank 2: D, E, F -> InvestmentSet::Layer

These are returned as a Vec<InvestmentSet> from highest rank to lowest (i.e. the D, E, F layer first, then the B, C layer, then the singleton A).

Arguments:

  • graph - The investment graph. Any cycles in the graph MUST have already been compressed. This will be necessary anyway as computing a topological sort to obtain the order requires an acyclic graph.
  • order - The topological order of the graph nodes. Computed using petgraph::algo::toposort.

Returns: A Vec of InvestmentSets in the order they should be solved, with independent sets grouped into InvestmentSet::Layers.