Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ColorGraphProblem.cpp #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions ColorGraphProblem.cpp
Original file line number Diff line number Diff line change
@@ -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<GraphNode*> neighbors_;
string color_;

public:
GraphNode(const string& label) :
label_(label),
neighbors_(),
color_()
{
}

const string& getLabel() const{
return label_;
}

const unordered_set<GraphNode*> 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<GraphNode*> graph { &a, &b, &c };