1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/******************************************************************************
*
* Project: GDAL
* Purpose: Implementation of topologic sorting over a directed acyclic graph
* Author: Even Rouault
*
******************************************************************************
* Copyright (c) 2021, Even Rouault <even dot rouault at spatialys dot com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#ifndef DIRECTEDACYCLICGRAPH_INCLUDED_H
#define DIRECTEDACYCLICGRAPH_INCLUDED_H
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
namespace gdal
{
// See https://en.wikipedia.org/wiki/Directed_acyclic_graph
template <class T, class V = std::string> class DirectedAcyclicGraph
{
std::set<T> nodes{};
std::map<T, std::set<T>>
incomingNodes{}; // incomingNodes[j][i] means an edge from i to j
std::map<T, std::set<T>>
outgoingNodes{}; // outgoingNodes[i][j] means an edge from i to j
std::map<T, V> names{};
public:
DirectedAcyclicGraph() = default;
void clear()
{
nodes.clear();
incomingNodes.clear();
outgoingNodes.clear();
names.clear();
}
void addNode(const T &i, const V &s)
{
nodes.insert(i);
names[i] = s;
}
void removeNode(const T &i);
const char *addEdge(const T &i, const T &j);
const char *removeEdge(const T &i, const T &j);
bool isTherePathFromTo(const T &i, const T &j) const;
std::vector<T> findStartingNodes() const;
std::vector<T> getTopologicalOrdering();
};
template <class T, class V>
void DirectedAcyclicGraph<T, V>::removeNode(const T &i)
{
nodes.erase(i);
names.erase(i);
{
auto incomingIter = incomingNodes.find(i);
if (incomingIter != incomingNodes.end())
{
for (const T &j : incomingIter->second)
{
auto outgoingIter = outgoingNodes.find(j);
assert(outgoingIter != outgoingNodes.end());
auto iterJI = outgoingIter->second.find(i);
assert(iterJI != outgoingIter->second.end());
outgoingIter->second.erase(iterJI);
if (outgoingIter->second.empty())
outgoingNodes.erase(outgoingIter);
}
incomingNodes.erase(incomingIter);
}
}
{
auto outgoingIter = outgoingNodes.find(i);
if (outgoingIter != outgoingNodes.end())
{
for (const T &j : outgoingIter->second)
{
auto incomingIter = incomingNodes.find(j);
assert(incomingIter != incomingNodes.end());
auto iterJI = incomingIter->second.find(i);
assert(iterJI != incomingIter->second.end());
incomingIter->second.erase(iterJI);
if (incomingIter->second.empty())
incomingNodes.erase(incomingIter);
}
outgoingNodes.erase(outgoingIter);
}
}
}
template <class T, class V>
const char *DirectedAcyclicGraph<T, V>::addEdge(const T &i, const T &j)
{
if (i == j)
{
return "self cycle";
}
const auto iterI = outgoingNodes.find(i);
if (iterI != outgoingNodes.end() &&
iterI->second.find(j) != iterI->second.end())
{
return "already inserted edge";
}
if (!cpl::contains(nodes, i))
{
return "node i unknown";
}
if (!cpl::contains(nodes, j))
{
return "node j unknown";
}
if (isTherePathFromTo(j, i))
{
return "can't add edge: this would cause a cycle";
}
outgoingNodes[i].insert(j);
incomingNodes[j].insert(i);
return nullptr;
}
template <class T, class V>
const char *DirectedAcyclicGraph<T, V>::removeEdge(const T &i, const T &j)
{
auto iterI = outgoingNodes.find(i);
if (iterI == outgoingNodes.end())
return "no outgoing nodes from i";
auto iterIJ = iterI->second.find(j);
if (iterIJ == iterI->second.end())
return "no outgoing node from i to j";
iterI->second.erase(iterIJ);
if (iterI->second.empty())
outgoingNodes.erase(iterI);
auto iterJ = incomingNodes.find(j);
assert(iterJ != incomingNodes.end());
auto iterJI = iterJ->second.find(i);
assert(iterJI != iterJ->second.end());
iterJ->second.erase(iterJI);
if (iterJ->second.empty())
incomingNodes.erase(iterJ);
return nullptr;
}
template <class T, class V>
bool DirectedAcyclicGraph<T, V>::isTherePathFromTo(const T &i, const T &j) const
{
std::set<T> plannedForVisit;
std::stack<T> toVisit;
toVisit.push(i);
plannedForVisit.insert(i);
while (!toVisit.empty())
{
const T n = toVisit.top();
toVisit.pop();
if (n == j)
return true;
const auto iter = outgoingNodes.find(n);
if (iter != outgoingNodes.end())
{
for (const T &k : iter->second)
{
if (!cpl::contains(plannedForVisit, k))
{
plannedForVisit.insert(k);
toVisit.push(k);
}
}
}
}
return false;
}
template <class T, class V>
std::vector<T> DirectedAcyclicGraph<T, V>::findStartingNodes() const
{
std::vector<T> ret;
for (const auto &i : nodes)
{
if (!cpl::contains(incomingNodes, i))
ret.emplace_back(i);
}
return ret;
}
// Kahn's algorithm:
// https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
template <class T, class V>
std::vector<T> DirectedAcyclicGraph<T, V>::getTopologicalOrdering()
{
std::vector<T> ret;
ret.reserve(nodes.size());
const auto cmp = [this](const T &a, const T &b)
{ return names.find(a)->second < names.find(b)->second; };
std::set<T, decltype(cmp)> S(cmp);
const auto sn = findStartingNodes();
for (const auto &i : sn)
S.insert(i);
while (true)
{
auto iterFirst = S.begin();
if (iterFirst == S.end())
break;
const auto n = *iterFirst;
S.erase(iterFirst);
ret.emplace_back(n);
const auto iter = outgoingNodes.find(n);
if (iter != outgoingNodes.end())
{
// Need to take a copy as we remove edges during iteration
const std::set<T> myOutgoingNodes = iter->second;
for (const T &m : myOutgoingNodes)
{
const char *retRemoveEdge = removeEdge(n, m);
(void)retRemoveEdge;
assert(retRemoveEdge == nullptr);
if (!cpl::contains(incomingNodes, m))
{
S.insert(m);
}
}
}
}
// Should not happen for a direct acyclic graph
assert(incomingNodes.empty());
assert(outgoingNodes.empty());
return ret;
}
} // namespace gdal
#endif // DIRECTEDACYCLICGRAPH_INCLUDED_H
|