CMS 3D CMS Logo

TICLGraph.cc
Go to the documentation of this file.
2 #include "TICLGraph.h"
3 
4 namespace ticl {
5 
6  void Node::findSubComponents(std::vector<Node>& graph, std::vector<unsigned int>& subComponent, std::string tabs) {
7  tabs += "\t";
8  if (!alreadyVisited_) {
9  LogDebug("TICLGraph") << tabs << " Visiting node " << index_ << std::endl;
10  alreadyVisited_ = true;
11  subComponent.push_back(index_);
12  for (auto const& neighbour : outerNeighboursId_) {
13  LogDebug("TICLGraph") << tabs << " Trying to visit " << neighbour << std::endl;
14  graph[neighbour].findSubComponents(graph, subComponent, tabs);
15  }
16  }
17  }
18 } // namespace ticl
19 
20 std::vector<std::vector<unsigned int>> TICLGraph::findSubComponents() {
21  std::vector<std::vector<unsigned int>> components;
22  for (auto const& node : nodes_) {
23  auto const id = node.getId();
24  if (isRootNode_[id]) {
25  //LogDebug("TICLGraph") << "DFS Starting From " << id << std::endl;
26  std::string tabs = "\t";
27  std::vector<unsigned int> tmpSubComponents;
28  nodes_[id].findSubComponents(nodes_, tmpSubComponents, tabs);
29  components.push_back(tmpSubComponents);
30  }
31  }
32  return components;
33 }
34 
35 void TICLGraph::dfsForCC(unsigned int nodeIndex,
36  std::unordered_set<unsigned int>& visited,
37  std::vector<unsigned int>& component) const {
38  visited.insert(nodeIndex);
39  component.push_back(nodeIndex);
40 
41  for (auto const& neighbourIndex : nodes_[nodeIndex].getOuterNeighbours()) {
42  if (visited.find(neighbourIndex) == visited.end()) {
43  dfsForCC(neighbourIndex, visited, component);
44  }
45  }
46 }
47 
48 std::vector<std::vector<unsigned int>> TICLGraph::getConnectedComponents() const {
49  std::unordered_set<unsigned int> visited;
50  std::vector<std::vector<unsigned int>> components;
51 
52  for (unsigned int i = 0; i < nodes_.size(); ++i) {
53  if (visited.find(i) == visited.end()) {
54  std::vector<unsigned int> component;
55  dfsForCC(i, visited, component);
56  components.push_back(component);
57  }
58  }
59  return components;
60 }
unsigned index_
Definition: TICLGraph.h:31
std::vector< std::vector< unsigned int > > getConnectedComponents() const
Definition: TICLGraph.cc:48
std::vector< int > isRootNode_
Definition: TICLGraph.h:66
std::vector< ticl::Node > nodes_
Definition: TICLGraph.h:65
void dfsForCC(unsigned int nodeIndex, std::unordered_set< unsigned int > &visited, std::vector< unsigned int > &component) const
Definition: TICLGraph.cc:35
std::vector< unsigned int > outerNeighboursId_
Definition: TICLGraph.h:34
std::vector< std::vector< unsigned int > > findSubComponents()
Definition: TICLGraph.cc:20
Definition: Common.h:10
void findSubComponents(std::vector< Node > &graph, std::vector< unsigned int > &subComponent, std::string tabs)
Definition: TICLGraph.cc:6
bool alreadyVisited_
Definition: TICLGraph.h:36
#define LogDebug(id)