DSatur Algorithm for Graph Coloring


Introduction

Graph coloring may be an essential issue in graph hypothesis. The DSatur algorithm presents a compelling approach to play down the utilization of colors while performing chart coloring. By deliberately selecting vertices with the most noteworthy immersion degree, DSatur guarantees an optimized color task that maximizes color differing qualities and minimizes color utilization.

In this article, we explore the DSatur calculation for chart coloring and its utilization utilizing C++. The calculation decides its title from the two key concepts it utilizes: Degree and Submersion. It considers the degrees of the vertices and their inundation degrees, which speak to the number of particular colors utilized by their neighbors.

DSatur Algorithm for Graph Coloring

The DSatur Calculation could be a graph coloring strategy utilized in graph hypothesis with critical applications in computer science and optimization. It spins around the assignment of relegating colors to the vertices of a chart, guaranteeing that no two adjoining vertices share the same color. By utilizing a clever strategy, DSatur points to play down the entire number of colors required for coloring the graph successfully. At each step, the calculation chooses the vertex with the most elevated immersion degree (i.e., the number of as-of-now colored adjoining vertices) and allocates it to a color that's not as of now utilized by its neighbors. This approach leads to optimized coloring, maximizing color differing qualities, and minimizing color utilization. The DSatur Calculation offers a proficient and commonsense arrangement to the graph coloring issue, making it a basic device in different computer science and optimization spaces.

The algorithm starts by selecting a vertex with the most elevated degree as the starting vertex and relegates it to the primary color. At that point iteratively chooses the vertex with the most noteworthy immersion degree among the uncolored vertices. In the case of a tie, it chooses the vertex with the most noteworthy degree.

Approach 1: Using an Adjacency List

In this approach, the graph is represented using a contiguousness list. The calculation assigns a starting color to the primary vertex and calculates the saturation degree for each vertex by counting the particular colors utilized by its adjoining vertices. It at that point continues to color the remaining vertices iteratively, selecting the vertex with the highest saturation degree and assigning the smallest accessible color.

Algorithm

  • Step 1 − Make an adjacency list to represent the graph, where each component of the list speaks to a vertex and stores its adjoining vertices.

  • Step 2 − Actualize the DSaturGraphColoring work.

  • Step 3 − Initialize three vectors − colors to store the relegated color for each vertex, immersion to store the immersion degree of each vertex, and accessible to keep track of accessible colors. At first, all colors are set to -1, immersion is set to 0, and all colors are accessible.

  • Step 4 − Dole out a beginning color (0) to the primary vertex and mark it as colored within the colors vector.

  • Step 5 − Compute the immersion degree of each vertex by emphasizing through the contiguous ness list and checking the particular colors utilized by its adjoining vertices. Store the immersion degree within the immersion vector.

  • Step 6 − Print the vertex-color sets from the colors vector.

Example

#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

using namespace std;

class MyNewGraph {
   int numVertices;
   list<int> *adjList;

   public:
      MyNewGraph(int vertices) {
         numVertices = vertices;
         adjList = new list<int>[vertices];
      }

      void addMyEdge(int vertexA, int vertexB) {
         adjList[vertexA].push_back(vertexB);
         adjList[vertexB].push_back(vertexA);
      }

   void performMyDSaturGraphColoring();
};

void MyNewGraph::performMyDSaturGraphColoring() {
   vector<int> vertexColors(numVertices, -1);
   vector<int> saturationDegree(numVertices, 0);
   vector<bool> colorAvailability(numVertices, true);

   vertexColors[0] = 0;

   for (int myIndex = 1; myIndex < numVertices; myIndex++) {
      for (int myVertex : adjList[myIndex]) {
         if (vertexColors[myVertex] != -1)
            saturationDegree[myIndex]++;
      }
   }

   for (int k = 1; k < numVertices; k++) {
      int myMaxSaturation = -1;
      int myVertex = -1;

      for (int myIndex = 0; myIndex < numVertices; myIndex++) {
         if (vertexColors[myIndex] == -1 && saturationDegree[myIndex] > myMaxSaturation) {
            myMaxSaturation = saturationDegree[myIndex];
            myVertex = myIndex;
         }
      }

      int availableColor = 0;
      while (!colorAvailability[availableColor]) {
         availableColor++;
      }
      
      vertexColors[myVertex] = availableColor;
      colorAvailability[availableColor] = false;

      for (int myVertex : adjList[myVertex]) {
         saturationDegree[myVertex]++;
      }
   }

   cout << "MyVertex\tMyColor" << endl;
   for (int myIndex = 0; myIndex < numVertices; myIndex++) {
      cout << myIndex << "\t" << vertexColors[myIndex] << endl;
   }
}
int main() {
   MyNewGraph myNewGraph(4);
   myNewGraph.addMyEdge(0, 1);
   myNewGraph.addMyEdge(0, 2);
   myNewGraph.addMyEdge(0, 3);
   myNewGraph.addMyEdge(2, 3);

   myNewGraph.performMyDSaturGraphColoring();

   return 0;
}

