Table of Contents

Class TopOrderAndLevels

Namespace
Graphity
Assembly
Graphity.Core.dll

A topological order of a DAG (directed acyclic graph) with the level of each node. A root node has level 0, and every other node has one more than the highest level of its in-nodes.

public sealed class TopOrderAndLevels
Inheritance
TopOrderAndLevels
Inherited Members

Remarks

The level of a node is the number of edges on a longest path to it from a root node, so no path connects two nodes of the same level. Order lists the nodes level by level, LevelOf(int) and Levels return their levels, and LevelCount the number of levels.

Uses Kahn's algorithm: Of(Graph) starts with the root nodes in ascending order, places a node once all its in-nodes are placed and takes the out-nodes of each node in ascending order, so a graph always gets the same order. For the order alone, Of(Graph) needs less memory.

Instances are immutable and safe to share between threads.

Memory: 8 x NodeCount bytes.

Properties

LevelCount

Gets the number of levels, or 0 for a graph without nodes. It equals the number of nodes on a longest path.

public int LevelCount { get; }

Property Value

int

Remarks

Time: O(1).

Levels

Gets the level of every node, by node ID.

public ImmutableArray<int> Levels { get; }

Property Value

ImmutableArray<int>

Remarks

Time: O(1).

Order

Gets the nodes in topological order, level by level: for every edge u -> v, u comes before v.

public ImmutableArray<int> Order { get; }

Property Value

ImmutableArray<int>

Remarks

Time: O(1).

Methods

LevelOf(int)

Returns the level of node. Equivalent to Levels[node].

public int LevelOf(int node)

Parameters

node int

Node ID in 0..NodeCount-1.

Returns

int

The level.

Remarks

Time: O(1).

Exceptions

ArgumentOutOfRangeException

node is out of range.

Of(Graph)

Returns the topological order and the levels of graph.

public static TopOrderAndLevels Of(Graph graph)

Parameters

graph Graph

The DAG to sort.

Returns

TopOrderAndLevels

The order and the levels.

Remarks

On a BiAdjacentGraph, it reads the in-degrees instead of counting them in a pass over the edges.

Time: O(NodeCount + EdgeCount).
Memory: 8 x NodeCount bytes for the result; while it runs, 8 x NodeCount bytes more.

Exceptions

ArgumentException

graph has a cycle.