dai::DAG Class Reference

Represents the neighborhood structure of nodes in a directed cyclic graph. More...

#include <dai/dag.h>

List of all members.

Input and output

void printDot (std::ostream &os) const
 Writes this DAG to an output stream in GraphViz .dot syntax.
std::ostream & operator<< (std::ostream &os, const DAG &g)
 Writes this DAG to an output stream.

Public Member Functions

Constructors and destructors
 DAG ()
 Default constructor (creates an empty DAG).
 DAG (size_t nr)
 Constructs DAG with nr nodes and no edges.
template<typename EdgeInputIterator>
 DAG (size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check=true)
 Constructs DAG from a range of edges.
Accessors and mutators
const Neighborpa (size_t n, size_t _p) const
 Returns constant reference to the _p 'th parent of node n.
Neighborpa (size_t n, size_t _p)
 Returns reference to the _p 'th parent of node n.
const Neighborspa (size_t n) const
 Returns constant reference to all parents of node n.
Neighborspa (size_t n)
 Returns reference to all parents of node n.
const Neighborch (size_t n, size_t _c) const
 Returns constant reference to the _c 'th child of node n.
Neighborch (size_t n, size_t _c)
 Returns reference to the _c 'th child of node n.
const Neighborsch (size_t n) const
 Returns constant reference to all children of node n.
Neighborsch (size_t n)
 Returns reference to all children of node n.
Adding nodes and edges
template<typename EdgeInputIterator>
void construct (size_t nr, EdgeInputIterator begin, EdgeInputIterator end, bool check=true)
 (Re)constructs DAG from a range of edges.
size_t addNode ()
 Adds a node without parents and children and returns the index of the added node.
template<typename NodeInputIterator>
size_t addNode (NodeInputIterator begin, NodeInputIterator end, size_t sizeHint=0)
 Adds a node with parents specified by a range of nodes.
DAGaddEdge (size_t n1, size_t n2, bool check=true)
 Adds an edge from node n1 and node n2.
Erasing nodes and edges
void eraseNode (size_t n)
 Removes node n and all ingoing and outgoing edges; indices of other nodes are changed accordingly.
void eraseEdge (size_t n1, size_t n2)
 Removes edge from node n1 to node n2.
Queries
size_t nrNodes () const
 Returns number of nodes.
size_t nrEdges () const
 Calculates the number of edges, time complexity: O(nrNodes()).
bool hasEdge (size_t n1, size_t n2) const
 Returns true if the DAG contains an edge from node n1 and n2.
size_t findPa (size_t n, size_t p)
 Returns the index of a given node p amongst the parents of n.
size_t findCh (size_t n, size_t c)
 Returns the index of a given node c amongst the children of n.
SmallSet< size_t > paSet (size_t n) const
 Returns parents of node n as a SmallSet<size_t>.
SmallSet< size_t > chSet (size_t n) const
 Returns children of node n as a SmallSet<size_t>.
std::set< size_t > ancestors (size_t n) const
 Returns the set of ancestors of node n, i.e., all nodes a such that there exists a directed path from a to n (excluding n itself).
std::set< size_t > descendants (size_t n) const
 Returns the set of descendants of node n, i.e., all nodes d such that there exists a directed path from n to d (excluding n itself).
bool existsDirectedPath (size_t n1, size_t n2) const
 Returns whether there exists a directed path from node n1 to node n2 (which may consist of zero edges).
bool isConnected () const
 Returns true if the DAG is connected.
void checkConsistency () const
 Asserts internal consistency.
bool operator== (const DAG &x) const
 Comparison operator which returns true if two DAGs are identical.

Private Types

typedef dai::Neighbor Neighbor
 Describes the parent/child relationship of two nodes in a DAG.
typedef dai::Neighbors Neighbors
 Type for storing the parents/children of some node.
typedef dai::Edge Edge
 Represents a directed edge: an Edge(n1,n2) corresponds to the edge from node n1 to node n2.

Private Attributes

