OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
Graph.cpp
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23#include "Graph.hpp"
24using namespace std;
25Graph::Graph(const int& npt, const int& nconnect, const vector<int>& connect_list) :
26 m_adj_list(vector<vector<int>> (npt, vector<int>())), m_degree(npt), m_npt(npt), m_nconnect(nconnect),
27 m_nb_connected_components(0), m_total_size(0)
28{
29 for (int i(0) ; i < m_nconnect ; ++i) {
30 int p1 = connect_list.at(2 * i);
31 int p2 = connect_list.at(2 * i + 1);
32 m_adj_list.at(p1).push_back(p2);
33 m_adj_list.at(p2).push_back(p1);
34 }
35 for (int i(0) ; i < npt ; ++i) {
36 std::sort(m_adj_list.at(i).begin(), m_adj_list.at(i).end());
37 vector<int>::iterator iter = std::unique(m_adj_list.at(i).begin(), m_adj_list.at(i).end());
38 m_adj_list.at(i).erase(iter, m_adj_list.at(i).end());
39 m_degree.at(i) = m_adj_list.at(i).size();
40 }
41 // Number of connected components and paths through each connected components
42}
43
44// DFS
45vector<int> Graph::dfs(int p0, vector<int>& path) {
46 vector<int> res(m_npt, -2);
47 // color the points 0: white, 1: gray, 2: black
48 m_color.resize(m_npt, 0);
49 vector<int> p;
50 p.push_back(p0);
51 path.push_back(p0);
52 m_color.at(p0) = 1;
53 res.at(p0) = -1;
54 bool ok = true;
55 while (ok) {
56 int si = *(p.end()-1);
57 vector<int>::const_iterator iter =
58 find_if(m_adj_list.at(si).begin(), m_adj_list.at(si).end(), [this](const int& ii) {return (m_color.at(ii) == 0);});
59 if (iter != m_adj_list.at(si).end()) {
60 int sj = *iter;
61 p.push_back(sj);
62 m_color.at(sj) = 1;
63 path.push_back(sj);
64 res.at(sj) = si;
65 } else {
66 m_color.at(si) = 2;
67 p.pop_back();
68 }
69 ok = !(p.empty());
70 }
71 return res;
72}
73
76 bool ok = true;
77 int s0 = 0;
78 while (ok) {
80 vector<int> path;
81 path.reserve(m_npt);
82 m_path_diag.push_back(dfs(s0, path));
83 m_path.push_back(path);
84 vector<int>::iterator iter;
85 ok = (iter = find(m_color.begin(), m_color.end(), 0)) != m_color.end();
86 if (ok) {
87 s0 = distance(m_color.begin(), iter);
88 }
89 }
90
91 m_total_size = 0;
92 for (int i_connect(0) ; i_connect < m_nb_connected_components ; ++i_connect) {
93 // find seed
94 vector<int>::iterator iter(find(m_path_diag.at(i_connect).begin(), m_path_diag.at(i_connect).end(), -1));
95 if (iter != m_path_diag.at(i_connect).end()) {
96 int s0 = distance(m_path_diag.at(i_connect).begin(), iter);
97 vector<vector<int>> inver(m_npt, vector<int>());
98 for (int ipt(0) ; ipt < m_npt ; ipt++) {
99 if (m_path_diag.at(i_connect).at(ipt) >= 0) {
100 inver.at(m_path_diag.at(i_connect).at(ipt)).push_back(ipt);
101 }
102 }
103 }
104 m_total_size += m_path.at(i_connect).size();
105 }
106}
107
108vector<bool> Graph::build_cycle()
109{
110 vector<bool> res(m_nb_connected_components, false);
111 for (int iconnect(0) ; iconnect < m_nb_connected_components ; ++iconnect) {
112 vector<int> degree;
113 for (int i(0) ; i < m_path.at(iconnect).size() ; ++i) {
114 degree.push_back(m_degree.at(m_path.at(iconnect).at(i)));
115 }
116 vector<int>::iterator max_it, min_it;
117 max_it = max_element(degree.begin(), degree.end());
118 min_it = min_element(degree.begin(), degree.end());
119 if (*max_it == 2 && *min_it == 2) {
120 vector<int> path_new;
121 int s0 = m_path.at(iconnect).at(0);
122 int sinit = s0;
123 int s1, s2;
124 s1 = m_adj_list.at(s0).at(0);
125 bool ok = true;
126 while (ok) {
127 path_new.push_back(s0);
128 if ((s2 = m_adj_list.at(s1).at(0)) != s0) {
129 } else {
130 s2 = m_adj_list.at(s1).at(1);
131 }
132 ok = (path_new.size() != m_path.at(iconnect).size());
133 if (!ok && s1 == sinit) {
134 res.at(iconnect) = true;
135 }
136 s0 = s1;
137 s1 = s2;
138 }
139 m_path.at(iconnect) = path_new;
140 }
141 }
142 return res;
143}
144
145void Graph::print() const {
146 cout << "Number of points: " << m_npt << endl;
147 cout << "Number of edges: " << m_nconnect << endl;
148 cout << "Number of connected components: " << m_nb_connected_components << endl;
149 for (int i(0) ; i < m_nb_connected_components ; ++i) {
150 cout << "Component " << i << endl;
151 cout << "\t";
152 for (int j(0) ; j < m_path.at(i).size() ; j++) {
153 cout << m_path.at(i).at(j) << " " ;
154 }
155 cout << endl;
156 }
157}
std::vector< int > dfs(int p0, std::vector< int > &)
Definition Graph.cpp:45
std::vector< std::vector< int > > m_path_diag
Definition Graph.hpp:59
void build_path()
Definition Graph.cpp:74
std::vector< std::vector< int > > m_adj_list
Definition Graph.hpp:59
int m_nb_connected_components
Definition Graph.hpp:57
std::vector< int > m_color
Definition Graph.hpp:61
int m_nconnect
Definition Graph.hpp:56
std::vector< std::vector< int > > m_path
Definition Graph.hpp:59
int m_npt
Definition Graph.hpp:55
Graph()
Definition Graph.hpp:40
std::vector< int > m_degree
Definition Graph.hpp:60
void print() const
Definition Graph.cpp:145
std::vector< bool > build_cycle()
Definition Graph.cpp:108
int m_total_size
Definition Graph.hpp:58