Package edu.cmu.tetrad.graph
Class RandomGraph
java.lang.Object
edu.cmu.tetrad.graph.RandomGraph
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 ClassesModifier and TypeClassDescriptionstatic final classGenerates random DAGs uniformly with certain classes of DAGs using variants of Markov chain algorithm by Malancon, Dutour, and Philippe. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddTwoCycles(Graph graph, int numTwoCycles) addTwoCycles.static voidfixLatents1(int numLatentConfounders, Graph graph) fixLatents1.static voidfixLatents4(int numLatentConfounders, Graph graph) fixLatents4.static GraphrandomCyclicGraph2(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 GraphrandomCyclicGraph3(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 GraphrandomDag(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 GraphrandomDag(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 DagrandomDag(List<Node> nodes, int numLatentConfounders, int maxNumEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected) Generates a random Directed Acyclic Graph (DAG).static DagrandomDag(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 GraphrandomGraph(int numMeasures, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected) Generates a random graph based on the given parameters.static GraphrandomGraph(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 GraphrandomGraph(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 GraphrandomGraph(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 GraphrandomGraphRandomForwardEdges(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 GraphrandomGraphRandomForwardEdges(List<Node> nodes, int numLatentConfounders, int numEdges, int maxDegree, int maxIndegree, int maxOutdegree, boolean connected) Generates a random graph with forward edges.static GraphrandomGraphRandomForwardEdges(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 GraphrandomGraphRandomForwardEdges(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 GraphrandomGraphUniform(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 GraphrandomScaleFreeGraph(int numNodes, int numLatentConfounders, double alpha, double beta, double delta_in, double delta_out) Generates a random scale-free graph.
-
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 graphnumLatentConfounders- the number of latent confounders (unobserved variables) in the graphmaxNumEdges- the maximum number of edges in the graphmaxDegree- the maximum number of edges incident to a single node in the graphmaxIndegree- the maximum number of incoming edges for a single node in the graphmaxOutdegree- the maximum number of outgoing edges for a single node in the graphconnected- 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 DAGnumLatentConfounders- the number of latent confounders to include in the DAGmaxNumEdges- the maximum number of edges the DAG can havemaxDegree- the maximum degree (sum of indegree and outdegree) of any nodemaxIndegree- the maximum number of incoming edges for any nodemaxOutdegree- the maximum number of outgoing edges for any nodeconnected- whether the generated graph should ensure connectivityseed- 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 graphnumLatentConfounders- the number of latent confounders in the graphnumEdges- the number of edges in the graphmaxDegree- the maximum degree of each node in the graphmaxIndegree- the maximum indegree of each node in the graphmaxOutdegree- the maximum outdegree of each node in the graphconnected- 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 graphnumLatentConfounders- the number of latent confounding variables (nodes) to include in the graphnumEdges- the total number of edges in the graphmaxDegree- the maximum degree (number of edges connected) for any node in the graphmaxIndegree- the maximum number of incoming edges for any node in the graphmaxOutdegree- the maximum number of outgoing edges for any node in the graphconnected- whether the graph should be generated as a single connected componentseed- 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 withnumLatentConfounders- the number of latent confounders in the graphmaxNumEdges- the maximum number of edges in the graphmaxDegree- the maximum total degree (in-degree + out-degree) of each nodemaxIndegree- the maximum in-degree of each nodemaxOutdegree- the maximum out-degree of each nodeconnected- 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 graphnumLatentConfounders- The maximum number of latent confounders to be added to the graphmaxNumEdges- The maximum number of edges to be added to the graphmaxDegree- The maximum degree of each node in the graphmaxIndegree- The maximum indegree of each node in the graphmaxOutdegree- The maximum outdegree of each node in the graphconnected- 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 graphnumLatentConfounders- the number of latent confounders in the graphnumEdges- the number of edges in the graphmaxDegree- the maximum degree of a node in the graphmaxIndegree- the maximum indegree of a node in the graphmaxOutdegree- the maximum outdegree of a node in the graphconnected- indicates whether the graph should be connectedseed- 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 graphnumLatentConfounders- the number of latent confounders in the graphnumEdges- the total number of edges in the graphmaxDegree- the maximum number of edges connected to each nodemaxIndegree- the maximum in-degree of each nodemaxOutdegree- the maximum out-degree of each nodeconnected- 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 graphnumLatentConfounders- the number of latent confounders to add to the graphnumEdges- the total number of edges in the graphmaxDegree- the maximum degree (indegree plus outdegree) allowed for any nodemaxIndegree- the maximum number of incoming edges allowed for any nodemaxOutdegree- the maximum number of outgoing edges allowed for any nodeconnected- whether the graph should be connectedseed- 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 withnumLatentConfounders- the maximum number of latent confounders to includenumEdges- the desired number of edges in the graphmaxDegree- the maximum degree of a node in the graphmaxIndegree- the maximum indegree of a node in the graphmaxOutdegree- the maximum outdegree of a node in the graphconnected- indicates if the graph should be connectedlayoutAsCircle- indicates if the graph should be laid out in a circular patternseed- 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
fixLatents1.
- Parameters:
numLatentConfounders- a intgraph- aGraphobject
-
fixLatents4
fixLatents4.
- Parameters:
numLatentConfounders- a intgraph- aGraphobject
-
randomCyclicGraph2
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 intnumEdges- a intmaxDegree- a int- Returns:
- a
Graphobject
-
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 intnumEdges- a intmaxDegree- a intprobCycle- a doubleprobTwoCycle- a double- Returns:
- a
Graphobject
-
addTwoCycles
addTwoCycles.
- Parameters:
graph- aGraphobjectnumTwoCycles- a int
-