Package edu.cmu.tetrad.graph
Class Dag
java.lang.Object
edu.cmu.tetrad.graph.Dag
- All Implemented Interfaces:
Graph
,TetradSerializable
,Serializable
Represents a directed acyclic graph--that is, a graph containing only directed edges, with no cycles. Variables are
permitted to be either measured or latent, with at most one edge per node pair, and no edges to self.
- Version:
- $Id: $Id
- Author:
- josephramsey
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAmbiguousTriple
(Node x, Node y, Node z) Adds an ambiguous triple to the list of ambiguous triples.void
addAttribute
(String key, Object value) Adds an attribute to the graph.boolean
addBidirectedEdge
(Node node1, Node node2) Adds a bidirectional edge between two nodes.boolean
addDirectedEdge
(Node node1, Node node2) Adds a directed edge between two nodes.void
addDottedUnderlineTriple
(Node x, Node y, Node z) Adds a dotted underline triple to the graph.boolean
Adds a directed edge to the Directed Acyclic Graph (DAG).boolean
Adds a Node to the graph.boolean
addNondirectedEdge
(Node node1, Node node2) Adds a nondirected edge between two nodes in the graph.boolean
addPartiallyOrientedEdge
(Node node1, Node node2) Adds a partially oriented edge between two nodes.void
Adds a PropertyChangeListener to the underlying graph object.void
addUnderlineTriple
(Node x, Node y, Node z) Adds an underline triple to the current object.boolean
addUndirectedEdge
(Node node1, Node node2) Adds an undirected edge between two nodes.void
clear()
clear.boolean
containsEdge
(Edge edge) Checks if the given edge is present in the graph.boolean
containsNode
(Node node) Checks if the given Node object is contained in the graph.boolean
Compares thisGraph
object with the specified object for equality.void
fullyConnect
(Endpoint endpoint) Fully connects the given endpoint.getAdjacentNodes
(Node node) Retrieves the adjacent nodes of a given node in the graph.getAllAttributes.Returns a set of ambiguous triples.getAttribute
(String key) Retrieves the value associated with the given key in the attribute map.getChildren
(Node node) Retrieves the children of a specified Node in the graph.int
getDegree.int
Returns the degree of a given node in the graph.getDirectedEdge
(Node node1, Node node2) Returns the directed edge between the given nodes, if one exists in the graph.getDottedUnderlines.Retrieves the edge between two nodes in the graph.getEdges()
getEdges.Returns a list of edges connected to the given node.Returns a list of edges between the specified nodes in the graph.getEndpoint
(Node node1, Node node2) Returns the endpoint between two nodes in the graph.int
getIndegree
(Node node) Returns the indegree of the specified node in the graph.Retrieves the node in the graph with the specified name.getNodeNames.getNodes()
getNodes.getNodesInTo
(Node node, Endpoint n) Retrieves a list of nodes in the given graph that have edges pointing into the specified node and endpoint.getNodesOutTo
(Node node, Endpoint n) Retrieves a list of nodes that have outgoing edges to a specified node and endpoint.int
getNumEdges.int
getNumEdges
(Node node) Returns the number of edges connected to the specified node.int
getNumNodes.int
getOutdegree
(Node node) Returns the outdegree of the given node.getParents
(Node node) Retrieves the list of parent nodes for a given node in the graph.getSepset
(Node n1, Node n2, IndependenceTest test) Returns the sepset between two given nodes in the graph.getTimeLagGraph.Retrieves the set of underlined triples.boolean
isAdjacentTo
(Node node1, Node node2) Determines whether two nodes are adjacent in the graph.boolean
isAmbiguousTriple
(Node x, Node y, Node z) Determines if a triple of nodes is ambiguous.boolean
Checks if the given node1 is a child of node2 in the graph.boolean
isDefCollider
(Node node1, Node node2, Node node3) Checks if there is a definite collider between three nodes in the graph.boolean
isDefNoncollider
(Node node1, Node node2, Node node3) Checks if three given nodes form a definite non-collider in a graph.boolean
isExogenous
(Node node) Checks whether a given node is exogenous.boolean
isParameterizable
(Node node) Checks if the given node is parameterizable.boolean
isParentOf
(Node node1, Node node2) Determines if a given node is a parent of another node in the graph.boolean
isTimeLagModel.boolean
isUnderlineTriple
(Node x, Node y, Node z) Determines if a triple of nodes is underlined.paths()
paths.void
removeAmbiguousTriple
(Node x, Node y, Node z) Removes an ambiguous triple from the list of ambiguous triples.void
removeAttribute
(String key) Removes an attribute from the graph.void
removeDottedUnderlineTriple
(Node x, Node y, Node z) Removes a dotted underline triple from the set of triples.boolean
removeEdge
(Edge edge) Removes a given edge from the graph.boolean
removeEdge
(Node node1, Node node2) Removes the edge between two nodes in the graph.boolean
removeEdges
(Node node1, Node node2) Removes an edge between two nodes.boolean
removeEdges
(Collection<Edge> edges) Removes the given edges from the graph.boolean
removeNode
(Node node) Removes the specified node from the graph.boolean
removeNodes
(List<Node> nodes) Removes the specified nodes from the graph.void
Removes triples from the graph that contain nodes not present in the graph or are not adjacent to each other.void
removeUnderlineTriple
(Node x, Node y, Node z) Removes an underline triple from the list of underline triples.void
reorientAllWith
(Endpoint endpoint) Reorients all edges in a Directed Acyclic Graph (DAG) with a single endpoint type.static Dag
Generates a simple exemplar of this class to test serialization.void
setAmbiguousTriples
(Set<Triple> triples) Sets the ambiguous triples for the object.void
setDottedUnderLineTriples
(Set<Triple> triples) Sets the dotted underline triples for the given set of Triples.boolean
setEndpoint
(Node from, Node to, Endpoint endPoint) Sets the endpoint of a directed edge between two nodes in a graph.void
Set the nodes of the graph.void
setUnderLineTriples
(Set<Triple> triples) Sets the underlined triples.Returns a subgraph of the current graph consisting only of the specified nodes.toString()
toString.void
transferAttributes
(Graph graph) Transfers attributes from the given graph to the current graph.void
transferNodesAndEdges
(Graph graph) Transfers nodes and edges from the given graph to the current graph.Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface edu.cmu.tetrad.graph.Graph
existsSemidirectedPath, isAncestorOf
-
Constructor Details
-
Dag
public Dag()Constructs a new directed acyclic graph (DAG). -
Dag
-
Dag
Constructs a new directed acyclic graph from the given graph object.- Parameters:
graph
- the graph to base the new DAG on.- Throws:
IllegalArgumentException
- if the given graph cannot for some reason be converted into a DAG.
-
-
Method Details
-
serializableInstance
-
addBidirectedEdge
Adds a bidirectional edge between two nodes.- Specified by:
addBidirectedEdge
in interfaceGraph
- Parameters:
node1
- the first node to connect (aNode
object)node2
- the second node to connect (aNode
object)- Returns:
- true if the bidirectional edge was successfully added, false otherwise
-
addDirectedEdge
Adds a directed edge between two nodes.- Specified by:
addDirectedEdge
in interfaceGraph
- Parameters:
node1
- the first node to connect (source node)node2
- the second node to connect (target node)- Returns:
- true if the directed edge is successfully added, false otherwise
-
addUndirectedEdge
Adds an undirected edge between two nodes.- Specified by:
addUndirectedEdge
in interfaceGraph
- Parameters:
node1
- the first node to connect (aNode
object)node2
- the second node to connect (aNode
object)- Returns:
- true if the undirected edge is successfully added, false otherwise
-
addNondirectedEdge
Adds a nondirected edge between two nodes in the graph.- Specified by:
addNondirectedEdge
in interfaceGraph
- Parameters:
node1
- the first node to connect (aNode
object)node2
- the second node to connect (aNode
object)- Returns:
- true if the edge was successfully added, false otherwise
-
addPartiallyOrientedEdge
Adds a partially oriented edge between two nodes.- Specified by:
addPartiallyOrientedEdge
in interfaceGraph
- Parameters:
node1
- the first node to be connectednode2
- the second node to be connected- Returns:
- true if the partially oriented edge is added successfully, false otherwise
- Throws:
UnsupportedOperationException
- if the graph is a directed acyclic graph (DAG)
-
addEdge
Adds a directed edge to the Directed Acyclic Graph (DAG).- Specified by:
addEdge
in interfaceGraph
- Parameters:
edge
- the directedEdge
object to be added- Returns:
- true if the edge is successfully added, false otherwise
- Throws:
IllegalArgumentException
- if the provided edge is not a directed edge or adding the edge would result in a cycle
-
addNode
-
addPropertyChangeListener
Adds a PropertyChangeListener to the underlying graph object.- Specified by:
addPropertyChangeListener
in interfaceGraph
- Parameters:
e
- the PropertyChangeListener to be added
-
clear
-
containsEdge
Checks if the given edge is present in the graph.- Specified by:
containsEdge
in interfaceGraph
- Parameters:
edge
- the edge to check if present in the graph- Returns:
- true if the edge is present in the graph, false otherwise
-
containsNode
Checks if the given Node object is contained in the graph.- Specified by:
containsNode
in interfaceGraph
- Parameters:
node
- The Node object to check for containment. Must not be null.- Returns:
- true if the Node object is contained in the graph, false otherwise.
-
equals
Compares thisGraph
object with the specified object for equality. -
fullyConnect
Fully connects the given endpoint.- Specified by:
fullyConnect
in interfaceGraph
- Parameters:
endpoint
- The endpoint to fully connect.- Throws:
UnsupportedOperationException
- If the endpoint is a single endpoint type and cannot be fully connected.
-
reorientAllWith
Reorients all edges in a Directed Acyclic Graph (DAG) with a single endpoint type.- Specified by:
reorientAllWith
in interfaceGraph
- Parameters:
endpoint
- The type of endpoint to reorient all edges with. Must be an instance of `edu.cmu.tetrad.graph.Endpoint`.- Throws:
UnsupportedOperationException
- if attempting to reorient all edges in a DAG with a single endpoint type.
-
getAdjacentNodes
Retrieves the adjacent nodes of a given node in the graph.- Specified by:
getAdjacentNodes
in interfaceGraph
- Parameters:
node
- the node for which to retrieve adjacent nodes- Returns:
- a List of nodes that are adjacent to the given node
-
getChildren
Retrieves the children of a specified Node in the graph.- Specified by:
getChildren
in interfaceGraph
- Parameters:
node
- The Node object whose children are to be retrieved.- Returns:
- A List of Node objects representing the children of the specified node.
-
getDegree
-
getEdge
Retrieves the edge between two nodes in the graph. -
getDirectedEdge
Returns the directed edge between the given nodes, if one exists in the graph.- Specified by:
getDirectedEdge
in interfaceGraph
- Parameters:
node1
- the first Node objectnode2
- the second Node object- Returns:
- the directed edge between the given nodes, or null if no edge exists
-
getEdges
-
getEdges
Returns a list of edges between the specified nodes in the graph. -
getEdges
-
getEndpoint
Returns the endpoint between two nodes in the graph.- Specified by:
getEndpoint
in interfaceGraph
- Parameters:
node1
- a Node object representing the first nodenode2
- a Node object representing the second node- Returns:
- the Endpoint object representing the endpoint between the two nodes
-
getIndegree
Returns the indegree of the specified node in the graph.- Specified by:
getIndegree
in interfaceGraph
- Parameters:
node
- the node for which to find the indegree- Returns:
- the indegree of the specified node
-
getDegree
-
getNode
-
getNodes
-
setNodes
-
getNodeNames
getNodeNames.
- Specified by:
getNodeNames
in interfaceGraph
- Returns:
- a
List
object
-
getNumEdges
-
getNumEdges
Returns the number of edges connected to the specified node.- Specified by:
getNumEdges
in interfaceGraph
- Parameters:
node
- the node for which to retrieve the number of edges- Returns:
- the number of edges connected to the specified node
-
getNumNodes
-
getOutdegree
Returns the outdegree of the given node.- Specified by:
getOutdegree
in interfaceGraph
- Parameters:
node
- aNode
object- Returns:
- the outdegree of the node
-
getParents
Retrieves the list of parent nodes for a given node in the graph.- Specified by:
getParents
in interfaceGraph
- Parameters:
node
- the node for which to retrieve the parent nodes- Returns:
- the list of parent nodes for the given node
-
isAdjacentTo
Determines whether two nodes are adjacent in the graph.- Specified by:
isAdjacentTo
in interfaceGraph
- Parameters:
node1
- The first node to check adjacency.node2
- The second node to check adjacency.- Returns:
- true if the nodes are adjacent, false otherwise.
-
isChildOf
Checks if the given node1 is a child of node2 in the graph. -
isParentOf
Determines if a given node is a parent of another node in the graph.- Specified by:
isParentOf
in interfaceGraph
- Parameters:
node1
- the first node to be compared.node2
- the second node to be compared.- Returns:
- true if node1 is a parent of node2, false otherwise.
-
isDefNoncollider
Checks if three given nodes form a definite non-collider in a graph.- Specified by:
isDefNoncollider
in interfaceGraph
- Parameters:
node1
- the first nodenode2
- the second nodenode3
- the third node- Returns:
- true if the three nodes form a definite non-collider, false otherwise
-
isDefCollider
Checks if there is a definite collider between three nodes in the graph.- Specified by:
isDefCollider
in interfaceGraph
- Parameters:
node1
- the first node to checknode2
- the second node to checknode3
- the third node to check- Returns:
- true if there is a definite collider, false otherwise
-
isExogenous
Checks whether a given node is exogenous.- Specified by:
isExogenous
in interfaceGraph
- Parameters:
node
- ANode
object representing the node to be checked.- Returns:
- True if the given node is exogenous, false otherwise.
-
getNodesInTo
Retrieves a list of nodes in the given graph that have edges pointing into the specified node and endpoint.- Specified by:
getNodesInTo
in interfaceGraph
- Parameters:
node
- the node to check for incoming edgesn
- the endpoint to check for incoming edges- Returns:
- a list of nodes with edges pointing into the specified node and endpoint
-
getNodesOutTo
Retrieves a list of nodes that have outgoing edges to a specified node and endpoint.- Specified by:
getNodesOutTo
in interfaceGraph
- Parameters:
node
- The node to which the outgoing edges lead.n
- The endpoint to which the outgoing edges connect to the specified node.- Returns:
- A list of nodes that have outgoing edges to the specified node and endpoint.
-
removeEdge
Removes a given edge from the graph.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
edge
- the edge to be removed- Returns:
- true if the edge was successfully removed, false otherwise
-
removeEdge
Removes the edge between two nodes in the graph.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
node1
- the first node to remove the edge fromnode2
- the second node to remove the edge to- Returns:
- true if the edge was successfully removed, false otherwise
-
removeEdges
Removes an edge between two nodes.- Specified by:
removeEdges
in interfaceGraph
- Parameters:
node1
- the first nodenode2
- the second node- Returns:
- true if the edge was successfully removed, false otherwise
-
removeEdges
Removes the given edges from the graph.- Specified by:
removeEdges
in interfaceGraph
- Parameters:
edges
- a collection of edges to be removed from the graph- Returns:
- true if all the edges were successfully removed, false otherwise
-
removeNode
Removes the specified node from the graph.- Specified by:
removeNode
in interfaceGraph
- Parameters:
node
- the node to be removed from the graph- Returns:
- true if the node was successfully removed, false otherwise
-
removeNodes
Removes the specified nodes from the graph.- Specified by:
removeNodes
in interfaceGraph
- Parameters:
nodes
- aList
of nodes to remove- Returns:
true
if the nodes were successfully removed,false
otherwise
-
setEndpoint
Sets the endpoint of a directed edge between two nodes in a graph.- Specified by:
setEndpoint
in interfaceGraph
- Parameters:
from
- the starting node of the edgeto
- the ending node of the edgeendPoint
- the endpoint of the directed edge- Returns:
true
if the endpoint was successfully set,false
otherwise
-
subgraph
-
toString
-
transferNodesAndEdges
Transfers nodes and edges from the given graph to the current graph.- Specified by:
transferNodesAndEdges
in interfaceGraph
- Parameters:
graph
- the graph from which nodes and edges are to be pilfered- Throws:
IllegalArgumentException
- if the graph is null, or if adding a node/edge failsNullPointerException
- if no graph is provided
-
transferAttributes
Transfers attributes from the given graph to the current graph.- Specified by:
transferAttributes
in interfaceGraph
- Parameters:
graph
- aGraph
object representing the graph from which attributes will be transferred- Throws:
IllegalArgumentException
- if the graph is null
-
paths
-
isParameterizable
Checks if the given node is parameterizable.- Specified by:
isParameterizable
in interfaceGraph
- Parameters:
node
- the node to be checked for parameterizability- Returns:
true
if the node is parameterizable,false
otherwise
-
isTimeLagModel
public boolean isTimeLagModel()isTimeLagModel.
- Specified by:
isTimeLagModel
in interfaceGraph
- Returns:
- a boolean
-
getTimeLagGraph
getTimeLagGraph.
- Specified by:
getTimeLagGraph
in interfaceGraph
- Returns:
- a
TimeLagGraph
object
-
getSepset
Returns the sepset between two given nodes in the graph. -
getAllAttributes
-
getAttribute
Retrieves the value associated with the given key in the attribute map.- Specified by:
getAttribute
in interfaceGraph
- Parameters:
key
- the key of the attribute to be retrieved- Returns:
- the value associated with the given key
-
removeAttribute
Removes an attribute from the graph.- Specified by:
removeAttribute
in interfaceGraph
- Parameters:
key
- the key of the attribute to remove
-
addAttribute
Adds an attribute to the graph.- Specified by:
addAttribute
in interfaceGraph
- Parameters:
key
- the key of the attributevalue
- the value of the attribute
-
getAmbiguousTriples
Returns a set of ambiguous triples.- Specified by:
getAmbiguousTriples
in interfaceGraph
- Returns:
- a set of ambiguous triples
-
setAmbiguousTriples
Sets the ambiguous triples for the object.- Specified by:
setAmbiguousTriples
in interfaceGraph
- Parameters:
triples
- a set of Triple objects representing ambiguous triples
-
getUnderLines
Retrieves the set of underlined triples.- Specified by:
getUnderLines
in interfaceGraph
- Returns:
- The set of underlined triples as a
Set
ofTriple
objects.
-
getDottedUnderlines
getDottedUnderlines.
- Specified by:
getDottedUnderlines
in interfaceGraph
- Returns:
- a
Set
object
-
isAmbiguousTriple
Determines if a triple of nodes is ambiguous.- Specified by:
isAmbiguousTriple
in interfaceGraph
- Parameters:
x
- the first node in the triple.y
- the second node in the triple.z
- the third node in the triple.- Returns:
- true if the triple is ambiguous, false otherwise.
-
isUnderlineTriple
Determines if a triple of nodes is underlined.- Specified by:
isUnderlineTriple
in interfaceGraph
- Parameters:
x
- the first Node in the tripley
- the second Node in the triplez
- the third Node in the triple- Returns:
- true if the triple is underlined, false otherwise
-
addAmbiguousTriple
Adds an ambiguous triple to the list of ambiguous triples. An ambiguous triple consists of three nodes: x, y, and z.- Specified by:
addAmbiguousTriple
in interfaceGraph
- Parameters:
x
- the first node in the ambiguous tripley
- the second node in the ambiguous triplez
- the third node in the ambiguous triple
-
addUnderlineTriple
-
addDottedUnderlineTriple
Adds a dotted underline triple to the graph.- Specified by:
addDottedUnderlineTriple
in interfaceGraph
- Parameters:
x
- The first node of the triple.y
- The second node of the triple.z
- The third node of the triple.
-
removeAmbiguousTriple
Removes an ambiguous triple from the list of ambiguous triples.- Specified by:
removeAmbiguousTriple
in interfaceGraph
- Parameters:
x
- The first node of the triple.y
- The second node of the triple.z
- The third node of the triple.
-
removeUnderlineTriple
Removes an underline triple from the list of underline triples.- Specified by:
removeUnderlineTriple
in interfaceGraph
- Parameters:
x
- The firstNode
object in the underline triple.y
- The secondNode
object in the underline triple.z
- The thirdNode
object in the underline triple.
-
removeDottedUnderlineTriple
Removes a dotted underline triple from the set of triples.- Specified by:
removeDottedUnderlineTriple
in interfaceGraph
- Parameters:
x
- the first node of the triple to be removedy
- the second node of the triple to be removedz
- the third node of the triple to be removed
-
setUnderLineTriples
Sets the underlined triples.- Specified by:
setUnderLineTriples
in interfaceGraph
- Parameters:
triples
- a set of triples to be set as underlined
-
setDottedUnderLineTriples
Sets the dotted underline triples for the given set of Triples. Clears the existing dotted underline triples and adds the new ones from the set.- Specified by:
setDottedUnderLineTriples
in interfaceGraph
- Parameters:
triples
- a Set of Triples to set as dotted underline triples
-
removeTriplesNotInGraph
public void removeTriplesNotInGraph()Removes triples from the graph that contain nodes not present in the graph or are not adjacent to each other.- Specified by:
removeTriplesNotInGraph
in interfaceGraph
-