Clean up object-graph implementation
This commit is contained in:
@@ -56,7 +56,7 @@ public:
|
||||
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
|
||||
mutable Node *predecessor = nullptr;
|
||||
mutable const Node *predecessor = nullptr;
|
||||
|
||||
// Create a pair to track discovery / finish time when using DFS
|
||||
// + Discovery time is the iteration the node is first discovered
|
||||
@@ -67,9 +67,12 @@ public:
|
||||
// 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)
|
||||
{ return node1.discoveryFinish.second < node2.discoveryFinish.second;}
|
||||
// Define operator== for std::find
|
||||
{ return node1.discoveryFinish.second < node2.discoveryFinish.second;}
|
||||
|
||||
// Define operator== for std::find; And comparisons between nodes
|
||||
bool operator==(const Node &b) const { return this->number == b.number;}
|
||||
// Define an operator!= for comparing nodes for inequality
|
||||
bool operator!=(const Node &b) const { return this->number != b.number;}
|
||||
};
|
||||
|
||||
|
||||
@@ -83,29 +86,32 @@ public:
|
||||
|
||||
// Breadth First Search
|
||||
void BFS(const Node& startNode) const;
|
||||
std::deque<const Node *> PathBFS(const Node &start, const Node &finish) const;
|
||||
std::deque<Node> PathBFS(const Node &start, const Node &finish) const;
|
||||
|
||||
|
||||
// Depth First Search
|
||||
void DFS() const;
|
||||
// An alternate DFS that checks each node of the graph beginning at startNode
|
||||
void DFS(const Node &startNode) const;
|
||||
// Visit function is used in both versions of DFS
|
||||
void DFSVisit(int &time, const Node& startNode) const;
|
||||
// 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
|
||||
// + This uses the private, non-const accessor GetNode()
|
||||
inline Node GetNodeCopy(int i) { return GetNode(i);}
|
||||
// Return a constant iterator for reading node values
|
||||
inline std::vector<Node>::const_iterator NodeBegin() { return nodes_.begin();}
|
||||
inline std::vector<Node>::const_iterator NodeBegin() { return nodes_.cbegin();}
|
||||
|
||||
private:
|
||||
// A non-const accessor for direct access to a node with the number value i
|
||||
inline Node & GetNode(int i)
|
||||
{ return *std::find(nodes_.begin(), nodes_.end(), Node(i, {}));}
|
||||
{ return *std::find(nodes_.begin(), nodes_.end(), Node(i, {}));}
|
||||
// For use with const member functions to access mutable values
|
||||
inline const Node & GetNode(int i) const
|
||||
{ return *std::find(nodes_.begin(), nodes_.end(), Node(i, {}));}
|
||||
{ return *std::find(nodes_.begin(), nodes_.end(), Node(i, {}));}
|
||||
|
||||
std::vector<Node> nodes_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user