एक कम्पलीट ग्राफ एक ग्राफ है जिसमें वर्टेक्स के किसी भी जोड़े के बीच एक कनेक्टिंग एज होती है। यह कम्पलीट ग्राफ पर एज कलरिंग करने का C++ प्रोग्राम है।
एल्गोरिदम
Begin वर्टेक्स की संख्या 'n' का इनपुट लें। ed[][] में e=n*(n-1)/2 एज का उपयोग करके एक कम्पलीट ग्राफ बनाएं। फंक्शन EdgeColor() का उपयोग ग्राफ एज को कलर करने के लिए किया जाता है। A) वर्तमान एज को कलर c असाइन करें यानी शुरुआत में 1। B) यदि वही कलर किसी भी आसन्न एज द्वारा कब्जा कर लिया गया है, तो इस कलर को छोड़ दें और फिर से फ्लैग पर जाएं और अगले कलर का प्रयास करें। C) प्रत्येक एज के लिए कलर प्रिंट करें। End
उदाहरण
#include<iostream> using namespace std; void EdgeColor(int ed[][3], int e) { int i, c, j; for(i = 0; i < e; i++) { c = 1; //वर्तमान एज को कलर c असाइन करें यानी शुरुआत में 1। flag: ed[i][2] = c; //यदि वही कलर किसी भी आसन्न एज द्वारा कब्जा कर लिया गया है, तो इस कलर को छोड़ दें और फिर से फ्लैग पर जाएं और अगले कलर का प्रयास करें। 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]<<"."; }
आउटपुट
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.