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) addAmbiguousTriple.void
addAttribute
(String key, Object value) addAttribute.boolean
addBidirectedEdge
(Node node1, Node node2) Adds a bidirectional edge between two nodes to the graph.boolean
addDirectedEdge
(Node node1, Node node2) Adds a directed edge between two nodes.void
addDottedUnderlineTriple
(Node x, Node y, Node z) addDottedUnderlineTriple.boolean
Adds a directed edge to the DAG (Directed Acyclic Graph).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 this object.void
addUnderlineTriple
(Node x, Node y, Node z) addUnderlineTriple.boolean
addUndirectedEdge
(Node node1, Node node2) Adds an undirected edge between two nodes.void
clear()
clear.boolean
containsEdge
(Edge edge) Checks if the graph contains the specified edge.boolean
containsNode
(Node node) Checks if the specified node is contained in the graph.boolean
Compares this graph with the specified object for equality.void
fullyConnect
(Endpoint endpoint) This method fully connects a Directed Acyclic Graph (DAG) with a single endpoint type.getAdjacentNodes
(Node node) Retrieves the adjacent nodes of a given node in the graph.getAllAttributes.Getter for the fieldambiguousTriples
.getAttribute
(String key) getAttribute.getChildren
(Node node) Returns a list of children nodes for the given node.int
getDegree.int
Returns the degree of a given Node in the graph.getDirectedEdge
(Node node1, Node node2) Retrieves the directed edge between two nodes in the graph.getDottedUnderlines.Retrieves the edge between two given nodes in the graph.getEdges()
getEdges.Returns a list of edges connected to the given node.Returns a list of edges between two nodes.getEndpoint
(Node node1, Node node2) Gets the endpoint between two nodes in the graph.int
getIndegree
(Node node) Returns the indegree of the given node in the graph.Retrieves a node from the graph by its name.getNodeNames.getNodes()
getNodes.getNodesInTo
(Node node, Endpoint n) Nodes adjacent to the given node with the given proximal endpoint.getNodesOutTo
(Node node, Endpoint n) Nodes adjacent to the given node with the given distal 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 a given node.getParents
(Node node) Retrieves the parents of a given Node in the graph.getSepset.getTimeLagGraph.getUnderLines.boolean
isAdjacentTo
(Node node1, Node node2) Checks if two nodes are adjacent in the graph.boolean
isAmbiguousTriple
(Node x, Node y, Node z) States whether r-s-r is an underline triple or not.boolean
isChildOf.boolean
isDefCollider
(Node node1, Node node2, Node node3) Added by ekorber, 2004/6/9.boolean
isDefNoncollider
(Node node1, Node node2, Node node3) Added by ekorber, 2004/6/9.boolean
isExogenous
(Node node) isExogenous.boolean
isParameterizable
(Node node) isParameterizable.boolean
isParentOf
(Node node1, Node node2) Determines whether node1 is a parent of node2.boolean
isTimeLagModel.boolean
isUnderlineTriple
(Node x, Node y, Node z) States whether r-s-r is an underline triple or not.paths()
paths.void
removeAmbiguousTriple
(Node x, Node y, Node z) removeAmbiguousTriple.void
removeAttribute
(String key) removeAttribute.void
removeDottedUnderlineTriple
(Node x, Node y, Node z) removeDottedUnderlineTriple.boolean
removeEdge
(Edge edge) Removes the given edge from the graph.boolean
removeEdge
(Node node1, Node node2) Removes the edge connecting the two given nodes, provided there is exactly one such edge.boolean
removeEdges
(Node node1, Node node2) Removes all edges connecting node A to node B.boolean
removeEdges
(Collection<Edge> edges) Iterates through the list and removes any permissible edges found.boolean
removeNode
(Node node) Removes a node from the graph.boolean
removeNodes
(List<Node> nodes) Iterates through the list and removes any permissible nodes found.void
removeTriplesNotInGraph.void
removeUnderlineTriple
(Node x, Node y, Node z) removeUnderlineTriple.void
reorientAllWith
(Endpoint endpoint) Reorients all edges in a directed acyclic graph (DAG) with a single specified endpoint type.static Dag
Generates a simple exemplar of this class to test serialization.void
setAmbiguousTriples
(Set<Triple> triples) setAmbiguousTriples.void
setDottedUnderLineTriples
(Set<Triple> triples) setDottedUnderLineTriples.boolean
setEndpoint
(Node from, Node to, Endpoint endPoint) Sets the endpoint type at the 'to' end of the edge from 'from' to 'to' to the given endpoint.void
Sets the list of nodes in the graph.void
setUnderLineTriples
(Set<Triple> triples) setUnderLineTriples.Constructs and returns a subgraph consisting of a given subset of the nodes of this graph together with the edges between them.toString()
toString.void
transferAttributes
(Graph graph) transferAttributes.void
transferNodesAndEdges
(Graph graph) Transfers nodes and edges from one graph to another.
-
Constructor Details
-
Dag
public Dag()Constructs a new directed acyclic graph (DAG). -
Dag
Constructor for Dag.
- Parameters:
nodes
- aList
object
-
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
Generates a simple exemplar of this class to test serialization.- Returns:
- a
Dag
object
-
addBidirectedEdge
Adds a bidirectional edge between two nodes to the graph.- Specified by:
addBidirectedEdge
in interfaceGraph
- Parameters:
node1
- the first node to connectnode2
- the second node to connect- Returns:
- true if the 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 be connected (source node)node2
- the second node to be connected (target node)- Returns:
- true if the directed edge was successfully added, false otherwise
-
addUndirectedEdge
Adds an undirected edge between two nodes.- Specified by:
addUndirectedEdge
in interfaceGraph
- Parameters:
node1
- The first node involved in the edge.node2
- The second node involved in the edge.- Returns:
- True if the edge was successfully added, false otherwise.
- Throws:
UnsupportedOperationException
- if the operation is disallowed for a Directed Acyclic Graph (DAG).
-
addNondirectedEdge
Adds a nondirected edge between two nodes in the graph.- Specified by:
addNondirectedEdge
in interfaceGraph
- Parameters:
node1
- The first node to connect.node2
- The second node to connect.- Returns:
- Returns true if the nondirected edge was added successfully.
- Throws:
UnsupportedOperationException
- Thrown if the graph is a directed acyclic graph (DAG).
-
addPartiallyOrientedEdge
Adds a partially oriented edge between two nodes.- Specified by:
addPartiallyOrientedEdge
in interfaceGraph
- Parameters:
node1
- The first node involved in the edge. Must be aNode
object.node2
- The second node involved in the edge. Must be aNode
object.- Returns:
- True if the edge was added successfully, false otherwise.
- Throws:
UnsupportedOperationException
- If the graph is a Directed Acyclic Graph (DAG), this operation is not allowed.
-
addEdge
Adds a directed edge to the DAG (Directed Acyclic Graph).- Specified by:
addEdge
in interfaceGraph
- Parameters:
edge
- The Edge object to be added.- Returns:
- Returns true if the edge was successfully added to the DAG, false otherwise.
- Throws:
IllegalArgumentException
- if the given edge is not a directed edge.IllegalArgumentException
- if adding the edge would create a cycle in the DAG.
-
addNode
Adds a node to the graph. -
addPropertyChangeListener
Adds a PropertyChangeListener to this object.- Specified by:
addPropertyChangeListener
in interfaceGraph
- Parameters:
e
- the PropertyChangeListener to be added
-
clear
public void clear()clear.
-
containsEdge
Checks if the graph contains the specified edge.- Specified by:
containsEdge
in interfaceGraph
- Parameters:
edge
- the edge to be checked.- Returns:
- true if the graph contains the edge, false otherwise.
-
containsNode
Checks if the specified node is contained in the graph.- Specified by:
containsNode
in interfaceGraph
- Parameters:
node
- the node to check if contained in the graph- Returns:
- true if the node is contained in the graph, false otherwise
-
equals
Compares this graph with the specified object for equality. -
fullyConnect
This method fully connects a Directed Acyclic Graph (DAG) with a single endpoint type.- Specified by:
fullyConnect
in interfaceGraph
- Parameters:
endpoint
- the endpoint to be fully connected- Throws:
UnsupportedOperationException
- if the DAG has a single endpoint type
-
reorientAllWith
Reorients all edges in a directed acyclic graph (DAG) with a single specified endpoint type.- Specified by:
reorientAllWith
in interfaceGraph
- Parameters:
endpoint
- the endpoint type to reorient the edges with. It should be a validEndpoint
object.- Throws:
UnsupportedOperationException
- if the DAG does not have a single endpoint type, i.e., it is not a DAG.
-
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 the adjacent nodes.- Returns:
- A list of adjacent nodes to the given node.
-
getChildren
Returns a list of children nodes for the given node.- Specified by:
getChildren
in interfaceGraph
- Parameters:
node
- The node for which to retrieve the children.- Returns:
- A list of children nodes.
-
getDegree
public int getDegree()getDegree.
-
getEdge
Retrieves the edge between two given nodes in the graph. -
getDirectedEdge
Retrieves the directed edge between two nodes in the graph.- Specified by:
getDirectedEdge
in interfaceGraph
- Parameters:
node1
- The first node.node2
- The second node.- Returns:
- The directed edge between the two nodes, or null if there is no such edge.
-
getEdges
Returns a list of edges connected to the given node. -
getEdges
Returns a list of edges between two nodes. -
getEdges
getEdges.
-
getEndpoint
Gets the endpoint between two nodes in the graph.- Specified by:
getEndpoint
in interfaceGraph
- Parameters:
node1
- the first nodenode2
- the second node- Returns:
- the endpoint between the two nodes
-
getIndegree
Returns the indegree of the given node in the graph.- Specified by:
getIndegree
in interfaceGraph
- Parameters:
node
- the node whose indegree is to be determined- Returns:
- the indegree of the given node
-
getDegree
Returns the degree of a given Node in the graph. -
getNode
Retrieves a node from the graph by its name. -
getNodes
getNodes.
-
setNodes
Sets the list of nodes in the graph. -
getNodeNames
getNodeNames.
- Specified by:
getNodeNames
in interfaceGraph
- Returns:
- a
List
object
-
getNumEdges
public int getNumEdges()getNumEdges.
- Specified by:
getNumEdges
in interfaceGraph
- Returns:
- a int
-
getNumEdges
Returns the number of edges connected to the specified node.- Specified by:
getNumEdges
in interfaceGraph
- Parameters:
node
- aNode
object representing the node of interest- Returns:
- the number of edges connected to the specified node
-
getNumNodes
public int getNumNodes()getNumNodes.
- Specified by:
getNumNodes
in interfaceGraph
- Returns:
- a int
-
getOutdegree
Returns the outdegree of a given node.- Specified by:
getOutdegree
in interfaceGraph
- Parameters:
node
- aNode
object representing the node whose outdegree is to be retrieved- Returns:
- an integer value representing the outdegree of the node
-
getParents
Retrieves the parents of a given Node in the graph.- Specified by:
getParents
in interfaceGraph
- Parameters:
node
- The Node for which to retrieve the parents.- Returns:
- A List of Nodes representing the parents.
-
isAdjacentTo
Checks if two nodes are adjacent in the graph.- Specified by:
isAdjacentTo
in interfaceGraph
- Parameters:
node1
- The first node to check adjacency for. (NonNull)node2
- The second node to check adjacency for. (NonNull)- Returns:
- true if the nodes are adjacent, false otherwise.
-
isChildOf
isChildOf.
-
isParentOf
Determines whether node1 is a parent of node2.- Specified by:
isParentOf
in interfaceGraph
- Parameters:
node1
- aNode
objectnode2
- aNode
object- Returns:
- a boolean
-
isDefNoncollider
Added by ekorber, 2004/6/9.- Specified by:
isDefNoncollider
in interfaceGraph
- Parameters:
node1
- aNode
objectnode2
- aNode
objectnode3
- aNode
object- Returns:
- true if node 2 is a definite noncollider between 1 and 3
-
isDefCollider
Added by ekorber, 2004/6/9.- Specified by:
isDefCollider
in interfaceGraph
- Parameters:
node1
- aNode
objectnode2
- aNode
objectnode3
- aNode
object- Returns:
- true if node 2 is a definite collider between 1 and 3
-
isExogenous
isExogenous.
- Specified by:
isExogenous
in interfaceGraph
- Parameters:
node
- aNode
object- Returns:
- true iff the given node is exogenous in the graph.
-
getNodesInTo
Nodes adjacent to the given node with the given proximal endpoint.- Specified by:
getNodesInTo
in interfaceGraph
- Parameters:
node
- aNode
objectn
- aEndpoint
object- Returns:
- a
List
object
-
getNodesOutTo
Nodes adjacent to the given node with the given distal endpoint.- Specified by:
getNodesOutTo
in interfaceGraph
- Parameters:
node
- aNode
objectn
- aEndpoint
object- Returns:
- a
List
object
-
removeEdge
Removes the given edge from the graph.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
edge
- aEdge
object- Returns:
- true if the edge was removed, false if not.
-
removeEdge
Removes the edge connecting the two given nodes, provided there is exactly one such edge.- Specified by:
removeEdge
in interfaceGraph
- Parameters:
node1
- aNode
objectnode2
- aNode
object- Returns:
- a boolean
-
removeEdges
Removes all edges connecting node A to node B. In most cases, this will remove at most one edge, but since multiple edges are permitted in some graph implementations, the number will in some cases be greater than one.- Specified by:
removeEdges
in interfaceGraph
- Parameters:
node1
- aNode
objectnode2
- aNode
object- Returns:
- true if edges were removed, false if not.
-
removeEdges
Iterates through the list and removes any permissible edges found. The order in which edges are added is the order in which they are presented in the iterator.- Specified by:
removeEdges
in interfaceGraph
- Parameters:
edges
- aCollection
object- Returns:
- true if edges were added, false if not.
-
removeNode
Removes a node from the graph.- Specified by:
removeNode
in interfaceGraph
- Parameters:
node
- aNode
object- Returns:
- true if the node was removed, false if not.
-
removeNodes
Iterates through the list and removes any permissible nodes found. The order in which nodes are removed is the order in which they are presented in the iterator.- Specified by:
removeNodes
in interfaceGraph
- Parameters:
nodes
- aList
object- Returns:
- true if nodes were added, false if not.
-
setEndpoint
Sets the endpoint type at the 'to' end of the edge from 'from' to 'to' to the given endpoint. Note: NOT CONSTRAINT SAFE- Specified by:
setEndpoint
in interfaceGraph
- Parameters:
from
- aNode
objectto
- aNode
objectendPoint
- aEndpoint
object- Returns:
- a boolean
-
subgraph
Constructs and returns a subgraph consisting of a given subset of the nodes of this graph together with the edges between them. -
toString
toString.
-
transferNodesAndEdges
Transfers nodes and edges from one graph to another. One way this is used is to change graph types. One constructs a new graph based on the old graph, and this method is called to transfer the nodes and edges of the old graph to the new graph.- Specified by:
transferNodesAndEdges
in interfaceGraph
- Parameters:
graph
- the graph from which nodes and edges are to be pilfered.- Throws:
IllegalArgumentException
- This exception is thrown if adding some node.
-
transferAttributes
transferAttributes.
- Specified by:
transferAttributes
in interfaceGraph
- Parameters:
graph
- aGraph
object- Throws:
IllegalArgumentException
- if any.
-
paths
paths.
-
isParameterizable
isParameterizable.
- Specified by:
isParameterizable
in interfaceGraph
- Parameters:
node
- aNode
object- Returns:
- true if the given node is parameterizable.
-
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
getSepset.
-
getAllAttributes
getAllAttributes.
- Specified by:
getAllAttributes
in interfaceGraph
- Returns:
- a
Map
object
-
getAttribute
getAttribute.
- Specified by:
getAttribute
in interfaceGraph
- Parameters:
key
- aString
object- Returns:
- a
Object
object
-
removeAttribute
removeAttribute.
- Specified by:
removeAttribute
in interfaceGraph
- Parameters:
key
- aString
object
-
addAttribute
addAttribute.
- Specified by:
addAttribute
in interfaceGraph
- Parameters:
key
- aString
objectvalue
- aObject
object
-
getAmbiguousTriples
Getter for the field
ambiguousTriples
.- Specified by:
getAmbiguousTriples
in interfaceGraph
- Returns:
- a
Set
object
-
setAmbiguousTriples
setAmbiguousTriples.
- Specified by:
setAmbiguousTriples
in interfaceGraph
- Parameters:
triples
- aSet
object
-
getUnderLines
getUnderLines.
- Specified by:
getUnderLines
in interfaceGraph
- Returns:
- a
Set
object
-
getDottedUnderlines
getDottedUnderlines.
- Specified by:
getDottedUnderlines
in interfaceGraph
- Returns:
- a
Set
object
-
isAmbiguousTriple
States whether r-s-r is an underline triple or not.States whether r-s-r is an underline triple or not.
- Specified by:
isAmbiguousTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object- Returns:
- a boolean
-
isUnderlineTriple
States whether r-s-r is an underline triple or not.States whether r-s-r is an underline triple or not.
- Specified by:
isUnderlineTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object- Returns:
- a boolean
-
addAmbiguousTriple
addAmbiguousTriple.
- Specified by:
addAmbiguousTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object
-
addUnderlineTriple
addUnderlineTriple.
- Specified by:
addUnderlineTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object
-
addDottedUnderlineTriple
addDottedUnderlineTriple.
- Specified by:
addDottedUnderlineTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object
-
removeAmbiguousTriple
removeAmbiguousTriple.
- Specified by:
removeAmbiguousTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object
-
removeUnderlineTriple
removeUnderlineTriple.
- Specified by:
removeUnderlineTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object
-
removeDottedUnderlineTriple
removeDottedUnderlineTriple.
- Specified by:
removeDottedUnderlineTriple
in interfaceGraph
- Parameters:
x
- aNode
objecty
- aNode
objectz
- aNode
object
-
setUnderLineTriples
setUnderLineTriples.
- Specified by:
setUnderLineTriples
in interfaceGraph
- Parameters:
triples
- aSet
object
-
setDottedUnderLineTriples
setDottedUnderLineTriples.
- Specified by:
setDottedUnderLineTriples
in interfaceGraph
- Parameters:
triples
- aSet
object
-
removeTriplesNotInGraph
public void removeTriplesNotInGraph()removeTriplesNotInGraph.
- Specified by:
removeTriplesNotInGraph
in interfaceGraph
-