C++ Program to Perform Edge Coloring of a Graph

In this program, we will perform Edge Coloring of a Graph in which we have to color the edges of the graph that no two adjacent edges have the same color. Steps in Example.

Algorithm

Begin
   Take the input of the number of vertices, n, and then number of edges, e, in the graph.
   The graph is stored as adjacency list.
   BFS is implemented using queue and colors are assigned to each edge.
End

Example

#include
using namespace std;
int n, e, i, j;
vector > > g;
vector color;
bool v[111001];
void col(int n) {
   queue q;
   int c = 0;
   set vertex_colored;
   if(v[n])
      return;
      v[n] = 1;
   for(i = 0;i empty;
   cout>n>>e;
   cout>u>>w;
      u--; w--;
      g[u].push_back(make_pair(w,i));
      g[w].push_back(make_pair(u,i));
   }
   col(0);
   for(i = 0;i

Output

Enter number of vertices and edges respectively:4 5
Enter edge vertices of edge 1 :1 2
Enter edge vertices of edge 2 :2 3
Enter edge vertices of edge 3 :1 1
Enter edge vertices of edge 4 :3 4
Enter edge vertices of edge 5 :1 4
Edge 1 is coloured with colour 1
Edge 2 is coloured with colour 2
Edge 3 is coloured with colour 2
Edge 4 is coloured with colour 1
Edge 5 is coloured with colour 3
Updated on: 2019-07-30T22:30:25+05:30

724 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements