Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ प्रोग्राम: साइक्लिक ग्राफ का क्रोमेटिक इंडेक्स खोजें


क्रोमेटिक इंडेक्स दिए गए ग्राफ की एज कलरिंग के लिए आवश्यक रंगों की अधिकतम संख्या है। यह साइक्लिक ग्राफ का क्रोमेटिक इंडेक्स खोजने का C++ प्रोग्राम है।

एल्गोरिदम

Begin    वर्टेक्स की संख्या 'n' और एज की संख्या 'e' का इनपुट लें।    'e' एज के लिए 'e' वर्टेक्स जोड़े का इनपुट edge[][] में लें।    फंक्शन ChromaticIndex(), ग्राफ एज को कलर करें:    A) वर्तमान एज को कलर c असाइन करें।    B) यदि किसी भी आसन्न एज का वही कलर है तो इस कलर को छोड़ दें और फिर से फ्लैग पर जाएं और अगले कलर के साथ प्रयास करें।    C) साइक्लिक ग्राफ का क्रोमेटिक इंडेक्स प्रिंट करें।    प्रत्येक एज का कलर प्रिंट करें। End

उदाहरण

#include<iostream> using namespace std; int ChromaticIndex(int ed[][3], int e) {    int i, c, j, max = -1;    //हर एज 'i' को एक वैध कलर असाइन करने के लिए।    for(i = 0; i < e; i++) {       c = 1;       flag:          //वर्तमान एज को कलर असाइन करें          ed[i][2] = c;          for(j = 0; j < e; j++) {             if(j == i)                continue;                //एज i से आसन्न एज के कलर की जांच करें।                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;                   }                }          }    }    // कलरिंग इंडेक्स खोजें और लौटाएं    for(i = 0; i < e; i++) {       if(max < ed[i][2])          max = ed[i][2];    }    return max; } int main() {    int i, v, e, j, max = -1;    cout<<"Enter the number of vertices of the graph: ";    cin>>v;    cout<<"Enter the number of edges of the graph: ";    cin>>e;    int ed[e][3];    for(i = 0; i < e; i++) {       cout<<"\nEnter the vertex pair for edge "<<i+1;       cout<<"\nV(1): ";       cin>>ed[i][0];       cout<<"V(2): ";       cin>>ed[i][1];       ed[i][2] = -1;    }    cout<<"\n\nThe chromatic index of the given graph is: "<<ChromaticIndex(ed , e);    for(i = 0; i < e; i++)       cout<<"\nThe color of the edge between vertex n(1):"<<ed[i][0]<<" and n(2):"<<ed[i][1]<<" is: color"<<ed[i][2]<<".";       return 0; }

आउटपुट

Enter the number of vertices of the graph:4 Enter the number of edges of the graph: 5 Enter the vertex pair for edge 1 V(1): 2 V(2):1 Enter the vertex pair for edge 2 V(1): 3 V(2): 2 Enter the vertex pair for edge 3 V(1): 3 V(2): 1 Enter the vertex pair for edge 4 V(1): 4 V(2): 2 Enter the vertex pair for edge 5 V(1):1 V(2): 3 The chromatic index of the given graph is: 4 The color of the edge between vertex n(1):2 and n(2):1 is: color1. The color of the edge between vertex n(1):3 and n(2):2 is: color2. The color of the edge between vertex n(1):3 and n(2):1 is: color3. The color of the edge between vertex n(1):4 and n(2):2 is: color3. The color of the edge between vertex n(1):1 and n(2):3 is: color4.

  1. - C++ प्रोग्राम: LCM (लघुत्तम समापवर्त्य) खोजें

    परिचयदो संख्याओं का लघुत्तम समापवर्त्य (LCM) वह सबसे छोटी संख्या है जो दोनों का गुणक है। उदाहरण के लिए: मान लें हमारे पास दो संख्याएं हैं: 15 और 9। 15 = 5 * 3 9 = 3 * 3 इसलिए, 15 और 9 का LCM 45 है। उदाहरण (लूप का उपयोग करके)#include <iostream> using namespace std; int main() { int a = 7,

  1. - C++ प्रोग्राम: GCD (महत्तम समापवर्तक) खोजें

    परिचयदो संख्याओं का महत्तम समापवर्तक (GCD) वह सबसे बड़ी संख्या है जो दोनों को विभाजित करती है। उदाहरण के लिए: मान लें हमारे पास दो संख्याएं हैं 45 और 27। 45 = 5 * 3 * 3 27 = 3 * 3 * 3 इसलिए, 45 और 27 का GCD 9 है। उदाहरण 1 (यूक्लिडियन एल्गोरिदम)#include <iostream> using namespace std; int gcd(

  1. - C++ प्रोग्राम: फैक्टोरियल खोजें

    परिचयएक गैर-ऋणात्मक पूर्णांक n का फैक्टोरियल उन सभी धनात्मक पूर्णांकों का गुणनफल है जो n से कम या बराबर हैं। उदाहरण के लिए: 5 का फैक्टोरियल 120 है। 5! = 5 * 4 * 3 * 2 * 1 5! = 120 एक पूर्णांक का फैक्टोरियल रिकर्सिव प्रोग्राम या नॉन-रिकर्सिव प्रोग्राम का उपयोग करके पाया जा सकता है। दोनों के उदाहरण नी