Class RandomGraph

java.lang.Object
edu.cmu.tetrad.graph.RandomGraph

public class RandomGraph extends Object
The RandomGraph class provides methods for generating random graphs. It includes methods for generating random directed acyclic graphs (DAGs), random graphs with arbitrary edges, and random scale-free graphs.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Generates random DAGs uniformly with certain classes of DAGs using variants of Markov chain algorithm by Malancon, Dutour, and Philippe.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addTwoCycles(Graph graph, int numTwoCycles)
    addTwoCycles.
    static void
    fixLatents1(int numLatentConfounders, Graph graph)
    fixLatents1.
    static void
    fixLatents4(int numLatentConfounders, Graph graph)
    fixLatents4.
    static Graph
    randomCyclicGraph2(int numNodes, int numEdges, int maxDegree)
    Makes a cyclic graph by repeatedly adding cycles of length of 3, 4, or 5 to the graph, then finally adding two cycles.
    static Graph
    randomCyclicGraph3(int numNodes, int numEdges, int maxDegree, double probCycle, double probTwoCycle)
    Makes a cyclic graph by repeatedly adding cycles of length of 3, 4, or 5 to the graph, then finally adding two cycles.
    static Graph
    randomDag(int numNodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
    Generates a random Directed Acyclic Graph (DAG) with the specified parameters.
    static Graph
    randomDag(int numNodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
    Generates a random Directed Acyclic Graph (DAG) based on specified constraints.
    static Dag
    randomDag(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
    Generates a random Directed Acyclic Graph (DAG).
    static Dag
    randomDag(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
    Generates a random Directed Acyclic Graph (DAG) based on the given parameters.
    static Graph
    randomGraph(int numMeasures, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
    Generates a random graph based on the given parameters.
    static Graph
    randomGraph(int numMeasures, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
    Generates a random graph based on the specified parameters.
    static Graph
    randomGraph(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
    Generates a random graph based on the given parameters.
    static Graph
    randomGraph(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
    Generates a random graph with the specified parameters and properties.
    static Graph
    randomGraphRandomForwardEdges(int numNodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
    Generates a random graph with the given parameters and random forward edges.
    static Graph
    randomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
    Generates a random graph with forward edges.
    static Graph
    randomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, boolean layoutAsCircle, long seed)
    Generates a random graph with forward edges.
    static Graph
    randomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
    Generates a random directed acyclic graph with specified properties, including constraints on maximum degree, indegree, and outdegree, with random forward edges.
    static Graph
    randomGraphUniform(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, int numIterations)
    Generates a random graph using UniformGraphGenerator with the specified parameters.
    static Graph
    randomScaleFreeGraph(int numNodes, int numLatentConfounders, double alpha, double beta, double delta_in, double delta_out)
    Generates a random scale-free graph.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • randomDag

      public static Graph randomDag(int numNodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
      Generates a random Directed Acyclic Graph (DAG) with the specified parameters.
      Parameters:
      numNodes - The number of nodes in the DAG.
      numLatentConfounders - The number of latent confounders in the DAG.
      maxNumEdges - The maximum number of edges in the DAG.
      maxDegree - The maximum degree of each node in the DAG.
      maxIndegree - The maximum indegree of each node in the DAG.
      maxOutdegree - The maximum outdegree of each node in the DAG.
      connected - Specifies whether the DAG should be connected.
      Returns:
      The randomly generated DAG.
    • randomDag

      public static Graph randomDag(int numNodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
      Generates a random Directed Acyclic Graph (DAG) based on specified constraints.
      Parameters:
      numNodes - the number of nodes in the graph.
      numLatentConfounders - the number of latent confounders to include in the graph.
      maxNumEdges - the maximum number of edges that can be added to the graph.
      maxDegree - the maximum degree (number of incoming and outgoing edges) of any node in the graph.
      maxIndegree - the maximum number of incoming edges to any node.
      maxOutdegree - the maximum number of outgoing edges from any node.
      connected - whether the graph should be connected (i.e., a single connected component).
      seed - the random seed for reproducibility in graph generation, -1 for random.
      Returns:
      a randomly generated DAG that satisfies the given constraints.
    • randomDag

      public static Dag randomDag(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
      Generates a random Directed Acyclic Graph (DAG).
      Parameters:
      nodes - the list of nodes in the graph
      numLatentConfounders - the number of latent confounders (unobserved variables) in the graph
      maxNumEdges - the maximum number of edges in the graph
      maxDegree - the maximum number of edges incident to a single node in the graph
      maxIndegree - the maximum number of incoming edges for a single node in the graph
      maxOutdegree - the maximum number of outgoing edges for a single node in the graph
      connected - whether the graph should be connected or not
      Returns:
      a randomly generated DAG representing the causal relationships between the nodes
    • randomDag

      public static Dag randomDag(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
      Generates a random Directed Acyclic Graph (DAG) based on the given parameters.
      Parameters:
      nodes - the list of nodes that will be included in the DAG
      numLatentConfounders - the number of latent confounders to include in the DAG
      maxNumEdges - the maximum number of edges the DAG can have
      maxDegree - the maximum degree (sum of indegree and outdegree) of any node
      maxIndegree - the maximum number of incoming edges for any node
      maxOutdegree - the maximum number of outgoing edges for any node
      connected - whether the generated graph should ensure connectivity
      seed - the seed value for random number generation, -1 for random.
      Returns:
      a randomly generated DAG based on the specified parameters
    • randomGraph

      public static Graph randomGraph(int numMeasures, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
      Generates a random graph based on the given parameters.
      Parameters:
      numMeasures - the number of nodes in the graph
      numLatentConfounders - the number of latent confounders in the graph
      numEdges - the number of edges in the graph
      maxDegree - the maximum degree of each node in the graph
      maxIndegree - the maximum indegree of each node in the graph
      maxOutdegree - the maximum outdegree of each node in the graph
      connected - indicates whether the graph should be connected or not
      Returns:
      a randomly generated graph
    • randomGraph

      public static Graph randomGraph(int numMeasures, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
      Generates a random graph based on the specified parameters.
      Parameters:
      numMeasures - the number of measured variables (nodes) to include in the graph
      numLatentConfounders - the number of latent confounding variables (nodes) to include in the graph
      numEdges - the total number of edges in the graph
      maxDegree - the maximum degree (number of edges connected) for any node in the graph
      maxIndegree - the maximum number of incoming edges for any node in the graph
      maxOutdegree - the maximum number of outgoing edges for any node in the graph
      connected - whether the graph should be generated as a single connected component
      seed - the random seed to ensure reproducibility of the graph generation, -1 for random.
      Returns:
      a randomly generated graph that satisfies the input constraints
    • randomGraph

      public static Graph randomGraph(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
      Generates a random graph based on the given parameters.
      Parameters:
      nodes - the list of nodes to create the graph with
      numLatentConfounders - the number of latent confounders in the graph
      maxNumEdges - the maximum number of edges in the graph
      maxDegree - the maximum total degree (in-degree + out-degree) of each node
      maxIndegree - the maximum in-degree of each node
      maxOutdegree - the maximum out-degree of each node
      connected - boolean flag indicating whether the generated graph should be connected or not
      Returns:
      the randomly generated graph
    • randomGraph

      public static Graph randomGraph(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
      Generates a random graph with the specified parameters and properties.
      Parameters:
      nodes - The list of nodes to include in the graph.
      numLatentConfounders - The number of latent confounders in the graph.
      maxNumEdges - The maximum number of allowable edges in the graph.
      maxDegree - The maximum degree for any node in the graph.
      maxIndegree - The maximum in-degree for any node in the graph.
      maxOutdegree - The maximum out-degree for any node in the graph.
      connected - Whether the graph should be guaranteed to be connected.
      seed - The seed for random number generation to ensure reproducibility, -1 for random.
      Returns:
      A randomly generated graph adhering to the specified constraints and properties.
    • randomGraphUniform

      public static Graph randomGraphUniform(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, int numIterations)
      Generates a random graph using UniformGraphGenerator with the specified parameters.
      Parameters:
      nodes - The list of nodes to be included in the graph
      numLatentConfounders - The maximum number of latent confounders to be added to the graph
      maxNumEdges - The maximum number of edges to be added to the graph
      maxDegree - The maximum degree of each node in the graph
      maxIndegree - The maximum indegree of each node in the graph
      maxOutdegree - The maximum outdegree of each node in the graph
      connected - A flag indicating whether the graph should be a connected directed acyclic graph (DAG)
      numIterations - The number of iterations to generate the graph
      Returns:
      The generated random graph
      Throws:
      IllegalArgumentException - if the given number of nodes is 0, or the maximum number of edges is out of range, or the maximum number of latent confounders is out of range
    • randomGraphRandomForwardEdges

      public static Graph randomGraphRandomForwardEdges(int numNodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
      Generates a random graph with the given parameters and random forward edges.
      Parameters:
      numNodes - the number of nodes in the graph
      numLatentConfounders - the number of latent confounders in the graph
      numEdges - the number of edges in the graph
      maxDegree - the maximum degree of a node in the graph
      maxIndegree - the maximum indegree of a node in the graph
      maxOutdegree - the maximum outdegree of a node in the graph
      connected - indicates whether the graph should be connected
      seed - Seed for reproducibility, -1 for random.
      Returns:
      a random graph object
    • randomGraphRandomForwardEdges

      public static Graph randomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected)
      Generates a random graph with forward edges.
      Parameters:
      nodes - the list of nodes in the graph
      numLatentConfounders - the number of latent confounders in the graph
      numEdges - the total number of edges in the graph
      maxDegree - the maximum number of edges connected to each node
      maxIndegree - the maximum in-degree of each node
      maxOutdegree - the maximum out-degree of each node
      connected - if true, ensures that the generated graph is connected
      Returns:
      the random graph with forward edges
    • randomGraphRandomForwardEdges

      public static Graph randomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, long seed)
      Generates a random directed acyclic graph with specified properties, including constraints on maximum degree, indegree, and outdegree, with random forward edges.
      Parameters:
      nodes - the list of nodes to include in the graph
      numLatentConfounders - the number of latent confounders to add to the graph
      numEdges - the total number of edges in the graph
      maxDegree - the maximum degree (indegree plus outdegree) allowed for any node
      maxIndegree - the maximum number of incoming edges allowed for any node
      maxOutdegree - the maximum number of outgoing edges allowed for any node
      connected - whether the graph should be connected
      seed - the random seed for the graph generation process
      Returns:
      a random directed acyclic graph constructed with the specified properties
    • randomGraphRandomForwardEdges

      public static Graph randomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected, boolean layoutAsCircle, long seed)
      Generates a random graph with forward edges.
      Parameters:
      nodes - a list of nodes to create the graph with
      numLatentConfounders - the maximum number of latent confounders to include
      numEdges - the desired number of edges in the graph
      maxDegree - the maximum degree of a node in the graph
      maxIndegree - the maximum indegree of a node in the graph
      maxOutdegree - the maximum outdegree of a node in the graph
      connected - indicates if the graph should be connected
      layoutAsCircle - indicates if the graph should be laid out in a circular pattern
      seed - The seed, -1 for random.
      Returns:
      a randomly generated graph with forward edges
      Throws:
      IllegalArgumentException - if the number of nodes is less than or equal to 0, the number of edges is negative or exceeds the possible maximum, or the number of latent confounders is negative or exceeds the number of nodes
    • randomScaleFreeGraph

      public static Graph randomScaleFreeGraph(int numNodes, int numLatentConfounders, double alpha, double beta, double delta_in, double delta_out)
      Generates a random scale-free graph.
      Parameters:
      numNodes - The number of nodes in the graph.
      numLatentConfounders - The number of latent confounders in the graph.
      alpha - The parameter alpha for generating node degrees in the graph.
      beta - The parameter beta for generating node degrees in the graph.
      delta_in - The parameter delta_in for generating node degrees in the graph.
      delta_out - The parameter delta_out for generating node degrees in the graph.
      Returns:
      A randomly generated scale-free graph.
    • fixLatents1

      public static void fixLatents1(int numLatentConfounders, Graph graph)

      fixLatents1.

      Parameters:
      numLatentConfounders - a int
      graph - a Graph object
    • fixLatents4

      public static void fixLatents4(int numLatentConfounders, Graph graph)

      fixLatents4.

      Parameters:
      numLatentConfounders - a int
      graph - a Graph object
    • randomCyclicGraph2

      public static Graph randomCyclicGraph2(int numNodes, int numEdges, int maxDegree)
      Makes a cyclic graph by repeatedly adding cycles of length of 3, 4, or 5 to the graph, then finally adding two cycles.
      Parameters:
      numNodes - a int
      numEdges - a int
      maxDegree - a int
      Returns:
      a Graph object
    • randomCyclicGraph3

      public static Graph randomCyclicGraph3(int numNodes, int numEdges, int maxDegree, double probCycle, double probTwoCycle)
      Makes a cyclic graph by repeatedly adding cycles of length of 3, 4, or 5 to the graph, then finally adding two cycles.
      Parameters:
      numNodes - a int
      numEdges - a int
      maxDegree - a int
      probCycle - a double
      probTwoCycle - a double
      Returns:
      a Graph object
    • addTwoCycles

      public static void addTwoCycles(Graph graph, int numTwoCycles)

      addTwoCycles.

      Parameters:
      graph - a Graph object
      numTwoCycles - a int