-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconncomp.h
More file actions
103 lines (83 loc) · 2.97 KB
/
conncomp.h
File metadata and controls
103 lines (83 loc) · 2.97 KB
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
/*
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
* Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#ifndef CONNCOMP_H
#define CONNCOMP_H
#include <Eigen/Core>
#include <Eigen/SparseCore>
#include <functional>
//! Sparse incidence matrix and connected components
namespace Graph {
using Eigen::SparseMatrix;
using Eigen::ColMajor;
using Eigen::Index;
using Eigen::Dynamic;
using Eigen::VectorXi;
using Eigen::Vector;
//! Matlab's bins = conncomp( AA, 'outputForm', 'vector')
[[nodiscard,maybe_unused]] static VectorXi
conncomp(const SparseMatrix<int, ColMajor> &AA)
{
VectorXi component; // index vector components, 0-based
Vector<bool,Dynamic> visited; // for each vertex: visited?
const Index V = AA.rows(); // number of vertices
visited.setConstant( V,false);
component.setConstant( V,-1);
int num_connected_components = 0;
std::function<void(Index,Index)> dfs = [&](const int c, const Index v){
visited(v) = true;
component(v) = c;
for ( SparseMatrix<int,ColMajor>::InnerIterator it(AA,v); it; ++it) {
if ( !visited(it.index()) ) {
dfs( c, it.row() );
}
}
};
for ( Index v=0; v<V; v++ ) {
if ( !visited(v) ) {
dfs( num_connected_components++, v );
}
}
return component;
/*static VectorXi m_comp; // index vector components, 0-based
static Vector<bool,Dynamic> m_visited; // for each vertex: visited?
struct s {
static void depthFirstSearch( const SparseMatrix<int,ColMajor> & CC, int c, Index v)
{
m_visited(v) = true;
m_comp(v) = c;
for ( SparseMatrix<int,ColMajor>::InnerIterator it(CC,v); it; ++it) {
if ( !m_visited(it.index()) ) {
depthFirstSearch( CC, c, it.row() );
}
}
}
};
const Index V = AA.rows(); // number of vertices
constexpr int INVALID_INDEX = -1;
m_visited.setConstant( V,false);
m_comp.setConstant( V,INVALID_INDEX);
int num_connected_components = 0;
for ( Index v=0; v<V; v++ ) {
if ( !m_visited(v) ) {
s::depthFirstSearch( AA, num_connected_components++, v );
}
}
return m_comp;*/
}
} // namespace Graph
#endif // CONNCOMP_H