2023-08-28 12:31:18 +02:00
|
|
|
/* A directed graph.
|
2018-08-21 00:25:44 +02:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Daniel Krebs <github@daniel-krebs.net>
|
2022-03-15 09:05:42 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-05-19 17:40:10 +02:00
|
|
|
* @license Apache License 2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <villas/log.hpp>
|
|
|
|
#include <villas/graph/vertex.hpp>
|
|
|
|
#include <villas/graph/edge.hpp>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace graph {
|
|
|
|
|
|
|
|
template<typename VertexType = Vertex, typename EdgeType = Edge>
|
|
|
|
class DirectedGraph {
|
|
|
|
public:
|
|
|
|
|
|
|
|
using VertexIdentifier = Vertex::Identifier;
|
|
|
|
using EdgeIdentifier = Edge::Identifier;
|
|
|
|
using Path = std::list<EdgeIdentifier>;
|
|
|
|
|
2020-06-14 21:53:18 +02:00
|
|
|
DirectedGraph(const std::string &name = "DirectedGraph") :
|
2021-03-17 11:38:00 -04:00
|
|
|
lastVertexId(0),
|
|
|
|
lastEdgeId(0),
|
|
|
|
logger(logging.get(name))
|
|
|
|
{ }
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
std::shared_ptr<VertexType> getVertex(VertexIdentifier vertexId) const
|
|
|
|
{
|
2022-12-02 17:16:44 +01:00
|
|
|
// Cannot use [] operator, because creates non-existing elements
|
2018-08-21 00:25:44 +02:00
|
|
|
// at() will throw std::out_of_range if element does not exist
|
|
|
|
return vertices.at(vertexId);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class UnaryPredicate>
|
|
|
|
VertexIdentifier findVertex(UnaryPredicate p)
|
|
|
|
{
|
2020-06-14 21:53:18 +02:00
|
|
|
for (auto &v : vertices) {
|
|
|
|
auto &vertexId = v.first;
|
|
|
|
auto &vertex = v.second;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2020-06-11 18:33:34 +02:00
|
|
|
if (p(vertex)) {
|
2018-08-21 00:25:44 +02:00
|
|
|
return vertexId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::out_of_range("vertex not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<EdgeType> getEdge(EdgeIdentifier edgeId) const
|
|
|
|
{
|
2020-06-11 18:33:34 +02:00
|
|
|
if (edgeId >= lastEdgeId)
|
2018-08-21 00:25:44 +02:00
|
|
|
throw std::invalid_argument("edge doesn't exist");
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Cannot use [] operator, because creates non-existing elements
|
2018-08-21 00:25:44 +02:00
|
|
|
// at() will throw std::out_of_range if element does not exist
|
|
|
|
return edges.at(edgeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t getEdgeCount() const
|
2022-12-02 17:16:44 +01:00
|
|
|
{
|
|
|
|
return edges.size();
|
|
|
|
}
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
std::size_t getVertexCount() const
|
2022-12-02 17:16:44 +01:00
|
|
|
{
|
|
|
|
return vertices.size();
|
|
|
|
}
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
VertexIdentifier addVertex(std::shared_ptr<VertexType> vertex)
|
|
|
|
{
|
|
|
|
vertex->id = lastVertexId++;
|
|
|
|
|
2023-01-10 15:15:20 +01:00
|
|
|
logger->debug("New vertex: {}", vertex->toString());
|
2018-08-21 00:25:44 +02:00
|
|
|
vertices[vertex->id] = vertex;
|
|
|
|
|
|
|
|
return vertex->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
EdgeIdentifier addEdge(std::shared_ptr<EdgeType> edge,
|
|
|
|
VertexIdentifier fromVertexId,
|
|
|
|
VertexIdentifier toVertexId)
|
|
|
|
{
|
2022-12-02 17:16:44 +01:00
|
|
|
// Allocate edge id
|
2018-08-21 00:25:44 +02:00
|
|
|
edge->id = lastEdgeId++;
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Connect it
|
2018-08-21 00:25:44 +02:00
|
|
|
edge->from = fromVertexId;
|
|
|
|
edge->to = toVertexId;
|
|
|
|
|
2023-01-10 15:15:20 +01:00
|
|
|
logger->debug("New edge {}: {} -> {}", edge->toString(), edge->from, edge->to);
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// This is a directed graph, so only push edge to starting vertex
|
2018-08-21 00:25:44 +02:00
|
|
|
getVertex(edge->from)->edges.push_back(edge->id);
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Add new edge to graph
|
2018-08-21 00:25:44 +02:00
|
|
|
edges[edge->id] = edge;
|
|
|
|
|
|
|
|
return edge->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
EdgeIdentifier addDefaultEdge(VertexIdentifier fromVertexId,
|
|
|
|
VertexIdentifier toVertexId)
|
|
|
|
{
|
2022-12-02 17:16:44 +01:00
|
|
|
// Create a new edge
|
2018-08-21 00:25:44 +02:00
|
|
|
std::shared_ptr<EdgeType> edge(new EdgeType);
|
|
|
|
|
|
|
|
return addEdge(edge, fromVertexId, toVertexId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeEdge(EdgeIdentifier edgeId)
|
|
|
|
{
|
|
|
|
auto edge = getEdge(edgeId);
|
|
|
|
auto startVertex = getVertex(edge->from);
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Remove edge only from starting vertex (this is a directed graph)
|
2018-08-21 00:25:44 +02:00
|
|
|
logger->debug("Remove edge {} from vertex {}", edgeId, edge->from);
|
|
|
|
startVertex->edges.remove(edgeId);
|
|
|
|
|
|
|
|
logger->debug("Remove edge {}", edgeId);
|
|
|
|
edges.erase(edgeId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeVertex(VertexIdentifier vertexId)
|
|
|
|
{
|
2022-12-02 17:16:44 +01:00
|
|
|
// Delete every edge that start or ends at this vertex
|
2018-08-21 00:25:44 +02:00
|
|
|
auto it = edges.begin();
|
2020-06-11 18:33:34 +02:00
|
|
|
while (it != edges.end()) {
|
2020-06-14 21:53:18 +02:00
|
|
|
auto &edgeId = it->first;
|
|
|
|
auto &edge = it->second;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
bool removeEdge = false;
|
|
|
|
|
2020-06-11 18:33:34 +02:00
|
|
|
if (edge->to == vertexId) {
|
2018-08-21 00:25:44 +02:00
|
|
|
logger->debug("Remove edge {} from vertex {}'s edge list",
|
|
|
|
edgeId, edge->from);
|
|
|
|
|
|
|
|
removeEdge = true;
|
|
|
|
|
|
|
|
auto startVertex = getVertex(edge->from);
|
|
|
|
startVertex->edges.remove(edge->id);
|
2022-12-02 13:27:31 +01:00
|
|
|
|
2018-08-21 00:25:44 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 18:33:34 +02:00
|
|
|
if ((edge->from == vertexId) or removeEdge) {
|
2018-08-21 00:25:44 +02:00
|
|
|
logger->debug("Remove edge {}", edgeId);
|
2022-12-02 17:16:44 +01:00
|
|
|
// Remove edge from global edge list
|
2018-08-21 00:25:44 +02:00
|
|
|
it = edges.erase(it);
|
|
|
|
}
|
2020-06-15 21:05:51 +02:00
|
|
|
else
|
|
|
|
++it;
|
2018-08-21 00:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger->debug("Remove vertex {}", vertexId);
|
|
|
|
vertices.erase(vertexId);
|
2022-12-02 13:27:31 +01:00
|
|
|
lastVertexId--;
|
2018-08-21 00:25:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
const std::list<EdgeIdentifier>& vertexGetEdges(VertexIdentifier vertexId) const
|
|
|
|
{
|
|
|
|
return getVertex(vertexId)->edges;
|
|
|
|
}
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
using check_path_fn = std::function<bool(const Path&)>;
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
static
|
|
|
|
bool checkPath(const Path&)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-21 00:25:44 +02:00
|
|
|
|
|
|
|
bool getPath(VertexIdentifier fromVertexId,
|
|
|
|
VertexIdentifier toVertexId,
|
2020-06-14 21:53:18 +02:00
|
|
|
Path &path,
|
2018-08-21 00:25:44 +02:00
|
|
|
check_path_fn pathCheckFunc = checkPath)
|
|
|
|
{
|
2020-06-15 21:05:51 +02:00
|
|
|
if (fromVertexId == toVertexId)
|
2022-12-02 17:16:44 +01:00
|
|
|
// Arrived at the destination
|
2018-08-21 00:25:44 +02:00
|
|
|
return true;
|
2020-06-15 21:05:51 +02:00
|
|
|
else {
|
2018-08-21 00:25:44 +02:00
|
|
|
auto fromVertex = getVertex(fromVertexId);
|
|
|
|
|
2020-06-14 21:53:18 +02:00
|
|
|
for (auto &edgeId : fromVertex->edges) {
|
2018-08-21 00:25:44 +02:00
|
|
|
auto edgeOfFromVertex = getEdge(edgeId);
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Loop detection
|
2018-08-21 00:25:44 +02:00
|
|
|
bool loop = false;
|
2020-06-14 21:53:18 +02:00
|
|
|
for (auto &edgeIdInPath : path) {
|
2018-08-21 00:25:44 +02:00
|
|
|
auto edgeInPath = getEdge(edgeIdInPath);
|
2020-06-11 18:33:34 +02:00
|
|
|
if (edgeInPath->from == edgeOfFromVertex->to) {
|
2018-08-21 00:25:44 +02:00
|
|
|
loop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:33:34 +02:00
|
|
|
if (loop) {
|
2018-08-21 00:25:44 +02:00
|
|
|
logger->debug("Loop detected via edge {}", edgeId);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Remember the path we're investigating to detect loops
|
2018-08-21 00:25:44 +02:00
|
|
|
path.push_back(edgeId);
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Recursive, depth-first search
|
2020-06-15 21:05:51 +02:00
|
|
|
if (getPath(edgeOfFromVertex->to, toVertexId, path, pathCheckFunc) and pathCheckFunc(path))
|
2022-12-02 17:16:44 +01:00
|
|
|
// Path found, we're done
|
2020-06-15 21:05:51 +02:00
|
|
|
return true;
|
|
|
|
else
|
2022-12-02 17:16:44 +01:00
|
|
|
// Tear down path that didn't lead to the destination
|
2018-08-21 00:25:44 +02:00
|
|
|
path.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-14 21:53:18 +02:00
|
|
|
void dump(const std::string &fileName = "") const
|
2018-08-21 00:25:44 +02:00
|
|
|
{
|
|
|
|
logger->info("Vertices:");
|
2020-06-14 21:53:18 +02:00
|
|
|
for (auto &v : vertices) {
|
|
|
|
auto &vertex = v.second;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
// Format connected vertices into a list
|
2018-08-21 00:25:44 +02:00
|
|
|
std::stringstream ssEdges;
|
2020-06-14 21:53:18 +02:00
|
|
|
for (auto &edge : vertex->edges) {
|
2018-08-21 00:25:44 +02:00
|
|
|
ssEdges << getEdge(edge)->to << " ";
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:15:20 +01:00
|
|
|
logger->info(" {} connected to: {}", vertex->toString(), ssEdges.str());
|
2018-08-21 00:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::fstream s(fileName, s.out | s.trunc);
|
2020-06-11 18:33:34 +02:00
|
|
|
if (s.is_open()) {
|
2018-08-21 00:25:44 +02:00
|
|
|
s << "digraph memgraph {" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger->info("Edges:");
|
2020-06-14 21:53:18 +02:00
|
|
|
for (auto &e : edges) {
|
|
|
|
auto &edge = e.second;
|
2018-08-21 00:25:44 +02:00
|
|
|
|
2023-01-10 15:15:20 +01:00
|
|
|
logger->info(" {}: {} -> {}", edge->toString(), edge->from, edge->to);
|
2020-06-11 18:33:34 +02:00
|
|
|
if (s.is_open()) {
|
2018-08-21 00:25:44 +02:00
|
|
|
auto from = getVertex(edge->from);
|
|
|
|
auto to = getVertex(edge->to);
|
|
|
|
|
|
|
|
s << std::dec;
|
|
|
|
s << " \"" << *from << "\" -> \"" << *to << "\""
|
|
|
|
<< " [label=\"" << *edge << "\"];" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 18:33:34 +02:00
|
|
|
if (s.is_open()) {
|
2018-08-21 00:25:44 +02:00
|
|
|
s << "}" << std::endl;
|
|
|
|
s.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
VertexIdentifier lastVertexId;
|
|
|
|
EdgeIdentifier lastEdgeId;
|
|
|
|
|
|
|
|
std::map<VertexIdentifier, std::shared_ptr<VertexType>> vertices;
|
|
|
|
std::map<EdgeIdentifier, std::shared_ptr<EdgeType>> edges;
|
|
|
|
|
2018-10-19 14:33:10 +02:00
|
|
|
Logger logger;
|
2018-08-21 00:25:44 +02:00
|
|
|
};
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace graph
|
|
|
|
} // namespace villas
|