checkCycles.cpp
Go to the documentation of this file.
1 /* $Id: checkCycles.cpp 490 2011-01-14 16:07:12Z pbelotti $
2  *
3  * Name: checkCycles.cpp
4  * Author: Pietro Belotti
5  * Purpose: check for cycles in dependence graph
6  *
7  * (C) Carnegie-Mellon University, 2007-10.
8  * This file is licensed under the Eclipse Public License (EPL)
9  */
10 
11 //#include <stdio.h>
12 #include "CouenneDepGraph.hpp"
13 
14 //#define DEBUG
15 
16 using namespace Couenne;
17 
18 static bool visit (std::set <DepNode *, compNode>::iterator &v);
19 
21 
23 
24  for (std::set <DepNode *, compNode>::iterator
25  i = vertices_.begin ();
26  i != vertices_.end (); ++i)
27  (*i) -> color () = DepNode::DEP_WHITE;
28 
29  // simple DFS that checks for cycles
30 
31  for (std::set <DepNode *, compNode>::iterator
32  i = vertices_.begin ();
33  i != vertices_.end (); ++i)
34 
35  if (((*i) -> color () == DepNode::DEP_WHITE) &&
36  (visit (i)))
37  return true;
38 
39  return false;
40 }
41 
42 // subroutine that visits a single node
43 bool visit (std::set <DepNode *, compNode>::iterator &v) {
44 
45  //printf ("%d is gray\n", (*v) -> Index ());
46  (*v) -> color () = DepNode::DEP_GRAY;
47  std::set <DepNode *, compNode> *list = (*v) -> DepList ();
48 
49  // gen2 contains the adjacency list of this node
50 
51  for (std::set <DepNode *, compNode>::iterator
52  j = list -> begin ();
53  j != list -> end (); ++j)
54 
55  if ((*j) -> color () == DepNode::DEP_GRAY) {
56  //printf ("%d is gray\n", (*j) -> Index ());
57  return true;
58  }
59  else {
60 
61  if ((*j) -> color () == DepNode::DEP_WHITE)
62  //printf ("visiting %d\n", (*j) -> Index ());
63 
64  if (((*j) -> color () == DepNode::DEP_WHITE) && (visit (j))) {
65  //printf ("%d is true\n", (*j) -> Index ());
66  return true;
67  }
68  }
69 
70  (*v) -> color () = DepNode::DEP_BLACK;
71  //printf ("%d is black\n", (*v) -> Index ());
72  return false;
73 }
std::set< DepNode *, compNode > vertices_
set of variable nodes
bool checkCycles()
check for dependence cycles in graph
Definition: checkCycles.cpp:22
static char * j
Definition: OSdtoa.cpp:3622
fint end
static bool visit(std::set< DepNode *, compNode >::iterator &v)
Definition: checkCycles.cpp:43