File: connected.h

package info (click to toggle)
polymake 3.0r2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,752 kB
  • ctags: 30,928
  • sloc: cpp: 151,785; perl: 32,510; ansic: 3,597; java: 2,654; python: 278; makefile: 181; xml: 103; sh: 79
file content (176 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download
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
/* Copyright (c) 1997-2015
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.polymake.org

   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, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   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.
--------------------------------------------------------------------------------
*/

#ifndef POLYMAKE_GRAPH_CONNECTED_H
#define POLYMAKE_GRAPH_CONNECTED_H

#include "polymake/graph/BFSiterator.h"
#include "polymake/PowerSet.h"

namespace polymake { namespace graph {

/// Determine whether a graph is connected.
template <typename Graph>
bool is_connected(const GenericGraph<Graph>& G)
{
   if (!G.nodes()) return true;
   BFSiterator<Graph> it(G.top(), nodes(G).front());
   while (!it.at_end()) {
      if (it.unvisited_nodes()==0) return true;
      ++it;
   }
   return false;
}

template <typename Graph>
class connected_components_iterator : protected BFSiterator< Graph, Visitor< BoolNodeVisitor<true> > > {
protected:
   typedef BFSiterator< Graph, Visitor< BoolNodeVisitor<true> > > BFS;
   Set<int> component;

   void fill()
   {
      do {
         component+=this->queue.front();
         BFS::operator++();
      } while (!BFS::at_end());
   }

   void next()
   {
      const int n=this->visitor.get_visited_nodes().front();
      this->queue.push_back(n);
      this->visitor.add(n);
      --this->unvisited;
   }

public:
   typedef std::forward_iterator_tag iterator_category;
   typedef Set<int> value_type;
   typedef value_type reference;
   typedef const value_type* pointer;
   typedef ptrdiff_t difference_type;

   typedef connected_components_iterator iterator;
   typedef iterator const_iterator;

   connected_components_iterator() {}
   connected_components_iterator(const Graph& graph_arg)
      : BFS(graph_arg, graph_arg.nodes() ? nodes(graph_arg).front() : -1)
   {
      if (this->unvisited>=0) fill();
   }

   reference operator* () const { return component; }
   pointer operator-> () const { return &component; }

   iterator& operator++ ()
   {
      component.clear();
      if (BFS::unvisited>0) { next(); fill(); }
      return *this;
   }
   const iterator operator++ (int) { iterator copy(*this);  operator++();  return copy; }

   bool operator== (const iterator& it) const { return component==it.component; }
   bool operator!= (const iterator& it) const { return !operator==(it); }

   bool at_end() const { return component.empty(); }

   void rewind()
   {
      if (this->graph->nodes()) {
         BFS::reset(nodes(*this->graph).front());
         component.clear();
         fill();
      }
   }
};

/// Compute the connected components
template <typename Graph> inline
pm::GraphComponents<const Graph&, connected_components_iterator>
connected_components(const GenericGraph<Graph>& G) { return G.top(); }

namespace {

template<typename Container, typename E>
void add_to(Container& C, const E& e) 
{
   C.push_back(e);
}

template<typename E>
void add_to(Set<E>& C, const E& e)
{
   C += e;
}

} // end anonymous namespace

// compute a spanning tree rooted at vertex root_node,
// optionally restricted to a support set
template<typename GraphType, typename DirType, typename Container>
void
connected_component(const GenericGraph<GraphType, DirType>& G, 
                    Container& C,
                    int root_node=0,
                    const Set<int> support=Set<int>())
{
   std::list<int> unprocessed_leaves;
   Bitset marked;  // nodes already included in the tree

   unprocessed_leaves.push_back(root_node); // we start the tree at the given node
   add_to(C, root_node);
   marked.insert(root_node);

   while ( !unprocessed_leaves.empty() ) {      
      const int current = unprocessed_leaves.front();
      unprocessed_leaves.pop_front();
      const Set<int> neighbours = G.top().adjacent_nodes(current);  
      for (Entire< Set< int > >::const_iterator v = entire(neighbours); !v.at_end(); ++v) 
         if ((!support.size() || support.contains(*v)) 
             && !marked.contains(*v)) {
            unprocessed_leaves.push_back(*v);
            marked.insert(*v);
            add_to(C, *v);
         }
   }
}


} }

namespace pm {

template <typename Graph>
struct check_iterator_feature< polymake::graph::connected_components_iterator<Graph>, end_sensitive > : True {};

template <typename Graph>
struct check_iterator_feature< polymake::graph::connected_components_iterator<Graph>, rewindable > : True {};

template <typename GraphRef>
class generic_of_GraphComponents<GraphRef, polymake::graph::connected_components_iterator>
   : public GenericSet< GraphComponents<GraphRef,polymake::graph::connected_components_iterator>, Set<int>, operations::cmp > {};
}

#endif // POLYMAKE_GRAPH_CONNECTED_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: