2021-06-28 14:12:19 +00:00
|
|
|
/*##############################################################################
|
|
|
|
## Author: Shaun Reed ##
|
|
|
|
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
|
|
|
## About: Driver program to test object graph implementation ##
|
|
|
|
## ##
|
|
|
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
|
|
|
################################################################################
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib-graph.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
void Graph::BFS(const Node& startNode) const
|
|
|
|
{
|
2021-07-03 01:29:52 +00:00
|
|
|
// Track the nodes we have discovered by their Color
|
2021-07-01 21:17:47 +00:00
|
|
|
for (const auto &node : nodes_) {
|
|
|
|
node.color = White;
|
2021-07-03 01:29:52 +00:00
|
|
|
// Track distance from the startNode
|
2021-07-01 21:17:47 +00:00
|
|
|
node.distance = 0;
|
2021-07-03 01:29:52 +00:00
|
|
|
// Track predecessor using node that discovers this node
|
|
|
|
// + If this is the startNode, predecessor remains nullptr
|
2021-07-01 21:17:47 +00:00
|
|
|
node.predecessor = nullptr;
|
|
|
|
}
|
2021-06-28 14:12:19 +00:00
|
|
|
|
|
|
|
// Create a queue to visit discovered nodes in FIFO order
|
2021-07-03 01:29:52 +00:00
|
|
|
std::queue<const Node *> visitQueue;
|
2021-06-28 14:12:19 +00:00
|
|
|
|
|
|
|
// Mark the startNode as in progress until we finish checking adjacent nodes
|
|
|
|
startNode.color = Gray;
|
|
|
|
|
|
|
|
// Visit the startNode
|
2021-07-03 01:29:52 +00:00
|
|
|
visitQueue.push(&startNode);
|
2021-06-28 14:12:19 +00:00
|
|
|
|
|
|
|
// Continue to visit nodes until there are none left in the graph
|
|
|
|
while (!visitQueue.empty()) {
|
|
|
|
// Remove thisNode from the visitQueue, storing its vertex locally
|
2021-07-03 01:29:52 +00:00
|
|
|
const Node * thisNode = visitQueue.front();
|
2021-06-28 14:12:19 +00:00
|
|
|
visitQueue.pop();
|
2021-07-03 01:29:52 +00:00
|
|
|
std::cout << "Visiting node " << thisNode->number << std::endl;
|
2021-06-28 14:12:19 +00:00
|
|
|
|
|
|
|
// Check if we have already discovered all the adjacentNodes to thisNode
|
2021-07-03 01:29:52 +00:00
|
|
|
for (const auto &adjacent : thisNode->adjacent) {
|
2021-07-01 21:17:47 +00:00
|
|
|
if (GetNode(adjacent).color == White) {
|
2021-06-28 14:12:19 +00:00
|
|
|
std::cout << "Found undiscovered adjacentNode: " << adjacent << "\n";
|
|
|
|
// Mark the adjacent node as in progress
|
2021-07-01 21:17:47 +00:00
|
|
|
GetNode(adjacent).color = Gray;
|
2021-07-03 01:29:52 +00:00
|
|
|
GetNode(adjacent).distance = thisNode->distance + 1;
|
|
|
|
GetNode(adjacent).predecessor = &GetNode(thisNode->number);
|
2021-07-01 21:17:47 +00:00
|
|
|
|
2021-06-28 14:12:19 +00:00
|
|
|
// Add the discovered node the the visitQueue
|
2021-07-03 01:29:52 +00:00
|
|
|
visitQueue.push(&GetNode(adjacent));
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We are finished with this node and the adjacent nodes; Mark it discovered
|
2021-07-03 01:29:52 +00:00
|
|
|
GetNode(thisNode->number).color = Black;
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|
2021-07-01 21:17:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-03 01:29:52 +00:00
|
|
|
std::deque<Node> Graph::PathBFS(const Node &start, const Node &finish) const
|
2021-07-01 21:17:47 +00:00
|
|
|
{
|
2021-07-03 01:29:52 +00:00
|
|
|
// Store the path as copies of each node
|
|
|
|
// + If the caller modifies these, it will not impact the graph's data
|
|
|
|
std::deque<Node> path;
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
BFS(start);
|
|
|
|
const Node * next = finish.predecessor;
|
|
|
|
bool isValid = false;
|
|
|
|
do {
|
|
|
|
// If we have reached the start node, we have found a valid path
|
|
|
|
if (*next == Node(start)) isValid = true;
|
2021-06-28 14:12:19 +00:00
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
// Add the node to the path as we check each node
|
2021-07-03 01:29:52 +00:00
|
|
|
// + Use emplace_front to call the Node copy constructor
|
|
|
|
path.emplace_front(*next);
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
// Move to the next node
|
|
|
|
next = next->predecessor;
|
|
|
|
} while (next != nullptr);
|
2021-07-03 01:29:52 +00:00
|
|
|
// Use emplace_back to call Node copy constructor
|
|
|
|
path.emplace_back(finish);
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
// If we never found a valid path, erase all contents of the path
|
|
|
|
if (!isValid) path.erase(path.begin(), path.end());
|
|
|
|
|
|
|
|
// Return the path, the caller should handle empty paths accordingly
|
|
|
|
return path;
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Graph::DFS() const
|
|
|
|
{
|
|
|
|
// Track the nodes we have discovered
|
|
|
|
for (const auto &node : nodes_) node.color = White;
|
2021-06-28 16:46:04 +00:00
|
|
|
int time = 0;
|
2021-06-28 14:12:19 +00:00
|
|
|
|
|
|
|
// Visit each node in the graph
|
|
|
|
for (const auto& node : nodes_) {
|
|
|
|
std::cout << "Visiting node " << node.number << std::endl;
|
|
|
|
// If the node is undiscovered, visit it
|
|
|
|
if (node.color == White) {
|
|
|
|
std::cout << "Found undiscovered node: " << node.number << std::endl;
|
|
|
|
// Visiting the undiscovered node will check it's adjacent nodes
|
2021-06-28 16:46:04 +00:00
|
|
|
DFSVisit(time, node);
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
void Graph::DFS(const Node &startNode) const
|
|
|
|
{
|
|
|
|
// Track the nodes we have discovered
|
|
|
|
for (const auto &node : nodes_) node.color = White;
|
|
|
|
int time = 0;
|
|
|
|
|
|
|
|
auto startIter = std::find(nodes_.begin(), nodes_.end(),
|
|
|
|
Node(startNode.number, {})
|
|
|
|
);
|
|
|
|
|
2021-07-03 01:29:52 +00:00
|
|
|
// beginning at startNode, visit each node in the graph until we reach the end
|
2021-07-01 21:17:47 +00:00
|
|
|
while (startIter != nodes_.end()) {
|
|
|
|
std::cout << "Visiting node " << startIter->number << std::endl;
|
|
|
|
// If the startIter is undiscovered, visit it
|
|
|
|
if (startIter->color == White) {
|
|
|
|
std::cout << "Found undiscovered node: " << startIter->number << std::endl;
|
|
|
|
// Visiting the undiscovered node will check it's adjacent nodes
|
|
|
|
DFSVisit(time, *startIter);
|
|
|
|
}
|
|
|
|
startIter++;
|
|
|
|
}
|
2021-07-03 01:29:52 +00:00
|
|
|
|
|
|
|
// Once we reach the last node, check the beginning for unchecked nodes
|
2021-07-01 21:17:47 +00:00
|
|
|
startIter = nodes_.begin();
|
|
|
|
|
2021-07-03 01:29:52 +00:00
|
|
|
// Once we reach the initial startNode, we have checked all nodes
|
|
|
|
while (*startIter != startNode) {
|
2021-07-01 21:17:47 +00:00
|
|
|
std::cout << "Visiting node " << startIter->number << std::endl;
|
|
|
|
// If the startIter is undiscovered, visit it
|
|
|
|
if (startIter->color == White) {
|
|
|
|
std::cout << "Found undiscovered node: " << startIter->number << std::endl;
|
|
|
|
// Visiting the undiscovered node will check it's adjacent nodes
|
|
|
|
DFSVisit(time, *startIter);
|
|
|
|
}
|
|
|
|
startIter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:46:04 +00:00
|
|
|
void Graph::DFSVisit(int &time, const Node& startNode) const
|
2021-06-28 14:12:19 +00:00
|
|
|
{
|
|
|
|
startNode.color = Gray;
|
2021-06-28 16:46:04 +00:00
|
|
|
time++;
|
|
|
|
startNode.discoveryFinish.first = time;
|
2021-07-03 01:29:52 +00:00
|
|
|
|
2021-06-28 14:12:19 +00:00
|
|
|
// Check the adjacent nodes of the startNode
|
|
|
|
for (const auto &adjacent : startNode.adjacent) {
|
2021-06-28 16:46:04 +00:00
|
|
|
auto iter = std::find(nodes_.begin(), nodes_.end(),
|
|
|
|
Node(adjacent, {}));
|
2021-06-28 14:12:19 +00:00
|
|
|
// If the adjacentNode is undiscovered, visit it
|
|
|
|
// + Offset by 1 to account for 0 index of discovered vector
|
2021-06-28 16:46:04 +00:00
|
|
|
if (iter->color == White) {
|
2021-06-28 14:12:19 +00:00
|
|
|
std::cout << "Found undiscovered adjacentNode: "
|
2021-07-01 21:17:47 +00:00
|
|
|
<< GetNode(adjacent).number << std::endl;
|
2021-06-28 14:12:19 +00:00
|
|
|
// Visiting the undiscovered node will check it's adjacent nodes
|
2021-06-28 16:46:04 +00:00
|
|
|
DFSVisit(time, *iter);
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
startNode.color = Black;
|
2021-06-28 16:46:04 +00:00
|
|
|
time++;
|
|
|
|
startNode.discoveryFinish.second = time;
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
std::vector<Node> Graph::TopologicalSort(const Node &startNode) const
|
2021-06-28 14:12:19 +00:00
|
|
|
{
|
2021-07-01 21:17:47 +00:00
|
|
|
DFS(GetNode(startNode.number));
|
2021-06-28 16:46:04 +00:00
|
|
|
std::vector<Node> topological(nodes_);
|
2021-06-28 14:12:19 +00:00
|
|
|
|
2021-06-28 16:46:04 +00:00
|
|
|
std::sort(topological.begin(), topological.end(), Node::FinishedSort);
|
2021-06-28 14:12:19 +00:00
|
|
|
|
|
|
|
// The topologicalOrder is read right-to-left in the final result
|
|
|
|
// + Output is handled in main as FILO, similar to a stack
|
2021-06-28 16:46:04 +00:00
|
|
|
return topological;
|
2021-06-28 14:12:19 +00:00
|
|
|
}
|