2021-06-28 14:12:19 +00:00
|
|
|
/*##############################################################################
|
|
|
|
## Author: Shaun Reed ##
|
|
|
|
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
|
|
|
## About: An example of an object graph implementation ##
|
|
|
|
## Algorithms in this example are found in MIT Intro to Algorithms ##
|
|
|
|
## ##
|
|
|
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
|
|
|
################################################################################
|
|
|
|
*/
|
|
|
|
#ifndef LIB_GRAPH_HPP
|
|
|
|
#define LIB_GRAPH_HPP
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
2021-06-28 16:46:04 +00:00
|
|
|
#include <unordered_set>
|
2021-06-28 14:12:19 +00:00
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
// Color represents the discovery status of any given node
|
|
|
|
// + White is undiscovered, Gray is in progress, Black is fully discovered
|
2021-06-28 14:12:19 +00:00
|
|
|
enum Color {White, Gray, Black};
|
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
// Node structure for representing a graph
|
2021-06-28 14:12:19 +00:00
|
|
|
struct Node {
|
|
|
|
public:
|
2021-07-01 21:17:47 +00:00
|
|
|
// Constructors
|
2021-06-28 16:46:04 +00:00
|
|
|
Node(const Node &rhs) = default;
|
|
|
|
Node & operator=(Node rhs) {
|
|
|
|
if (this == &rhs) return *this;
|
|
|
|
swap(*this, rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
2021-07-01 21:17:47 +00:00
|
|
|
Node(int num, std::vector<int> adj) : number(num), adjacent(std::move(adj)) {}
|
|
|
|
|
2021-06-28 16:46:04 +00:00
|
|
|
friend void swap(Node &a, Node &b) {
|
|
|
|
std::swap(a.number, b.number);
|
|
|
|
std::swap(a.adjacent, b.adjacent);
|
|
|
|
std::swap(a.color, b.color);
|
|
|
|
std::swap(a.discoveryFinish, b.discoveryFinish);
|
|
|
|
}
|
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
// Don't allow anyone to change these values when using a const reference
|
2021-06-28 14:12:19 +00:00
|
|
|
int number;
|
2021-06-28 16:46:04 +00:00
|
|
|
std::vector<int> adjacent;
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
// Mutable members so we can update these values when using a const reference
|
|
|
|
// + Since they need to be modified during traversals
|
|
|
|
|
|
|
|
// Coloring of the nodes are used in both DFS and BFS
|
2021-06-28 14:12:19 +00:00
|
|
|
mutable Color color = White;
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
// Used in BFS to represent distance from start node
|
|
|
|
mutable int distance = 0;
|
|
|
|
// Used in BFS to represent the parent node that discovered this node
|
|
|
|
// + If we use this node as the starting point, this will remain a nullptr
|
2021-07-03 01:29:52 +00:00
|
|
|
mutable const Node *predecessor = nullptr;
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
// Create a pair to track discovery / finish time when using DFS
|
2021-06-28 16:46:04 +00:00
|
|
|
// + Discovery time is the iteration the node is first discovered
|
|
|
|
// + Finish time is the iteration the node has been checked completely
|
|
|
|
// ++ A finished node has considered all adjacent nodes
|
|
|
|
mutable std::pair<int, int> discoveryFinish;
|
2021-06-28 14:12:19 +00:00
|
|
|
|
2021-06-28 16:46:04 +00:00
|
|
|
// Define a comparator for std::sort
|
|
|
|
// + This will help to sort nodes by finished time after traversal
|
|
|
|
static bool FinishedSort(const Node &node1, const Node &node2)
|
2021-07-03 01:29:52 +00:00
|
|
|
{ return node1.discoveryFinish.second < node2.discoveryFinish.second;}
|
|
|
|
|
|
|
|
// Define operator== for std::find; And comparisons between nodes
|
2021-06-28 16:46:04 +00:00
|
|
|
bool operator==(const Node &b) const { return this->number == b.number;}
|
2021-07-03 01:29:52 +00:00
|
|
|
// Define an operator!= for comparing nodes for inequality
|
|
|
|
bool operator!=(const Node &b) const { return this->number != b.number;}
|
2021-06-28 14:12:19 +00:00
|
|
|
};
|
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
// Graph class declaration
|
2021-06-28 14:12:19 +00:00
|
|
|
class Graph {
|
|
|
|
public:
|
2021-07-01 21:17:47 +00:00
|
|
|
// Constructor
|
2021-06-28 14:12:19 +00:00
|
|
|
explicit Graph(std::vector<Node> nodes) : nodes_(std::move(nodes)) {}
|
|
|
|
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
// Breadth First Search
|
2021-06-28 14:12:19 +00:00
|
|
|
void BFS(const Node& startNode) const;
|
2021-07-03 01:29:52 +00:00
|
|
|
std::deque<Node> PathBFS(const Node &start, const Node &finish) const;
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Depth First Search
|
2021-06-28 14:12:19 +00:00
|
|
|
void DFS() const;
|
2021-07-03 01:29:52 +00:00
|
|
|
// An alternate DFS that checks each node of the graph beginning at startNode
|
2021-07-01 21:17:47 +00:00
|
|
|
void DFS(const Node &startNode) const;
|
2021-07-03 01:29:52 +00:00
|
|
|
// Visit function is used in both versions of DFS
|
2021-06-28 16:46:04 +00:00
|
|
|
void DFSVisit(int &time, const Node& startNode) const;
|
2021-07-01 21:17:47 +00:00
|
|
|
// Topological sort, using DFS
|
|
|
|
std::vector<Node> TopologicalSort(const Node &startNode) const;
|
|
|
|
|
|
|
|
|
|
|
|
// Returns a copy of a node with the number i within the graph
|
2021-07-03 01:29:52 +00:00
|
|
|
// + This uses the private, non-const accessor GetNode()
|
2021-07-01 21:17:47 +00:00
|
|
|
inline Node GetNodeCopy(int i) { return GetNode(i);}
|
|
|
|
// Return a constant iterator for reading node values
|
2021-07-03 01:29:52 +00:00
|
|
|
inline std::vector<Node>::const_iterator NodeBegin() { return nodes_.cbegin();}
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// A non-const accessor for direct access to a node with the number value i
|
|
|
|
inline Node & GetNode(int i)
|
2021-07-03 01:29:52 +00:00
|
|
|
{ return *std::find(nodes_.begin(), nodes_.end(), Node(i, {}));}
|
2021-07-01 21:17:47 +00:00
|
|
|
// For use with const member functions to access mutable values
|
|
|
|
inline const Node & GetNode(int i) const
|
2021-07-03 01:29:52 +00:00
|
|
|
{ return *std::find(nodes_.begin(), nodes_.end(), Node(i, {}));}
|
2021-07-01 21:17:47 +00:00
|
|
|
|
|
|
|
std::vector<Node> nodes_;
|
2021-06-28 14:12:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // LIB_GRAPH_HPP
|