Yog.Layout.Multipartite (YogEx v0.99.0)

Copy Markdown View Source

Multipartite layout algorithm for positioning graph nodes in parallel layers.

Arranges nodes in straight parallel lines (either vertical columns or horizontal rows) based on their partition/layer membership. This layout is standard for bipartite graphs, feedforward neural network visualizations, flow networks, or any layered hierarchical relationships.

Mathematical Model

Given a list of layers $L = [L_0, L_1, \dots, L_{M-1}]$, where each $L_j$ is a list of node IDs, a bounding space of size $W \times H$, and center $(c_x, c_y)$:

If :align is :vertical (default):

  1. The $x$-coordinate for all nodes in layer $L_j$ is: $$ x_j = \left(c_x - \frac{W}{2}\right) + \frac{j \cdot W}{M - 1} $$ (or $c_x$ if $M = 1$).
  2. For the $i$-th node in layer $L_j$ (where $0 \le i < K$ and $K = |L_j|$): $$ y_i = \left(c_y - \frac{H}{2}\right) + \frac{i \cdot H}{K - 1} $$ (or $c_y$ if $K = 1$).

If :align is :horizontal:

  1. The $y$-coordinate for all nodes in layer $L_j$ is: $$ y_j = \left(c_y - \frac{H}{2}\right) + \frac{j \cdot H}{M - 1} $$ (or $c_y$ if $M = 1$).
  2. For the $i$-th node in layer $L_j$ (where $0 \le i < K$ and $K = |L_j|$): $$ x_i = \left(c_x - \frac{W}{2}\right) + \frac{i \cdot W}{K - 1} $$ (or $c_x$ if $K = 1$).

Complexities

  • Time Complexity: $O(V)$ where $V$ is the number of nodes.
  • Space Complexity: $O(V)$ auxiliary space.

Summary

Functions

Positions nodes in parallel layers (columns or rows).

Functions

layout(graph, layers, opts \\ [])

@spec layout(Yog.Graph.t(), [[Yog.Graph.node_id()]], keyword()) :: %{
  required(Yog.Graph.node_id()) => {float(), float()}
}

Positions nodes in parallel layers (columns or rows).

Requires a list of layers, where each layer is a list of node IDs.

Options

  • :align - Layer alignment direction: :vertical or :horizontal (default: :vertical).
  • :width - Bounding width (default: 1.0).
  • :height - Bounding height (default: 1.0).
  • :center - Center of layout space (default: {0.0, 0.0}).

Examples

iex> graph = Yog.undirected() |> Yog.add_nodes_from([1, 2, 3])
iex> pos = Yog.Layout.Multipartite.layout(graph, [[1], [2, 3]])
iex> Map.keys(pos) |> Enum.sort()
[1, 2, 3]