Output

MyVertex	MyColor
0	0
1	0
2	1
3	2

Approach 2: Using a Matrix Representation

In this approach, the graph is represented using a matrix. The calculation allocates a beginning color to the primary vertex and computes the immersion degree for each vertex by checking the distinct colors utilized by its adjoining vertices. It at that point colors the remaining vertices iteratively, selecting the vertex with the highest immersion degree and relegating the smallest accessible color.

Algorithm

  • Step 1 − Make a matrix to represent the graph, where each cell represents an edge between two vertices.

  • Step 2 − Actualize the DSatur Graph Coloring function.

  • Step 3 − Initialize three vectors −colors to store the assigned color for each vertex, immersion to store the immersion degree of each vertex, and accessible to keep track of accessible colors. At first, all colors are set to -1, immersion is set to 0, and all colors are accessible.

  • Step 4 − Assign an introductory color (0) to the primary vertex and mark it as colored within the colors vector.

  • Step 5 − Compute the immersion degree of each vertex by iterating through the matrix and

Example

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class MyNewGraph {
   int myNumVertices;
   vector<vector<int>> myAdjacencyMatrix;

   public:
      MyNewGraph(int vertices) {
         myNumVertices = vertices;
         myAdjacencyMatrix.resize(vertices, vector<int>(vertices, 0));
      }

      void addMyEdge(int myVertex1, int myVertex2) {
         myAdjacencyMatrix[myVertex1][myVertex2] = 1;
         myAdjacencyMatrix[myVertex2][myVertex1] = 1;
      }

   void performMyDSaturGraphColoring();
};

void MyNewGraph::performMyDSaturGraphColoring() {
   vector<int> myVertexColors(myNumVertices, -1);
   vector<int> mySaturationDegree(myNumVertices, 0);
   vector<bool> myColorAvailability(myNumVertices, true);

   myVertexColors[0] = 0;

   for (int myIndex = 1; myIndex < myNumVertices; myIndex++) {
      for (int myVertex : myAdjacencyMatrix[myIndex]) {
         if (myVertexColors[myVertex] != -1)
            mySaturationDegree[myIndex]++;
      }
   }

   for (int k = 1; k < myNumVertices; k++) {
      int myMaxSaturation = -1;
      int myVertex = -1;

      for (int myIndex = 0; myIndex < myNumVertices; myIndex++) {
         if (myVertexColors[myIndex] == -1 && mySaturationDegree[myIndex] > myMaxSaturation) {
            myMaxSaturation = mySaturationDegree[myIndex];
            myVertex = myIndex;
         }
      }

      int myAvailableColor = 0;
      while (!myColorAvailability[myAvailableColor]) {
         myAvailableColor++;
      }

      myVertexColors[myVertex] = myAvailableColor;
      myColorAvailability[myAvailableColor] = false;

      for (int myIndex = 0; myIndex < myNumVertices; myIndex++) {
         if (myAdjacencyMatrix[myVertex][myIndex]) {
            mySaturationDegree[myIndex]++;
         }
      }
   }

   cout << "MyVertex\tMyColor" << endl;
   for (int myIndex = 0; myIndex < myNumVertices; myIndex++) {
      cout << myIndex << "\t" << myVertexColors[myIndex] << endl;
   }
}
int main() {
   MyNewGraph myNewGraph(4);
   myNewGraph.addMyEdge(0, 1);
   myNewGraph.addMyEdge(0, 2);
   myNewGraph.addMyEdge(0, 3);
   myNewGraph.addMyEdge(2, 3);

   myNewGraph.performMyDSaturGraphColoring();

   return 0;
}

Output

MyVertex	MyColor
0	0
1	0
2	1
3	2

Conclusion

The DSatur calculation offers an effective and compelling strategy for graph coloring, taking into consideration vertex degrees and immersion levels. By scholarly people allotting colors to the vertices of the graph, it points to play down the number of colors utilized. Through this article, we investigated the DSatur calculation and gave a step-by-step clarification of its three approaches. We executed Approach 1 utilizing a contiguousness framework representation in C++, guaranteeing reliable coloring comes about. The program code illustrated the viable application of the DSatur calculation in different chart coloring scenarios, exhibiting its capacity to deliver ideal coloring arrangements. By and large, the DSatur calculation stands as an important apparatus in chart hypothesis, supporting optimizing color assignments while lessening color utilization.

Updated on: 25-Aug-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements