Couenne  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CouenneDepGraph.hpp
Go to the documentation of this file.
1 /* $Id: CouenneDepGraph.hpp 549 2011-03-26 17:44:33Z pbelotti $
2  *
3  * Name: CouenneDepGraph.hpp
4  * Author: Pietro Belotti
5  * Purpose: class for manipulating dependencies between variables
6  *
7  * (C) Carnegie-Mellon University, 2007-11.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 #ifndef DEPGRAPH_HPP
12 #define DEPGRAPH_HPP
13 
14 #include <vector>
15 #include <set>
16 
17 #include "CouenneTypes.hpp"
18 #include "CouenneExpression.hpp"
19 #include "CouenneExprAux.hpp"
20 #include "CouenneProblemElem.hpp"
21 
22 namespace Couenne {
23 
25 struct compNode {
26  inline bool operator () (const DepNode *n0, const DepNode *n1) const;
27 };
28 
29 
32 
33 class DepNode {
34 
35 public:
36 
39 
40 protected:
41 
43  int index_;
44 
47  std::set <DepNode *, compNode> *depList_;
48 
50  int order_;
51 
54 
55 public:
56 
59  DepNode (int ind):
60  index_ (ind),
61  depList_ (new std::set <DepNode *, compNode>),
62  order_ (-1),
63  color_ (DEP_WHITE) {}
64 
67  {if (depList_) delete depList_;}
68 
70  inline int Index () const
71  {return index_;}
72 
74  inline int Order () const
75  {return order_;}
76 
78  inline std::set <DepNode *, compNode> *DepList () const
79  {return depList_;}
80 
82  bool depends (int xi, bool = false,
83  std::set <DepNode *, compNode> *already_visited = NULL) const;
84 
86  void createOrder (DepGraph *);
87 
89  void print (int = 0, bool descend = false) const;
90 
92  enum dep_color &color ()
93  {return color_;}
94 
97  std::set <DepNode *, compNode> *depList()
98  {return depList_;}
99 
102  void replaceIndex (DepNode *oldVarNode, DepNode *newVarNode);
103 };
104 
105 
107 
108 bool compNode::operator () (const DepNode *n0, const DepNode *n1) const
109 {return (n0 -> Index () < n1 -> Index ());}
110 
111 
114 
115 class DepGraph {
116 
117 protected:
118 
120  std::set <DepNode *, compNode> vertices_;
121 
123  int counter_;
124 
125 public:
126 
128  DepGraph (): counter_ (0) {}
129 
132  for (std::set <DepNode *, compNode>::iterator i = vertices_.begin ();
133  i != vertices_.end (); ++i)
134  delete (*i);
135  }
136 
138  std::set <DepNode *, compNode> &Vertices ()
139  {return vertices_;}
140 
142  inline int &Counter ()
143  {return counter_;}
144 
146  void insert (exprVar *);
147 
149  void insert (exprAux *);
150 
152  void erase (exprVar *);
153 
155  bool depends (int, int, bool = false);
156 
158  void createOrder ();
159 
161  void print (bool descend = false);
162 
164  DepNode *lookup (int index);
165 
167  bool checkCycles ();
168 
172  void replaceIndex (int oldVar, int newVar);
173 };
174 
175 }
176 
177 #endif
int counter_
counter to assign numbering to all nodes
std::set< DepNode *, compNode > vertices_
set of variable nodes
enum dep_color color_
color used in DFS for checking cycles
int & Counter()
node index counter
void createOrder(DepGraph *)
assign numbering to all nodes of graph
bool depends(int xi, bool=false, std::set< DepNode *, compNode > *already_visited=NULL) const
does this variable depend on variable with index xi?
dep_color
color used in DFS for checking cycles
int order_
order in which this variable should be updated, evaluated, etc.
enum dep_color & color()
return or set color of a node
vertex of a dependence graph.
void erase(exprVar *)
delete element
void insert(exprVar *)
insert new variable if new
std::set< DepNode *, compNode > * DepList() const
return all variables it depends on
structure for comparing nodes in the dependence graph
std::set< DepNode *, compNode > * depList()
index nodes on which this one depends (forward star in dependence graph)
~DepGraph()
destructor
~DepNode()
destructor
void print(int=0, bool descend=false) const
debugging procedure
Dependence graph.
Auxiliary variable.
void replaceIndex(DepNode *oldVarNode, DepNode *newVarNode)
replace the index of a variable with another in the entire graph.
variable-type operator
DepGraph()
constructor
std::set< DepNode *, compNode > & Vertices()
return vertex set
bool depends(int, int, bool=false)
does w depend on x?
void replaceIndex(int oldVar, int newVar)
replace, throughout the whole graph, the index of a variable with another in the entire graph...
int index_
index of variable associated with node
bool checkCycles()
check for dependence cycles in graph
int Index() const
return index of this variable
bool operator()(const DepNode *n0, const DepNode *n1) const
structure for comparing nodes
void print(bool descend=false)
debugging procedure
int Order() const
return index of this variable
void createOrder()
assign numbering to all nodes of graph
DepNode * lookup(int index)
search for node in vertex set
std::set< DepNode *, compNode > * depList_
index nodes on which this one depends (forward star in dependence graph)
DepNode(int ind)
fictitious constructor: only fill in index (such object is used in find() and then discarded) ...