Category: Graph Coloring

DSatur Algorithm for Graph Coloring

  #include #include #include #include using namespace std;  struct nodeInfo {    int sat;     int deg;     int vertex; };struct maxSat {    bool operator()(const nodeInfo& lhs,                    const nodeInfo& rhs) const    {                                        return tie(lhs.sat, lhs.deg, lhs.vertex)               > tie(rhs.sat, rhs.deg, rhs.vertex);    }};  class Graph {          int n;          vector adj;  public:        Graph(int numNodes)    {        n = numNodes;        adj.resize(n, vector());    }    ~Graph() { adj.clear(); }          void addEdge(int u, int v);              void DSatur();};  void Graph::addEdge(int u, int v){    adj[u].push_back(v);    adj[v].push_back(u);}  void Graph::DSatur(){    int u, i;    vector used(n, false);    vector c(n), […]

Continue Reading

Minimum time required to color all edges of a Tree

  #include using namespace std;  int ans = 0;  vector edges[100000];  void Add_edge(int u, int v){    edges[u].push_back(v);    edges[v].push_back(u);}  void minTimeToColor(int node, int parent,                    int arrival_time){            int current_time = 0;      for (auto x : edges[node]) {                  if (x != parent) {                                      ++current_time;                                      if (current_time == arrival_time)                ++current_time;                          ans = max(ans, current_time);                                      minTimeToColor(x, node, current_time);        }    }}  int main(){      pair A[] = { { 1, 2 },                            { 2, 3 },                            { 3, 4 } };      for (auto i : […]

Continue Reading