forked from vgvassilev/clad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compute and process Differentiation Request graph
- Loading branch information
Showing
12 changed files
with
393 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
#ifndef CLAD_DYNAMICGRAPH_H | ||
#define CLAD_DYNAMICGRAPH_H | ||
|
||
#include <algorithm> | ||
#include <functional> | ||
#include <iostream> | ||
#include <queue> | ||
#include <set> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
namespace clad { | ||
template <typename T> class DynamicGraph { | ||
private: | ||
// Storing nodes in the graph. The index of the node in the vector is used as | ||
// a unique identifier for the node in the adjacency list. | ||
std::vector<T> nodes; | ||
|
||
// Store the nodes in the graph as an unordered map from the node to a boolean | ||
// indicating whether the node is processed or not. The second element in the | ||
// pair is the id of the node in the nodes vector. | ||
std::unordered_map<T, std::pair<bool, size_t>> nodeMap; | ||
|
||
// Store the adjacency list for the graph. The adjacency list is a map from | ||
// a node to the set of nodes that it has an edge to. We use integers inside | ||
// the set to avoid copying the nodes. | ||
std::unordered_map<size_t, std::set<size_t>> adjList; | ||
|
||
// Store the reverse adjacency list for the graph. The reverse adjacency list | ||
// is a map from a node to the set of nodes that have an edge to it. We use | ||
// integers inside the set to avoid copying the nodes. | ||
std::unordered_map<size_t, std::set<size_t>> revAdjList; | ||
|
||
// Set of source nodes in the graph. | ||
std::set<size_t> sources; | ||
|
||
// Store the id of the node being processed right now. | ||
int currentId = -1; // -1 means no node is being processed. | ||
|
||
// Maintain a queue of nodes to be processed next. | ||
std::queue<size_t> toProcessQueue; | ||
|
||
public: | ||
DynamicGraph() = default; | ||
|
||
// Add an edge from src to dest | ||
void addEdge(const T& src, const T& dest) { | ||
std::pair<bool, size_t> srcInfo = addNode(src); | ||
std::pair<bool, size_t> destInfo = addNode(dest); | ||
size_t srcId = srcInfo.second; | ||
size_t destId = destInfo.second; | ||
adjList[srcId].insert(destId); | ||
revAdjList[destId].insert(srcId); | ||
} | ||
|
||
// Add a node to the graph | ||
std::pair<bool, size_t> addNode(const T& node, bool isSource = false) { | ||
if (nodeMap.find(node) == nodeMap.end()) { | ||
size_t id = nodes.size(); | ||
nodes.push_back(node); | ||
nodeMap[node] = {false, id}; // node is not processed yet. | ||
adjList[id] = {}; | ||
revAdjList[id] = {}; | ||
if (isSource) { | ||
sources.insert(id); | ||
toProcessQueue.push(id); | ||
} | ||
} | ||
return nodeMap[node]; | ||
} | ||
|
||
// Adds the edge from the current node to the destination node. | ||
void addEdgeToCurrentNode(const T& dest) { | ||
if (currentId == -1) | ||
return; | ||
addEdge(nodes[currentId], dest); | ||
} | ||
|
||
// Set the current node to the node with the given id. | ||
void setCurrentProcessingNode(const T& node) { | ||
if (nodeMap.find(node) != nodeMap.end()) | ||
currentId = nodeMap[node].second; | ||
} | ||
|
||
// Mark the current node as processed. | ||
void markCurrentNodeProcessed() { | ||
if (currentId != -1) { | ||
nodeMap[nodes[currentId]].first = true; | ||
for (size_t destId : adjList[currentId]) | ||
if (!nodeMap[nodes[destId]].first) | ||
toProcessQueue.push(destId); | ||
} | ||
currentId = -1; | ||
} | ||
|
||
// Get the nodes in the graph. | ||
std::vector<T> getNodes() { return nodes; } | ||
|
||
// Check if two nodes are connected in the graph. | ||
bool isConnected(const T& src, const T& dest) { | ||
if (nodeMap.find(src) == nodeMap.end() || | ||
nodeMap.find(dest) == nodeMap.end()) | ||
return false; | ||
size_t srcId = nodeMap[src].second; | ||
size_t destId = nodeMap[dest].second; | ||
return adjList[srcId].find(destId) != adjList[srcId].end(); | ||
} | ||
|
||
// Print the graph in a human-readable format. | ||
void print() { | ||
// First print the nodes with their insertion order. | ||
for (const T& node : nodes) { | ||
std::pair<bool, int> nodeInfo = nodeMap[node]; | ||
std::cout << (std::string)node << ": #" << nodeInfo.second; | ||
if (sources.find(nodeInfo.second) != sources.end()) | ||
std::cout << " (source)"; | ||
if (nodeInfo.first) | ||
std::cout << ", (done)\n"; | ||
else | ||
std::cout << ", (unprocessed)\n"; | ||
} | ||
// Then print the edges. | ||
for (int i = 0; i < nodes.size(); i++) | ||
for (size_t dest : adjList[i]) | ||
std::cout << i << " -> " << dest << "\n"; | ||
} | ||
|
||
// Topological sort of the directed graph. If the graph is not a DAG, the | ||
// result will be a partial order. Use a recursive dfs heler function to | ||
// implement the topological sort. If a->b, then a will come before b in the | ||
// topological sort. In reverseOrder mode, the result will be in reverse | ||
// topological order, i.e a->b, then b will come before a in the result. | ||
std::vector<T> topologicalSort(bool reverseOrder = false) { | ||
std::vector<T> res; | ||
std::unordered_set<size_t> visited; | ||
|
||
std::function<void(size_t)> dfs = [&](size_t node) -> void { | ||
visited.insert(node); | ||
for (size_t dest : adjList[node]) | ||
if (visited.find(dest) == visited.end()) | ||
dfs(dest); | ||
res.push_back(nodes[node]); | ||
}; | ||
for (size_t source : sources) | ||
if (visited.find(source) == visited.end()) | ||
dfs(source); | ||
|
||
if (reverseOrder) | ||
return res; | ||
std::reverse(res.begin(), res.end()); | ||
return res; | ||
} | ||
|
||
// Get the next to process node from the queue of nodes to be processed. | ||
T getNextToProcessNode() { | ||
if (toProcessQueue.empty()) | ||
return T(); | ||
size_t nextId = toProcessQueue.front(); | ||
toProcessQueue.pop(); | ||
return nodes[nextId]; | ||
} | ||
}; | ||
} // end namespace clad | ||
|
||
#endif // CLAD_DYNAMICGRAPH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.