diff --git a/ColorGraphProblem.cpp b/ColorGraphProblem.cpp new file mode 100644 index 0000000..a6df063 --- /dev/null +++ b/ColorGraphProblem.cpp @@ -0,0 +1,56 @@ +//in an undirected graph with maximum degree n, find a graph coloring using at max n+1 colors. +class GraphNode{ +private: + string label_; + unordered_set neighbors_; + string color_; + +public: + GraphNode(const string& label) : + label_(label), + neighbors_(), + color_() + { + } + + const string& getLabel() const{ + return label_; + } + + const unordered_set getNeighbors() const + { + return neighbors_; + } + + void addNeighbor(GraphNode& neighbor){ + neighbors_.insert(&neighbor); + } + + bool hasColor() const{ + return !color_.empty(); + } + + const string& getColor() const{ + if (hasColor()) { + return color_; + } + else { + throw logic_error("GraphNode is not marked with color"); + } + } + + void setColor(const string& color){ + color_ = color; + } +}; + +GraphNode a("a"); +GraphNode b("b"); +GraphNode c("c"); + +a.addNeighbor(b); +b.addNeighbor(a); +b.addNeighbor(c); +c.addNeighbor(b); + +vector graph { &a, &b, &c };