Minimum Colors Required such that Edges Forming Cycle do Not have Same Color


To reduce the number of colours needed and to avoid having the edges form a cycle with the same colour, you can use a chart colouring approach. The goal is to map colours to vertices such that no two adjacent vertices connected by an edge have the same colour. By recognising cycles within the chart, we are able to guarantee that the edges shaping the cycle are allotted diverse colours. This requires navigating the chart using strategies like Depth−First Look (DFS) or Breadth−First Look (BFS) and applying backtracking to backtrack and reassign colours when essential. The objective is to discover the least number of colours required to fulfil the condition and guarantee that no cycles have edges of the same colour.

Methods Used

  • DFS

  • Greedy colouring algorithm

DFS

Depth−first search (DFS) is a map traversal computation that traces backwards by looking at the farthest possible vertices along each parcel. It begins at a chosen vertex and visits its adjoining unvisited vertices, recursively applying the same procedure. DFS utilises a stack to keep track of vertices to visit and a gone−to cluster to check gone−to vertices. This calculation productively navigates through the graph's profundity, permitting applications such as wayfinding, cycle location, and topological sorting. By efficiently investigating the chart, DFS effectively looks for unvisited vertices, making it a principal strategy for understanding and analysing chart structures.

Algorithm

  • Initialise a purge colouring plot for the edges.

  • Make a gone−to cluster to track the visited vertices.

  • For each unvisited vertex v within the graph:

    • Stamp v as visited.

      Relegate the primary accessible colour to the edge interface vs. its parent (in case applicable).

      −Recursively investigate all unvisited neighbours of v.

    • In case a neighbour u has gone by and has the same colour as v's edge, backtrack by changing the colour of v's edge.

    • In the event that the neighbour u isn't gone, allot the primary accessible colour to the edge interfacing v and u.

  • If all edges are effectively coloured without clashes, return the colouring scheme.

  • In case clashes emerge and no colouring conspiracy is conceivable, backtrack by changing the colour of the past edge and proceed with the exploration.

  • Rehash steps 3−5 until a substantial colouring plot is found or all conceivable outcomes are exhausted.

Example

#include <iostream>
#include <vector>
using namespace std;

const int numVertices = 5, numEdges = 7;

// Variable to store the graph
vector<pair<int, int>> adjacencyList[numEdges];

// To store whether a vertex is visited or not
int visited[numVertices];

// Boolean value to store whether the graph contains a cycle or not
bool hasCycle;

// Variable to store the color of the edges in the graph
int edgeColors[numEdges];

// Function to traverse the graph using DFS Traversal
void dfs(int vertex)
{
    visited[vertex] = 1;

    // Loop to iterate through all the edges from the source vertex
    for (auto edge : adjacencyList[vertex]) {
        int adjacentVertex = edge.first, edgeId = edge.second;

        // If the adjacent vertex is not visited
        if (visited[adjacentVertex] == 0) {
            dfs(adjacentVertex);
            edgeColors[edgeId] = 1;
        }

        // Condition to check cross and forward edges of the graph
        else if (visited[adjacentVertex] == 2) {
            edgeColors[edgeId] = 1;
        }

        // Presence of Back Edge
        else {
            edgeColors[edgeId] = 2;
            hasCycle = true;
        }
    }
    visited[vertex] = 2;
}

// Driver Code
int main()
{
    adjacencyList[0].push_back(make_pair(1, 0));
    adjacencyList[0].push_back(make_pair(2, 1));
    adjacencyList[1].push_back(make_pair(2, 2));
    adjacencyList[1].push_back(make_pair(3, 3));
    adjacencyList[2].push_back(make_pair(3, 4));
    adjacencyList[3].push_back(make_pair(4, 5));
    adjacencyList[4].push_back(make_pair(2, 6));

    // Loop to run DFS Traversal on vertices that are not visited
    for (int i = 0; i < numVertices; ++i) {
        if (visited[i] == 0) {
            dfs(i);
        }
    }
    cout << (hasCycle ? 2 : 1) << endl;

    // Loop to print the colors of the edges
    for (int i = 0; i < numEdges; ++i) {
        cout << edgeColors[i] << ' ';
    }
    return 0;
}