std::vector< Neighbors_pa
 Contains for each node a vector of its parent nodes.
std::vector< Neighbors_ch
 Contains for each node a vector of its children nodes.


Detailed Description

Represents the neighborhood structure of nodes in a directed cyclic graph.

A directed cyclic graph has nodes connected by directed edges, such that there is no directed cycle of edges n1->n2->n3->...->n1. Nodes are indexed by an unsigned integer. If there are nrNodes() nodes, they are numbered 0,1,2,...,nrNodes()-1. An edge from node n1 to node n2 is represented by a Edge(n1,n2).

DAG is implemented as a sparse adjacency list, i.e., it stores for each node a list of its parents and a list of its children. Both lists are implemented as a vector of Neighbor structures (accessible by the pa() and ch() methods). Thus, each node has two associated variables of type DAG::Neighbors, which are vectors of Neighbor structures, describing their parent and children nodes.


Member Typedef Documentation

typedef dai::Neighbor dai::DAG::Neighbor [private]

Describes the parent/child relationship of two nodes in a DAG.

Deprecated:
Please use dai::Neighbor instead

Type for storing the parents/children of some node.

Deprecated:
Please use dai::Neighbors instead

typedef dai::Edge dai::DAG::Edge [private]

Represents a directed edge: an Edge(n1,n2) corresponds to the edge from node n1 to node n2.

Deprecated:
Please use dai::Edge instead


Constructor & Destructor Documentation

dai::DAG::DAG (  )  [inline]

Default constructor (creates an empty DAG).

dai::DAG::DAG ( size_t  nr  )  [inline]

Constructs DAG with nr nodes and no edges.

template<typename EdgeInputIterator>
dai::DAG::DAG ( size_t  nr,
EdgeInputIterator  begin,
EdgeInputIterator  end,
bool  check = true 
) [inline]

Constructs DAG from a range of edges.

Template Parameters:
EdgeInputIterator Iterator that iterates over instances of Edge.
Parameters:
nr The number of nodes.
begin Points to the first edge.
end Points just beyond the last edge.
check Whether to only add an edge if it does not exist already and if it does not introduce a cycle


Member Function Documentation

const Neighbor& dai::DAG::pa ( size_t  n,
size_t  _p 
) const [inline]

Returns constant reference to the _p 'th parent of node n.

Neighbor& dai::DAG::pa ( size_t  n,
size_t  _p 
) [inline]

Returns reference to the _p 'th parent of node n.

const Neighbors& dai::DAG::pa ( size_t  n  )  const [inline]

Returns constant reference to all parents of node n.

Neighbors& dai::DAG::pa ( size_t  n  )  [inline]

Returns reference to all parents of node n.

const Neighbor& dai::DAG::ch ( size_t  n,
size_t  _c 
) const [inline]

Returns constant reference to the _c 'th child of node n.

Neighbor& dai::DAG::ch ( size_t  n,
size_t  _c 
) [inline]

Returns reference to the _c 'th child of node n.

const Neighbors& dai::DAG::ch ( size_t  n  )  const [inline]

Returns constant reference to all children of node n.

Neighbors& dai::DAG::ch ( size_t  n  )  [inline]

Returns reference to all children of node n.

template<typename EdgeInputIterator>
void dai::DAG::construct ( size_t  nr,
EdgeInputIterator  begin,
EdgeInputIterator  end,
bool  check = true 
) [inline]

(Re)constructs DAG from a range of edges.

Template Parameters:
EdgeInputIterator Iterator that iterates over instances of Edge.
Parameters:
nr The number of nodes.
begin Points to the first edge.
end Points just beyond the last edge.
check Whether to only add an edge if it does not exist already and does not introduce a cycle.

size_t dai::DAG::addNode (  )  [inline]

Adds a node without parents and children and returns the index of the added node.

template<typename NodeInputIterator>
size_t dai::DAG::addNode ( NodeInputIterator  begin,
NodeInputIterator  end,
size_t  sizeHint = 0 
) [inline]

