Learn R Programming

caugi (version 1.0.0)

topological_sort: Get a topological ordering of a DAG

Description

Returns a topological ordering of the nodes in a DAG. For every directed edge u -> v in the graph, u will appear before v in the returned ordering.

Usage

topological_sort(cg)

Value

A character vector of node names in topological order.

Arguments

cg

A caugi object of class DAG.

See Also

Other queries: ancestors(), anteriors(), children(), descendants(), districts(), edge_types(), edges(), exogenous(), is_acyclic(), is_admg(), is_ag(), is_caugi(), is_cpdag(), is_dag(), is_empty_caugi(), is_mag(), is_pdag(), is_ug(), m_separated(), markov_blanket(), neighbors(), nodes(), parents(), same_nodes(), spouses(), subgraph()

Examples

Run this code
# Simple DAG: A -> B -> C
cg <- caugi(
  A %-->% B,
  B %-->% C,
  class = "DAG"
)
topological_sort(cg) # Returns c("A", "B", "C") or equivalent valid ordering

# DAG with multiple valid orderings
cg2 <- caugi(
  A %-->% C,
  B %-->% C,
  class = "DAG"
)
# Could return c("A", "B", "C") or c("B", "A", "C")
topological_sort(cg2)

Run the code above in your browser using DataLab