Output

2
1 1 1 1 1 1 2 

Greedy colouring Approach

A greedy colouring approach could be a simple computation that colours the vertices of the graph such that no adjacent vertices have the same colour.It begins by assigning the primary accessible colour to the primary vertex and, after that, continues to colour the remaining vertices in a successive arrangement. For each vertex, it chooses the least accessible colour not utilised by its adjoining vertices. This approach ensures substantial vertex colouring but may not continuously create the minimum number of colours required. Ravenous colouring is proficient and broadly utilised for different applications, such as planning, outline colouring, and allocation in compiler optimisation.

Algorithm

  • Initialise a cluster to store the colours doled out to vertices. This cluster will be overhauled as we assign colours to each vertex within the graph.

  • Sort the vertices of the chart in any desired order. Sorting the vertices can offer assistance in deciding the order in which they are handled and can possibly improve the colouring efficiency.

  • For each vertex v within the sorted order:

    • Make a set of utilised colours by its adjoining vertices. This set will store the colours, as of now, doled out to the neighbouring vertices of v.

    • Discover the littlest unused colour from the set of utilised colours. Iterate through the set of used colours and select the littlest colour that's not shown

    • Relegate the littlest unused colour to vertex v. Overhaul the colour cluster with the relegated colour for vertex v.

  • Return the cluster of colours doled out to each vertex. The coming colour cluster will give substantial vertex colouring where no two adjoining vertices share the same colour.

    The Eager Colouring calculation works on the principle of allotting colours to vertices in a ravenous way, based on the accessible colours and the colours utilised by neighbouring vertices. While it may not continuously abdicate the least number of colours required for the chart, it gives a fast and efficient approach for vertex colouring.

Example

#include <iostream>
#include <vector>
using namespace std;

const int numVertices = 5, numEdges = 7;

// Variable to store the graph
vector<vector<int>> adjacencyList(numVertices);

// To store whether a vertex is visited or not
int visited[numVertices];

// Boolean value to store whether the graph contains a cycle or not
bool hasCycle;

// Variable to store the color of the edges in the graph
int edgeColors[numEdges];

// Function to traverse the graph using DFS Traversal
void dfs(int vertex)
{
    visited[vertex] = 1;

    // Loop to iterate through all the edges from the source vertex
    for (int adjacentVertex : adjacencyList[vertex]) {
        // If the adjacent vertex is not visited
        if (visited[adjacentVertex] == 0) {
            dfs(adjacentVertex);
            edgeColors[vertex] = 1;
        }

        // Condition to check cross and forward edges of the graph
        else if (visited[adjacentVertex] == 2) {
            edgeColors[vertex] = 1;
        }

        // Presence of Back Edge
        else {
            edgeColors[vertex] = 2;
            hasCycle = true;
        }
    }
    visited[vertex] = 2;
}

// Driver Code
int main()
{
    adjacencyList[0] = {1, 2};
    adjacencyList[1] = {2, 3};
    adjacencyList[2] = {3};
    adjacencyList[3] = {4};
    adjacencyList[4] = {2};

    // Loop to run DFS Traversal on vertices that are not visited
    for (int i = 0; i < numVertices; ++i) {
        if (visited[i] == 0) {
            dfs(i);
        }
    }
    cout << (hasCycle ? 2 : 1) << endl;

    // Loop to print the colors of the edges
    for (int i = 0; i < numEdges; ++i) {
        cout << edgeColors[i] << ' ';
    }
    return 0;
}

Output

2
1 1 1 1 2 0 0 

Conclusion

This article talks about the issue of minimising the number of colours required in a chart so that edges shaping a cycle don't have the same colour. It investigates two approaches: the Depth−First Look (DFS) with Backtracking calculation and the Covetous Colouring calculation. The DFS approach includes navigating the chart and doling out colours to edges based on their connections with neighbouring vertices. The Ravenous Colouring approach relegates colours to vertices in an eager way, considering the colours utilised by adjoining vertices. Both approaches point to discovering the least number of colours required to fulfil the condition and guaranteeing that no cycles have edges of the same colour.

Updated on: 14-Jul-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements