15 using IndexType = std::size_t;
17 typedef typename std::unordered_set<IndexType> edgeListType;
19 typedef typename std::unordered_map<IndexType, edgeListType>
22 adjacencyListType adjacencyList;
23 std::vector<IndexType> componentIds;
25 void depthFirstComponentSearch(
const std::size_t ¤tVertex,
26 int ¤tComponent) {
28 componentIds[currentVertex] = currentComponent;
31 auto vertexListIt = adjacencyList.find(currentVertex);
32 if (vertexListIt == adjacencyList.end()) {
33 Logger::getInstance().addError(
34 "Graph: Vertex " + std::to_string(currentVertex) +
35 " could not be found although it should exist!");
38 for (
auto it = vertexListIt->second.begin();
39 it != vertexListIt->second.end(); ++it) {
41 if (componentIds[*it] == -1) {
42 depthFirstComponentSearch(*it, currentComponent);
50 adjacencyList.insert(std::make_pair(adjacencyList.size(), edgeListType()));
52 return adjacencyList.size() - 1;
60 adjacencyList.insert(std::make_pair(vertex1, edgeListType())).first;
61 it->second.insert(vertex2);
64 it = adjacencyList.insert(std::make_pair(vertex2, edgeListType())).first;
65 it->second.insert(vertex1);
73 componentIds.resize(adjacencyList.size(), -1);
74 int currentComponent = 0;
75 std::size_t currentVertex = 0;
77 for (
auto it = adjacencyList.begin(); it != adjacencyList.end(); ++it) {
80 if (componentIds[currentVertex] == -1) {
81 depthFirstComponentSearch(currentVertex, currentComponent);
92 std::cout <<
"Graph structure: " << std::endl;
93 for (
auto it = adjacencyList.begin(); it != adjacencyList.end(); ++it) {
94 auto &edges = (*it).second;
95 std::cout <<
"Vertex: " << (*it).first << std::endl;
96 for (
auto edgeIt = edges.begin(); edgeIt != edges.end(); ++edgeIt) {
97 std::cout << *edgeIt <<
", ";
99 std::cout << std::endl;
void insertNextEdge(std::size_t vertex1, std::size_t vertex2)
add connection of vertex1 to vertex2
Definition lsGraph.hpp:56