Adds a node with parents specified by a range of nodes.

Template Parameters:
NodeInputIterator Iterator that iterates over instances of size_t.
Parameters:
begin Points to the first index of the nodes that should become parents of the added node.
end Points just beyond the last index of the nodes that should become parents of the added node.
sizeHint For improved efficiency, the size of the range may be specified by sizeHint.
Returns:
Index of the added node.

DAG & dai::DAG::addEdge ( size_t  n1,
size_t  n2,
bool  check = true 
)

Adds an edge from node n1 and node n2.

If check == true, only adds the edge if it does not exist already and it would not introduce a cycle.

void dai::DAG::eraseNode ( size_t  n  ) 

Removes node n and all ingoing and outgoing edges; indices of other nodes are changed accordingly.

void dai::DAG::eraseEdge ( size_t  n1,
size_t  n2 
)

Removes edge from node n1 to node n2.

size_t dai::DAG::nrNodes (  )  const [inline]

Returns number of nodes.

size_t dai::DAG::nrEdges (  )  const [inline]

Calculates the number of edges, time complexity: O(nrNodes()).

bool dai::DAG::hasEdge ( size_t  n1,
size_t  n2 
) const [inline]

Returns true if the DAG contains an edge from node n1 and n2.

Note:
The time complexity is linear in the number of children of n1 or the number of parents of n2, whichever is smaller

size_t dai::DAG::findPa ( size_t  n,
size_t  p 
) [inline]

Returns the index of a given node p amongst the parents of n.

Note:
The time complexity is linear in the number of parents of n
Exceptions:
OBJECT_NOT_FOUND if p is not a parent of n

size_t dai::DAG::findCh ( size_t  n,
size_t  c 
) [inline]

Returns the index of a given node c amongst the children of n.

Note:
The time complexity is linear in the number of children of n
Exceptions:
OBJECT_NOT_FOUND if c is not a child n

SmallSet< size_t > dai::DAG::paSet ( size_t  n  )  const

Returns parents of node n as a SmallSet<size_t>.

SmallSet< size_t > dai::DAG::chSet ( size_t  n  )  const

Returns children of node n as a SmallSet<size_t>.

std::set< size_t > dai::DAG::ancestors ( size_t  n  )  const

Returns the set of ancestors of node n, i.e., all nodes a such that there exists a directed path from a to n (excluding n itself).

Returns the set of ancestors of node n, i.e., all nodes m such that there exists a directed path from m to n (excluding n itself).

std::set< size_t > dai::DAG::descendants ( size_t  n  )  const

Returns the set of descendants of node n, i.e., all nodes d such that there exists a directed path from n to d (excluding n itself).

Returns the set of descendants of node n, i.e., all nodes m such that there exists a directed path from n to m (excluding n itself).

bool dai::DAG::existsDirectedPath ( size_t  n1,
size_t  n2 
) const

Returns whether there exists a directed path from node n1 to node n2 (which may consist of zero edges).

bool dai::DAG::isConnected (  )  const

Returns true if the DAG is connected.

void dai::DAG::checkConsistency (  )  const

Asserts internal consistency.

bool dai::DAG::operator== ( const DAG x  )  const [inline]

Comparison operator which returns true if two DAGs are identical.

Note:
Two DAGs are called identical if they have the same number of nodes and the same edges (i.e., x has an edge from n1 to n2 if and only if *this has an edge from node n1 to n2).

void dai::DAG::printDot ( std::ostream &  os  )  const

Writes this DAG to an output stream in GraphViz .dot syntax.


Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const DAG g 
) [friend]

Writes this DAG to an output stream.


Member Data Documentation

std::vector<Neighbors> dai::DAG::_pa [private]

Contains for each node a vector of its parent nodes.

std::vector<Neighbors> dai::DAG::_ch [private]

Contains for each node a vector of its children nodes.


The documentation for this class was generated from the following files:

Generated on Thu Aug 5 19:04:04 2010 for libDAI by  doxygen 1.5.5