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

C++ प्रोग्राम इनपुट ग्राफ के लाइन ग्राफ में एज कलरिंग करने के लिए

एक अप्रत्यक्ष ग्राफ़ G का लाइन ग्राफ़ एक अन्य ग्राफ़ L(G) है जो G के किनारों के बीच आसन्नता का प्रतिनिधित्व करता है। इस कार्यक्रम में, हम एक इनपुट ग्राफ़ के लाइन ग्राफ़ में एज कलरिंग करते हैं।

एल्गोरिदम

Begin
   Take the input of the number of vertices ‘n’ and number of edges ‘e’.
   Take the input of ‘n’ vertex pairs for the ‘e’ edges in the graph in ed[][].
   Function GenLineGraph():
      Construct a line graph in LineEd[][].
      To construct line graph, for each edge in the given graph, connect it
   to its adjacent edge in LineEd.
   Function EdgeColor():
      Color the graph edges of LineEdge[][] graph.
      Assign color to current edge as col i.e. 1 initially.
      If any of the adjacent edges have the same color, then discard this
   color and go to flag again and try next color.
      Print the color for each edge of the line graph.
End.

उदाहरण कोड

#include<iostream>
using namespace std;
int GenLineGraph(int ed[][2], char LineEd[][3], int e) {
   int i, cnt = 0, j, N;
   char c;
   for(i = 0; i < e; i++) {
      for(j = i+1; j < e; j++) {
         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]) {
            LineEd[cnt][0] = 'a'+i;
            LineEd[cnt][1] = 'a'+j;
            LineEd[cnt][2] = 0;
            cnt++;
         }
      }
   }
   N = cnt;
   cout<<"\n\nThe adjacency list representation for the given graph: ";
   for(i = 0; i < e; i++) {
      cnt = 0;
      c = 'a'+i;
      cout<<"\n\t"<<c<<"-> { ";
      for(j = 0; j < N; j++) {
         if(LineEd[j][0] == i+'a') {
            cout<<LineEd[j][1]<<" ";
            cnt++;
         } else if(LineEd[j][1] == i+'a') {
            cout<<LineEd[j][0]<<" ";
            cnt++;
         } else if(j == e-1 && cnt == 0)
            cout<<"Isolated Vertex!";
      }
      cout<<" }";
   }
   return N;
}
void EdgeColor(char ed[][3], int e) {
   int i, col, j;
   for(i = 0; i < e; i++) {
      col = 1;
      flag:
         ed[i][2] = col;
      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]) {
               col++;
               goto flag;
            }
         }
      }
   }
}
int main() {
   int i, n, e, j, max = -1;
   char c= 'a';
   cout<<"Enter the number of vertices of the graph: ";
   cin>>n;
   cout<<"Enter the number of edges of the graph: ";
   cin>>e;
   int ed[e][2];
   char LineEd[e*e][3];
   for(i = 0; i < e; i++) {
      cout<<"\nEnter the vertex pair for edge '"<<c++<<"'";
      cout<<"\nV(1): ";
      cin>>ed[i][0];
      cout<<"V(2): ";
      cin>>ed[i][1];
   }
   e = GenLineGraph(ed, LineEd, e);
   EdgeColor(LineEd , e);
   for(i = 0; i < e; i++)
      cout<<"\nThe color of the edge between vertex n(1):"<<LineEd[i][0]<<" and n(2):"<<LineEd[i][1]<<" is: color"<<0+LineEd[i][2]<<".";
}

आउटपुट

Enter the number of vertices of the graph:4
Enter the number of edges of the graph: 3
Enter the vertex pair for edge 'a'
V(1): 1
V(2): 2
Enter the vertex pair for edge 'b'
V(1): 3
V(2): 2
Enter the vertex pair for edge 'c'
V(1): 4
V(2): 1
The adjacency list representation for the given graph:
a-> { b c }
b-> { a }
c-> { a }
The color of the edge between vertex n(1):a and n(2):b is: color1.
The color of the edge between vertex n(1):a and n(2):c is: color2.

  1. C++ प्रोग्राम ग्राफ में सुपर वर्टिस का पता लगाने के लिए

    मान लीजिए, हमें एक ग्राफ दिया गया है जिसमें n शीर्ष हैं। कोने 1 से n तक गिने जाते हैं, और वे सरणी किनारों में दिए गए किनारों से जुड़े होते हैं। प्रत्येक शीर्ष का 1 से n तक की संख्या के भीतर x मान होता है जो कि सरणी मान में दिया जाता है। अब, हमें ग्राफ से अति शीर्षों का पता लगाना है। एक शीर्ष i को सु

  1. C++ में एक लाइन के मध्य-बिंदु को खोजने का प्रोग्राम

    इस समस्या में, हमें दो बिंदु A और B दिए गए हैं, जो एक रेखा के आरंभ और अंत बिंदु हैं। हमारा काम C++ में एक लाइन के मध्य-बिंदु को खोजने के लिए एक प्रोग्राम बनाना है। समस्या का विवरण - यहाँ, हमारे पास एक रेखा है जिसमें शुरुआती और अंत बिंदु A(x1, y1) और B(x2, y2) हैं। और हमें रेखा के मध्य-बिंदु को खोजन

  1. C++ प्रोग्राम ग्राफ के एज कवर की गणना करने के लिए

    ग्राफ़ के शीर्षों की संख्या को देखते हुए, कार्य ग्राफ़ के किनारे कवर की गणना करना है। एज कवर ग्राफ़ के प्रत्येक शीर्ष को कवर करने के लिए आवश्यक किनारों की न्यूनतम संख्या ज्ञात करना है। जैसे हमारे पास n =5 . है तो इसका ग्राफ इस तरह होगा - तो इसका किनारा कवर 3 . है आइए एक और उदाहरण लेते हैं जह