/usr/include/polymake/graph/connected.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* Copyright (c) 1997-2018
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/graph_iterators.h"
#include "polymake/IncidenceMatrix.h"
#include "polymake/Graph.h"
namespace polymake { namespace graph {
template <typename Iterator, typename TGraph> inline
bool connectivity_via_BFS(const TGraph& G)
{
if (!G.nodes()) return true;
for (Iterator it(G, nodes(G).front()); !it.at_end(); ++it) {
if (it.undiscovered_nodes()==0) return true;
}
return false;
}
/// Determine whether an undirected graph is connected.
template <typename TGraph>
typename std::enable_if<!TGraph::is_directed, bool>::type
is_connected(const GenericGraph<TGraph>& G)
{
return connectivity_via_BFS<BFSiterator<TGraph>>(G.top());
}
/// Determine whether a directed graph is weakly connected.
template <typename TGraph>
typename std::enable_if<TGraph::is_directed, bool>::type
is_weakly_connected(const GenericGraph<TGraph>& G)
{
return connectivity_via_BFS<BFSiterator<TGraph, TraversalDirectionTag<int_constant<0>>>>(G.top());
}
template <typename TGraph>
class connected_components_iterator
: protected BFSiterator<TGraph, VisitorTag<NodeVisitor<true>>, TraversalDirectionTag<int_constant<!TGraph::is_directed>>> {
protected:
typedef BFSiterator<TGraph, VisitorTag<NodeVisitor<true>>, TraversalDirectionTag<int_constant<!TGraph::is_directed>>> base_t;
Set<int> component;
void fill()
{
do {
component += this->queue.front();
base_t::operator++();
} while (!base_t::at_end());
}
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() {}
explicit connected_components_iterator(const GenericGraph<TGraph>& graph_arg)
: base_t(graph_arg)
{
rewind();
}
reference operator* () const { return component; }
pointer operator-> () const { return &component; }
iterator& operator++ ()
{
component.clear();
if (this->undiscovered_nodes() != 0) {
this->process(this->visitor.get_visited_nodes().front());
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()) {
this->reset(nodes(*this->graph).front());
component.clear();
fill();
}
}
};
/// Compute the connected components of an undirected graph
template <typename TGraph> inline
typename std::enable_if<!TGraph::is_directed, IncidenceMatrix<>>::type
connected_components(const GenericGraph<TGraph>& G)
{
RestrictedIncidenceMatrix<only_cols> m(G.top().dim(), rowwise(), connected_components_iterator<TGraph>(G));
return IncidenceMatrix<>(std::move(m));
}
/// Compute the weakly connected components of a directed graph
template <typename TGraph> inline
typename std::enable_if<TGraph::is_directed, IncidenceMatrix<>>::type
weakly_connected_components(const GenericGraph<TGraph>& G)
{
RestrictedIncidenceMatrix<only_cols> m(G.top().dim(), rowwise(), connected_components_iterator<TGraph>(G));
return IncidenceMatrix<>(std::move(m));
}
/// Construct a connectivity graph of components of another graph
template <typename TGraph>
Graph<typename TGraph::dir>
component_connectivity(const GenericGraph<TGraph>& G, const IncidenceMatrix<>& C)
{
Graph<typename TGraph::dir> result(C.rows());
for (auto e=entire(edges(G)); !e.at_end(); ++e) {
for (auto comp1=entire(C.col(e.from_node())); !comp1.at_end(); ++comp1) {
for (auto comp2=entire(C.col(e.to_node())); !comp2.at_end(); ++comp2) {
if (*comp1 != *comp2)
result.add_edge(*comp1, *comp2);
}
}
}
return result;
}
} }
namespace pm {
template <typename TGraph>
struct check_iterator_feature<polymake::graph::connected_components_iterator<TGraph>, end_sensitive> : std::true_type {};
template <typename TGraph>
struct check_iterator_feature<polymake::graph::connected_components_iterator<TGraph>, rewindable> : std::true_type {};
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:
|