15 using IndexType = std::size_t;
17 typedef std::unordered_set<IndexType> edgeListType;
19 typedef std::unordered_map<IndexType, edgeListType> adjacencyListType;
21 adjacencyListType adjacencyList;
22 std::vector<IndexType> componentIds;
24 void depthFirstComponentSearch(
const std::size_t ¤tVertex,
25 int ¤tComponent) {
27 componentIds[currentVertex] = currentComponent;
30 auto vertexListIt = adjacencyList.find(currentVertex);
31 if (vertexListIt == adjacencyList.end()) {
32 Logger::getInstance().addError(
33 "Graph: Vertex " + std::to_string(currentVertex) +
34 " could not be found although it should exist!");
37 for (
unsigned long it : vertexListIt->second) {
39 if (componentIds[it] == -1) {
40 depthFirstComponentSearch(it, currentComponent);
48 adjacencyList.insert(std::make_pair(adjacencyList.size(), edgeListType()));
50 return adjacencyList.size() - 1;
58 adjacencyList.insert(std::make_pair(vertex1, edgeListType())).first;
59 it->second.insert(vertex2);
62 it = adjacencyList.insert(std::make_pair(vertex2, edgeListType())).first;
63 it->second.insert(vertex1);
71 componentIds.resize(adjacencyList.size(), -1);
72 int currentComponent = 0;
73 std::size_t currentVertex = 0;
75 for (
auto it = adjacencyList.begin(); it != adjacencyList.end(); ++it) {
78 if (componentIds[currentVertex] == -1) {
79 depthFirstComponentSearch(currentVertex, currentComponent);
90 std::cout <<
"Graph structure: " << std::endl;
91 for (
auto &[fst, snd] : adjacencyList) {
93 std::cout <<
"Vertex: " << fst << std::endl;
94 for (
const unsigned long edge : edges) {
95 std::cout << edge <<
", ";
97 std::cout << std::endl;
void insertNextEdge(std::size_t vertex1, std::size_t vertex2)
add connection of vertex1 to vertex2
Definition lsGraph.hpp:54