File: graph_flags.cpp

package info (click to toggle)
gamera 1%3A3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 15,912 kB
  • sloc: xml: 122,324; cpp: 50,730; python: 35,044; ansic: 258; makefile: 114; sh: 101
file content (405 lines) | stat: -rw-r--r-- 11,131 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 *
 * Copyright (C) 2011 Christian Brandt
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "graph/graph.hpp"
#include "graph/edge.hpp"
#include "graph/node.hpp"

namespace Gamera { namespace GraphApi {

   
   
// -----------------------------------------------------------------------------
bool Graph::is_directed() {
   return GRAPH_HAS_FLAG(this, FLAG_DIRECTED); 
}



// -----------------------------------------------------------------------------
/// sets each edge to directed and adds the same in other direction
void Graph::make_directed() {
   EdgePtrIterator* it = get_edges();
   Edge* e;
   GRAPH_SET_FLAG(this, FLAG_DIRECTED);
   EdgeVector edges;
   while((e = it->next()) != NULL) {
      e->is_directed = true;
      edges.push_back(e);
   }

   delete it;
   for(EdgeIterator it = edges.begin(); it != edges.end(); it++) {
      Edge* e = *it;
      add_edge(e->to_node, e->from_node, e->weight, true, e->label);
   }
}



// -----------------------------------------------------------------------------
/// This is a small helper-struct for remembering edges in make_undirected
struct smallEdge {
   Node *_to, *_from;
   smallEdge(Node* from, Node* to) {
      _to = to; _from = from;
   }
};



// -----------------------------------------------------------------------------
/// sets each edge as undirected and tries to remove edges in other direction
void Graph::make_undirected() {
   if(is_undirected())
      return;
   std::vector<smallEdge*> remove;
   {
      EdgePtrIterator *it = get_edges();
      Edge* e;
      while((e = it->next()) != NULL) {
         Node* from = e->from_node;
         Node* to = e->to_node;
         e->is_directed = false;
         if(has_edge(to,from)) {
            remove.push_back(new smallEdge(to, from));      
         }
      }

      delete it;
   }
   for(std::vector<smallEdge*>::iterator it = remove.begin(); 
         it != remove.end(); it++) {
      try {
         remove_edge((*it)->_from, (*it)->_to);
      }
      catch (std::runtime_error) {
         std::cerr << "somthing went wrong" << std::endl;
      }
      delete *it;
   }
   GRAPH_UNSET_FLAG(this, FLAG_DIRECTED);
}



// -----------------------------------------------------------------------------
bool Graph::is_cyclic() {
   if(get_nedges() == 0) //trivial case
      return false;
   else if(get_nnodes() == 1) //another trivial case
      return true;

   bool cyclic = false;
   if(is_directed()) { //similar algorithm to make_acyclic
      NodeStack node_stack;
      std::set<Node*> visited;
      if (get_nedges() != 0) {
         NodePtrIterator *i = get_nodes();
         Node* n;
         while((n = i->next()) != NULL && !cyclic) {
            if (visited.count(n) == 0) {
               node_stack.push(n);
               while (!node_stack.empty() && !cyclic) {
                  Node* node = node_stack.top();
                  node_stack.pop();
                  visited.insert(node);

                  EdgePtrIterator *it = node->get_edges();
                  Edge* e;
                  while ((e = it->next()) != NULL && !cyclic) {
                     Node* inner_node = e->traverse(node);
                     if(inner_node) {
                        if(visited.count(inner_node) != 0) {
                           cyclic = true;
                        }
                        else  {
                           node_stack.push(inner_node);
                           visited.insert(inner_node);
                        }
                     }
                  }

                  delete it;
               }
            }
         }

         delete i;
      }
   }  
   else {
      NodeVector* roots = NULL;
      roots = get_subgraph_roots();

      for(NodeVector::iterator rit = roots->begin(); 
         rit != roots->end() && !cyclic; rit++) {
         //tests for cycles in each subgraph
         DfsIterator *it = DFS(*rit);
         while(it->next() != NULL)
            ;
         cyclic = cyclic || it->has_cycles();
         delete it;
      }
      delete roots;
   }
   return cyclic;
}



// -----------------------------------------------------------------------------
// only sets flag to allow cycles
void Graph::make_cyclic() {
   GRAPH_SET_FLAG(this, FLAG_CYCLIC);
}



// -----------------------------------------------------------------------------
/** make_acyclic removes any cycles from the graph. 
 * Another probably better way for doing this could be spanning_tree 
 *
 * This implementation works only with directed graphs. As a solution the
 * graph is converted to a directed graph and back to an undirected after 
 * running the algorithm.
 * */
void Graph::make_acyclic() {
   typedef std::set<Edge*> EdgeSet;
   EdgeSet edges_to_remove;
   NodeStack node_stack;
   std::set<Node*> visited;  
   bool undirected = is_undirected();
   
   if(undirected)
      make_directed();

   if (get_nedges() != 0) {
      NodePtrIterator *i = get_nodes();
      Node* n;
      while((n = i->next()) != NULL) {
         if (visited.count(n) == 0) {
            if (node_stack.size())
               throw std::runtime_error("Error in graph_make_acyclic. "
                     "This error should never be raised.  Please report "
                     "it to the author.");
            node_stack.push(n);
            while (!node_stack.empty()) {
               Node* node = node_stack.top();
               node_stack.pop();
               visited.insert(node);

               EdgePtrIterator *it = node->get_edges();
               Edge* e;
               while ((e = it->next()) != NULL) {
                  Node* inner_node = e->traverse(node);
                  if(inner_node) {
                     if(visited.count(inner_node) != 0) {
                        edges_to_remove.insert(e);
                     }
                     else  {
                        node_stack.push(inner_node);
                        visited.insert(inner_node);
                     }
                  }
               }

               delete it;
            }
         }
      }

      for(EdgeSet::iterator it = edges_to_remove.begin(); 
            it != edges_to_remove.end(); it++) {
         remove_edge(*it); 
      }

      delete i;
   }
   if(undirected)
      make_undirected();

   GRAPH_UNSET_FLAG(this, FLAG_CYCLIC);
}

  

// -----------------------------------------------------------------------------
bool Graph::is_tree() {
   return !is_cyclic() && !is_directed();
}



// -----------------------------------------------------------------------------
// only sets flag
void Graph::make_multi_connected() {
   GRAPH_SET_FLAG(this, FLAG_MULTI_CONNECTED);
}



// -----------------------------------------------------------------------------
// unsets flag and removes multiple edges
void Graph::make_singly_connected() {
   typedef std::pair<Node*, Node*> edgepair;
   EdgeVector to_remove;
   std::set<edgepair > edgeset;
   EdgePtrIterator* it = get_edges();
   Edge* e;
   if(is_directed()) {
      while((e = it->next()) != NULL) {
         edgepair ep(e->from_node, e->to_node);
         if(edgeset.count(ep) > 0)
            to_remove.push_back(e);
         else
            edgeset.insert(ep);
      }
   }
   else {
      while((e = it->next()) != NULL) {  
         edgepair ep(std::min(e->from_node, e->to_node), 
                     std::max(e->from_node, e->to_node));
         if(edgeset.count(ep) > 0)
            to_remove.push_back(e);
         else
            edgeset.insert(ep);
      }
   }

   delete it;
   for(EdgeIterator it = to_remove.begin(); it != to_remove.end(); it++) {
      remove_edge(*it);
   }

   GRAPH_UNSET_FLAG(this, FLAG_MULTI_CONNECTED);
}



// -----------------------------------------------------------------------------
// only sets flag
void Graph::make_self_connected() {
   GRAPH_SET_FLAG(this, FLAG_SELF_CONNECTED);
}



// -----------------------------------------------------------------------------
//unsets flag and remove self-connections
void Graph::make_not_self_connected() {
   std::vector<smallEdge*> remove;
   {
      EdgePtrIterator *it = get_edges();
      Edge* e;
      while((e = it->next()) != NULL) {
         Node* from = e->from_node;
         Node* to = e->to_node;
         if(to == from) {
            remove.push_back(new smallEdge(to, from));      
         }
      }
      delete it;
   }
   for(std::vector<smallEdge*>::iterator it = remove.begin(); 
         it != remove.end(); it++) {
      remove_edge((*it)->_from, (*it)->_to);
      delete *it;
   }

   GRAPH_UNSET_FLAG(this, FLAG_SELF_CONNECTED);
}



// -----------------------------------------------------------------------------
bool Graph::conforms_restrictions() {
   if(!GRAPH_HAS_FLAG(this, FLAG_CYCLIC) && is_cyclic())
      return false;
   if(!GRAPH_HAS_FLAG(this, FLAG_MULTI_CONNECTED) && is_multi_connected())
      return false;
   if(!GRAPH_HAS_FLAG(this, FLAG_SELF_CONNECTED) && is_self_connected()) {
      return false;
   }

   return true;
}



// -----------------------------------------------------------------------------
bool Graph::is_multi_connected() {
   std::set<std::pair<Node*, Node*> > edgeset;
   EdgePtrIterator* it = get_edges();
   Edge* e;
   if(is_directed()) {
      while((e = it->next()) != NULL) {
         edgeset.insert(std::pair<Node*,Node*>(e->from_node, e->to_node));
      }
   }
   else {
      while((e = it->next()) != NULL) {
         edgeset.insert(std::pair<Node*,Node*>(
                  std::min(e->from_node, e->to_node), 
                  std::max(e->from_node, e->to_node)
                  ));
      }
   }

   delete it;
   return edgeset.size() != get_nedges();
}



// -----------------------------------------------------------------------------
bool Graph::is_self_connected() {
   bool selfconnections = false;
   EdgePtrIterator* it = get_edges();
   Edge* e;
   while((e = it->next()) != NULL && !selfconnections) {
      if( *e->from_node->_value == *e->to_node->_value) {
         selfconnections = true;
      }
   }

   delete it;
   return selfconnections;
}



// -----------------------------------------------------------------------------
void Graph::make_tree() {
   GRAPH_SET_FLAG(this, FLAG_TREE);
   make_undirected();
   make_acyclic();
}



// -----------------------------------------------------------------------------
void Graph::make_blob() {
   GRAPH_UNSET_FLAG(this, FLAG_TREE);
}



}} // end Gamera::GraphApi