OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
Graph Class Reference

#include <Graph.hpp>

Public Member Functions

 Graph ()
 ~Graph ()
 Graph (const int &npt, const int &nconnect, const std::vector< int > &connect_list)
void build_path ()
std::vector< bool > build_cycle ()
const int & getNbConnectedComponents () const
const std::vector< std::vector< int > > & getPath () const
const int & getTotalSize () const
const std::vector< std::vector< int > > & getAdjList () const
void print () const

Private Member Functions

std::vector< int > dfs (int p0, std::vector< int > &)

Private Attributes

int m_npt
int m_nconnect
int m_nb_connected_components
int m_total_size
std::vector< std::vector< int > > m_adj_list
std::vector< std::vector< int > > m_path_diag
std::vector< std::vector< int > > m_path
std::vector< int > m_degree
std::vector< int > m_color

Detailed Description

Definition at line 37 of file Graph.hpp.

Constructor & Destructor Documentation

◆ Graph() [1/2]

Graph::Graph ( )
inline

Definition at line 40 of file Graph.hpp.

40{};

◆ ~Graph()

Graph::~Graph ( )
inline

Definition at line 42 of file Graph.hpp.

42{};

◆ Graph() [2/2]

Graph::Graph ( const int & npt,
const int & nconnect,
const std::vector< int > & connect_list )

Member Function Documentation

◆ build_cycle()

vector< bool > Graph::build_cycle ( )

Definition at line 108 of file Graph.cpp.

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}
std::vector< std::vector< int > > m_adj_list
Definition Graph.hpp:59
int m_nb_connected_components
Definition Graph.hpp:57
std::vector< std::vector< int > > m_path
Definition Graph.hpp:59
std::vector< int > m_degree
Definition Graph.hpp:60

◆ build_path()

void Graph::build_path ( )

Definition at line 74 of file Graph.cpp.

74 {
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}
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
std::vector< int > m_color
Definition Graph.hpp:61
int m_npt
Definition Graph.hpp:55
int m_total_size
Definition Graph.hpp:58

◆ dfs()

vector< int > Graph::dfs ( int p0,
std::vector< int > &  )
private

Definition at line 45 of file Graph.cpp.

45 {
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}

◆ getAdjList()

const std::vector< std::vector< int > > & Graph::getAdjList ( ) const
inline

Definition at line 52 of file Graph.hpp.

52{return m_adj_list;};

◆ getNbConnectedComponents()

const int & Graph::getNbConnectedComponents ( ) const
inline

Definition at line 49 of file Graph.hpp.

◆ getPath()

const std::vector< std::vector< int > > & Graph::getPath ( ) const
inline

Definition at line 50 of file Graph.hpp.

50{return m_path;}

◆ getTotalSize()

const int & Graph::getTotalSize ( ) const
inline

Definition at line 51 of file Graph.hpp.

51{return m_total_size;};

◆ print()

void Graph::print ( ) const

Definition at line 145 of file Graph.cpp.

145 {
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}
int m_nconnect
Definition Graph.hpp:56

Field Documentation

◆ m_adj_list

std::vector<std::vector<int> > Graph::m_adj_list
private

Definition at line 59 of file Graph.hpp.

◆ m_color

std::vector<int> Graph::m_color
private

Definition at line 61 of file Graph.hpp.

◆ m_degree

std::vector<int> Graph::m_degree
private

Definition at line 60 of file Graph.hpp.

◆ m_nb_connected_components

int Graph::m_nb_connected_components
private

Definition at line 57 of file Graph.hpp.

◆ m_nconnect

int Graph::m_nconnect
private

Definition at line 56 of file Graph.hpp.

◆ m_npt

int Graph::m_npt
private

Definition at line 55 of file Graph.hpp.

◆ m_path

std::vector<std::vector<int> > Graph::m_path
private

Definition at line 59 of file Graph.hpp.

◆ m_path_diag

std::vector<std::vector<int> > Graph::m_path_diag
private

Definition at line 59 of file Graph.hpp.

◆ m_total_size

int Graph::m_total_size
private

Definition at line 58 of file Graph.hpp.


The documentation for this class was generated from the following files: