
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Perform Edge Coloring on Complete Graph
A complete graph is a graph which has a connecting edge between any pair of vertices. This is a C++ Program to Perform Edge Coloring on Complete Graph.
Algorithm
Begin Take the input of the number of vertices ‘n’. Construct a complete graph using e=n*(n-1)/2 edges, in ed[][]. Function EdgeColor() is used to Color the graph edges. A) Assign color to current edge as c i.e. 1 initially. B) If the same color is occupied by any of the adjacent edges, then discard this color and go to flag again and try next color. C) Print the color for each edge. End
Example
#include<iostream> using namespace std; void EdgeColor(int ed[][3], int e) { int i, c, j; for(i = 0; i < e; i++) { c = 1; //assign color to current edge as c i.e. 1 initially. flag: ed[i][2] = c; //If the same color is occupied by any of the adjacent edges, then discard this color and go to flag again and try next color. for(j = 0; j < e; j++) { if(j == i) continue; if(ed[j][0] == ed[i][0] || ed[j][0] == ed[i][1] || ed[j][1] == ed[i][0] || ed[j][1] == ed[i][1]) { if(ed[j][2] == ed[i][2]) { c++; goto flag; } } } } } int main() { int i, n, e, j, cnt = 0; cout<<"Enter the number of vertexes for the complete graph: "; cin>>n; e = (n*(n-1))/2; int ed[e][3]; for(i = 1; i <= n; i++) { for(j = i+1; j <= n; j++) { ed[cnt][0] = i; ed[cnt][1] = j; ed[cnt][2] = -1; cnt++; } } EdgeColor(ed , e); for(i = 0; i < e; i++) cout<<"\nThe color of the edge between vertex n1):"<<ed[i][0]<<" and n(2):"<<ed[i][1]<<" is: color"<<ed[i][2]<<"."; }
Output
Enter the number of vertexes for the complete graph: 4 The color of the edge between vertex n(1):1 and n(2):2 is: color1. The color of the edge between vertex n(1):1 and n(2):3 is: color2. The color of the edge between vertex n(1):1 and n(2):4 is: color3. The color of the edge between vertex n(1):2 and n(2):3 is: color3. The color of the edge between vertex n(1):2 and n(2):4 is: color2. The color of the edge between vertex n(1):3 and n(2):4 is: color1.
- Related Articles
- C++ Program to Perform Edge Coloring of a Graph
- C++ Program to Perform Edge Coloring to the Line Graph of an Input Graph
- C++ Program to Perform Graph Coloring on Bipartite Graphs
- C++ Program to Perform Greedy Coloring
- Graph Coloring
- Coloring Graph
- The Graph Coloring
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++
- C++ Program to Find the Edge Connectivity of a Graph
- C++ program to Calculate the Edge Cover of a Graph
- C++ Program to Create a Random Graph Using Random Edge Generation
- C++ Program to Find a Good Feedback Edge Set in a Graph
- C++ Program to Construct a Random Graph by the Method of Random Edge Selection
- C++ program to find winner of cell coloring game
- C program to perform union operation on two arrays

Advertisements