Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
#includeusing 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
Advertisements
