[cpp] Clean up graph implementations

This commit is contained in:
Shaun Reed 2022-04-13 21:15:03 -04:00
parent 6986c73651
commit 4b47630548
11 changed files with 227 additions and 185 deletions

View File

@ -1,8 +1,7 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of an object graph implementation ## ## About: Driver program to test object graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
################################################################################ ################################################################################

View File

@ -1,7 +1,8 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Driver program to test object graph implementation ## ## 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 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
################################################################################ ################################################################################
@ -183,4 +184,3 @@ std::vector<Node> Graph::TopologicalSort(const Node &startNode) const
// + Output is handled in main as FILO, similar to a stack // + Output is handled in main as FILO, similar to a stack
return order; return order;
} }

View File

@ -1,6 +1,6 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of an object graph implementation ## ## About: An example of an object graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ## ## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
@ -10,51 +10,14 @@
#ifndef LIB_GRAPH_HPP #ifndef LIB_GRAPH_HPP
#define LIB_GRAPH_HPP #define LIB_GRAPH_HPP
#include <iostream>
#include <algorithm> #include <algorithm>
#include <iostream>
#include <map> #include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <queue>
#include <unordered_set>
#include <unordered_map>
/******************************************************************************/
// Structures for tracking information gathered from various traversals
struct Node;
// Color represents the discovery status of any given node
// + White is undiscovered, Gray is in progress, Black is fully discovered
enum Color {White, Gray, Black};
// Information used in all searches
struct SearchInfo {
// Coloring of the nodes is used in both DFS and BFS
Color discovered = White;
};
// Information that is only used in BFS
struct BFS : SearchInfo {
// Used to represent distance from start node
int distance = 0;
// Used to represent the parent node that discovered this node
// + If we use this node as the starting point, this will remain a nullptr
const Node *predecessor = nullptr;
};
// Information that is only used in DFS
struct DFS : SearchInfo {
// Create a pair to track discovery / finish time
// + 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
std::pair<int, int> discoveryFinish;
};
// Store search information in unordered_maps so we can pass it around easily
// + Allows each node to store relative information on the traversal
using InfoBFS = std::unordered_map<int, struct BFS>;
using InfoDFS = std::unordered_map<int, struct DFS>;
/******************************************************************************/ /******************************************************************************/
@ -63,14 +26,16 @@ struct Node {
public: public:
// Constructors // Constructors
Node(const Node &rhs) = default; Node(const Node &rhs) = default;
Node & operator=(Node rhs) { Node & operator=(Node rhs)
{
if (this == &rhs) return *this; if (this == &rhs) return *this;
swap(*this, rhs); swap(*this, rhs);
return *this; return *this;
} }
Node(int num, std::vector<int> adj) : number(num), adjacent(std::move(adj)) {} Node(int num, std::vector<int> adj) : number(num), adjacent(std::move(adj)) {}
friend void swap(Node &a, Node &b) { friend void swap(Node &a, Node &b)
{
std::swap(a.number, b.number); std::swap(a.number, b.number);
std::swap(a.adjacent, b.adjacent); std::swap(a.adjacent, b.adjacent);
} }
@ -85,8 +50,61 @@ public:
}; };
/******************************************************************************/
// Base struct for storing traversal information on all nodes
// Color represents the discovery status of any given node
enum Color {
// Node is marked as undiscovered
White,
// Node discovery is in progress; Some adjacent nodes have not been checked
Gray,
// Node has been discovered; All adjacent nodes have been checked
Black
};
// Information used in all searches
struct SearchInfo {
// Coloring of the nodes is used in both DFS and BFS
Color discovered = White;
};
/******************************************************************************/
// BFS search information struct
// Information that is only used in BFS
struct BFS : SearchInfo {
// Used to represent distance from start node
int distance = 0;
// Used to represent the parent node that discovered this node
// + If we use this node as the starting point, this will remain a nullptr
const Node *predecessor = nullptr;
};
// Store search information in unordered_maps so we can pass it around easily
// + Allows each node to store relative information on the traversal
using InfoBFS = std::unordered_map<int, struct BFS>;
/******************************************************************************/
// DFS search information struct
// Information that is only used in DFS
struct DFS : SearchInfo {
// Create a pair to track discovery / finish time
// + 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
std::pair<int, int> discoveryFinish;
};
using InfoDFS = std::unordered_map<int, struct DFS>;
/******************************************************************************/ /******************************************************************************/
// Graph class declaration // Graph class declaration
class Graph { class Graph {
public: public:
// Constructor // Constructor

View File

@ -1,6 +1,6 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Driver program to test a simple graph implementation ## ## About: Driver program to test a simple graph implementation ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##

View File

@ -1,6 +1,6 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of a simple graph implementation ## ## About: An example of a simple graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ## ## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##

View File

@ -1,6 +1,6 @@
/*############################################################################# /*#############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of a simple graph implementation ## ## About: An example of a simple graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ## ## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
@ -12,9 +12,9 @@
#include <iostream> #include <iostream>
#include <queue> #include <queue>
#include <vector>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <vector>
class Graph { class Graph {

View File

@ -1,7 +1,7 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of a weighted graph implementation ## ## About: Driver program to test templated object graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ## ## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##

View File

@ -1,7 +1,7 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of an object graph implementation ## ## About: An example of a templated object graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ## ## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
@ -10,24 +10,74 @@
#ifndef LIB_GRAPH_HPP #ifndef LIB_GRAPH_HPP
#define LIB_GRAPH_HPP #define LIB_GRAPH_HPP
#include <iostream>
#include <algorithm> #include <algorithm>
#include <iostream>
#include <map> #include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <queue>
#include <unordered_set>
#include <unordered_map> /******************************************************************************/
// Node structure for representing a graph
template <typename T>
struct Node {
public:
template <typename> friend class Graph;
template <typename> friend class InfoMST;
// Constructors
Node(const Node &rhs) = default;
Node & operator=(Node rhs)
{
if (this == &rhs) return *this;
swap(*this, rhs);
return *this;
}
Node(T data, const std::vector<std::pair<T, int>> &adj) : data_(data)
{
// Place each adjacent node in vector into our unordered_map of edges
for (const auto &i : adj) adjacent_.emplace(i.first, i.second);
}
friend void swap(Node &a, Node &b)
{
std::swap(a.data_, b.data_);
std::swap(a.adjacent_, b.adjacent_);
}
// Operators
// Define operator== for std::find; And comparisons between nodes
bool operator==(const Node<T> &b) const { return this->data_ == b.data_;}
// Define an operator!= for comparing nodes for inequality
bool operator!=(const Node<T> &b) const { return this->data_ != b.data_;}
// Accessors
inline T GetData() const { return data_;}
inline std::unordered_map<int, int> GetAdjacent() const { return adjacent_;}
private:
T data_;
// Adjacent stored in an unordered_map<adj.number, edgeWeight>
std::unordered_map<T, int> adjacent_;
};
/******************************************************************************/ /******************************************************************************/
// Base struct for storing traversal information on all nodes // Base struct for storing traversal information on all nodes
template <typename T> struct Node;
// Color represents the discovery status of any given node // Color represents the discovery status of any given node
// + White is undiscovered, Gray is in progress, Black is fully discovered enum Color {
enum Color {White, Gray, Black}; // Node is marked as undiscovered
White,
// Node discovery is in progress; Some adjacent nodes have not been checked
Gray,
// Node has been discovered; All adjacent nodes have been checked
Black
};
// Information used in all searches // Information used in all searches
struct SearchInfo { struct SearchInfo {
@ -37,7 +87,7 @@ struct SearchInfo {
/******************************************************************************/ /******************************************************************************/
// BFS search information structs // BFS search information struct
// Information that is only used in BFS // Information that is only used in BFS
template <typename T> template <typename T>
@ -49,9 +99,13 @@ struct BFS : SearchInfo {
const Node<T> *predecessor = nullptr; const Node<T> *predecessor = nullptr;
}; };
// Store search information in unordered_maps so we can pass it around easily
// + Allows each node to store relative information on the traversal
template <typename T> using InfoBFS = std::unordered_map<T, struct BFS<T>>;
/******************************************************************************/ /******************************************************************************/
// DFS search information structs // DFS search information struct
// Information that is only used in DFS // Information that is only used in DFS
struct DFS : SearchInfo { struct DFS : SearchInfo {
@ -62,21 +116,15 @@ struct DFS : SearchInfo {
std::pair<int, int> discoveryFinish; std::pair<int, int> discoveryFinish;
}; };
template <typename T> using InfoDFS = std::unordered_map<T, struct DFS>;
/******************************************************************************/ /******************************************************************************/
// Alias types for storing search information structures // MST search information struct
// Store search information in unordered_maps so we can pass it around easily
// + Allows each node to store relative information on the traversal
template <typename T> using InfoBFS = std::unordered_map<T, struct BFS<T>>;
template <typename T> using InfoDFS = std::unordered_map<T, struct DFS>;
// Edges stored as multimap<weight, pair<nodeA.data_, nodeB.data_>> // Edges stored as multimap<weight, pair<nodeA.data_, nodeB.data_>>
template <typename T> using Edges = std::multimap<int, std::pair<T, T>>; template <typename T> using Edges = std::multimap<int, std::pair<T, T>>;
/******************************************************************************/
// MST search information structs
struct MST : SearchInfo { struct MST : SearchInfo {
int32_t parent = INT32_MIN; int32_t parent = INT32_MIN;
int rank = 0; int rank = 0;
@ -86,7 +134,8 @@ template <typename T>
struct InfoMST { struct InfoMST {
template <typename> friend class Graph; template <typename> friend class Graph;
explicit InfoMST(const std::vector<Node<T>> &nodes) { explicit InfoMST(const std::vector<Node<T>> &nodes)
{
for (const auto &node : nodes) { for (const auto &node : nodes) {
// Initialize the default values for forest tracked by this struct // Initialize the default values for forest tracked by this struct
// + This data is used in KruskalMST() to find the MST // + This data is used in KruskalMST() to find the MST
@ -157,51 +206,6 @@ private:
}; };
/******************************************************************************/
// Node structure for representing a graph
template <typename T>
struct Node {
public:
template <typename> friend class Graph;
template <typename> friend class InfoMST;
// Constructors
Node(const Node &rhs) = default;
Node & operator=(Node rhs) {
if (this == &rhs) return *this;
swap(*this, rhs);
return *this;
}
Node(T data, const std::vector<std::pair<T, int>> &adj)
: data_(data)
{
// Place each adjacent node in vector into our unordered_map of edges
for (const auto &i : adj) adjacent_.emplace(i.first, i.second);
}
friend void swap(Node &a, Node &b) {
std::swap(a.data_, b.data_);
std::swap(a.adjacent_, b.adjacent_);
}
// Operators
// Define operator== for std::find; And comparisons between nodes
bool operator==(const Node<T> &b) const { return this->data_ == b.data_;}
// Define an operator!= for comparing nodes for inequality
bool operator!=(const Node<T> &b) const { return this->data_ != b.data_;}
// Accessors
inline T GetData() const { return data_;}
inline std::unordered_map<int, int> GetAdjacent() const { return adjacent_;}
private:
T data_;
// Adjacent stored in an unordered_map<adj.number, edgeWeight>
std::unordered_map<T, int> adjacent_;
};
/******************************************************************************/ /******************************************************************************/
// Templated graph class // Templated graph class
@ -209,7 +213,7 @@ template <class T>
class Graph { class Graph {
public: public:
// Constructor // Constructor
Graph(std::vector<Node<T>> nodes) : nodes_(std::move(nodes)) {} explicit Graph(std::vector<Node<T>> nodes) : nodes_(std::move(nodes)) {}
// Breadth First Search // Breadth First Search
InfoBFS<T> BFS(const Node<T>& startNode) const; InfoBFS<T> BFS(const Node<T>& startNode) const;

View File

@ -1,8 +1,7 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of a weighted graph implementation ## ## About: Driver program to test weighted graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
################################################################################ ################################################################################

View File

@ -1,7 +1,8 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Driver program to test object graph implementation ## ## About: An example of a weighted graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
################################################################################ ################################################################################
@ -212,4 +213,3 @@ InfoMST Graph::KruskalMST() const
return searchInfo; return searchInfo;
} }

View File

@ -1,7 +1,7 @@
/*############################################################################## /*##############################################################################
## Author: Shaun Reed ## ## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## ## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: An example of an object graph implementation ## ## About: An example of a weighted graph implementation ##
## Algorithms in this example are found in MIT Intro to Algorithms ## ## Algorithms in this example are found in MIT Intro to Algorithms ##
## ## ## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
@ -10,56 +10,14 @@
#ifndef LIB_GRAPH_HPP #ifndef LIB_GRAPH_HPP
#define LIB_GRAPH_HPP #define LIB_GRAPH_HPP
#include <iostream>
#include <algorithm> #include <algorithm>
#include <iostream>
#include <map> #include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <queue>
#include <unordered_set>
#include <unordered_map>
/******************************************************************************/
// Structures for tracking information gathered from various traversals
struct Node;
// Color represents the discovery status of any given node
// + White is undiscovered, Gray is in progress, Black is fully discovered
enum Color {White, Gray, Black};
// Information used in all searches
struct SearchInfo {
// Coloring of the nodes is used in both DFS and BFS
Color discovered = White;
};
// Information that is only used in BFS
struct BFS : SearchInfo {
// Used to represent distance from start node
int distance = 0;
// Used to represent the parent node that discovered this node
// + If we use this node as the starting point, this will remain a nullptr
const Node *predecessor = nullptr;
};
// Information that is only used in DFS
struct DFS : SearchInfo {
// Create a pair to track discovery / finish time
// + 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
std::pair<int, int> discoveryFinish;
};
struct MST : SearchInfo {
int32_t parent = INT32_MIN;
int rank = 0;
};
// Store search information in unordered_maps so we can pass it around easily
// + Allows each node to store relative information on the traversal
using InfoBFS = std::unordered_map<int, struct BFS>;
using InfoDFS = std::unordered_map<int, struct DFS>;
/******************************************************************************/ /******************************************************************************/
@ -69,7 +27,8 @@ struct Node {
public: public:
// Constructors // Constructors
Node(const Node &rhs) = default; Node(const Node &rhs) = default;
Node & operator=(Node rhs) { Node & operator=(Node rhs)
{
if (this == &rhs) return *this; if (this == &rhs) return *this;
swap(*this, rhs); swap(*this, rhs);
return *this; return *this;
@ -80,7 +39,8 @@ public:
for (const auto &i : adj) adjacent.emplace(i.first, i.second); for (const auto &i : adj) adjacent.emplace(i.first, i.second);
} }
friend void swap(Node &a, Node &b) { friend void swap(Node &a, Node &b)
{
std::swap(a.number, b.number); std::swap(a.number, b.number);
std::swap(a.adjacent, b.adjacent); std::swap(a.adjacent, b.adjacent);
} }
@ -95,9 +55,70 @@ public:
bool operator!=(const Node &b) const { return this->number != b.number;} bool operator!=(const Node &b) const { return this->number != b.number;}
}; };
/******************************************************************************/
// Base struct for storing traversal information on all nodes
// Color represents the discovery status of any given node
enum Color {
// Node is marked as undiscovered
White,
// Node discovery is in progress; Some adjacent nodes have not been checked
Gray,
// Node has been discovered; All adjacent nodes have been checked
Black
};
// Information used in all searches
struct SearchInfo {
// Coloring of the nodes is used in both DFS and BFS
Color discovered = White;
};
/******************************************************************************/
// BFS search information struct
// Information that is only used in BFS
struct BFS : SearchInfo {
// Used to represent distance from start node
int distance = 0;
// Used to represent the parent node that discovered this node
// + If we use this node as the starting point, this will remain a nullptr
const Node *predecessor = nullptr;
};
// Store search information in unordered_maps so we can pass it around easily
// + Allows each node to store relative information on the traversal
using InfoBFS = std::unordered_map<int, struct BFS>;
/******************************************************************************/
// DFS search information struct
// Information that is only used in DFS
struct DFS : SearchInfo {
// Create a pair to track discovery / finish time
// + 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
std::pair<int, int> discoveryFinish;
};
/******************************************************************************/
// MST search information struct
struct MST : SearchInfo {
int32_t parent = INT32_MIN;
int rank = 0;
};
using InfoDFS = std::unordered_map<int, struct DFS>;
using Edges = std::multimap<int, std::pair<int, int>>; using Edges = std::multimap<int, std::pair<int, int>>;
struct InfoMST { struct InfoMST {
explicit InfoMST(const std::vector<Node> &nodes) { explicit InfoMST(const std::vector<Node> &nodes)
{
for (const auto &node : nodes) { for (const auto &node : nodes) {
// Initialize the default values for forest tracked by this struct // Initialize the default values for forest tracked by this struct
// + This data is used in KruskalMST() to find the MST // + This data is used in KruskalMST() to find the MST
@ -154,11 +175,12 @@ struct InfoMST {
} }
return searchInfo[x].parent; return searchInfo[x].parent;
} }
}; };
/******************************************************************************/ /******************************************************************************/
// Graph class declaration // Graph class declaration
class Graph { class Graph {
public: public:
// Constructor // Constructor