ビジングの定理を実装するためのC++プログラム
ビジングの定理は、単純なグラフの色指数はmaxdegreeまたはmaxdegree+1のいずれかである可能性があると述べています。ここで、彩色指数とは、グラフのエッジの彩色に必要な最大の色を意味します。
これは、ビジングの定理を実装するためのC++プログラムです。
アルゴリズム
Begin Take the number of vertices and edges as input. Take the vertex pair for the edges. function EdgeColor() : Color the graph edges. 1) Assign color to current edge as c i.e. 1 initially. 2) If the same color is occupied by any of the adjacent edges, then discard this color and go to flag again and try with the next color. 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; //Assign color to current edge 1 initially.
// 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.
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, max = -1;
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][3], deg[n+1] = {0};
for(i = 0; i < e; i++) {
cout<<"\nEnter the vertex pair for edge "<<i+1;
cout<<"\nN(1): ";
cin>>ed[i][0];
cout<<"N(2): ";
cin>>ed[i][1];
//calculate the degree of each vertex
ed[i][2] = -1;
deg[ed[i][0]]++;
deg[ed[i][1]]++;
}
//find out the maximum degree.
for(i = 1; i <= n; i++) {
if(max < deg[i])
max = deg[i];
}
EdgeColor(ed , e);
cout<<"\nAccording to Vizing's theorem this graph can use maximum of "<<max+1<<" colors to generate a valid edge coloring.\n\n";
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]<<".";
} 出力
Enter the number of vertices of the graph: 4 Enter the number of edges of the graph: 3 Enter the vertex pair for edge 1 N(1): 1 N(2): 2 Enter the vertex pair for edge 2 N(1): 3 N(2): 2 Enter the vertex pair for edge 3 N(1): 4 N(2): 1 According to Vizing's theorem this graph can use maximum of 3 colors to generate a valid edge coloring. 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):3 and N(2):2 is: color2. The color of the edge between vertex N(1):4 and N(2):1 is: color2.
-
シーザー暗号を実装するC++プログラム
これは、平文の各文字が別の文字に置き換えられて暗号文を形成するモノアルファベット暗号です。これは、換字式暗号方式の最も単純な形式です。 この暗号システムは、一般にシフト暗号と呼ばれます。コンセプトは、各アルファベットを、0から25の間の固定数で「シフト」された別のアルファベットに置き換えることです。 このタイプのスキームでは、送信者と受信者の両方がアルファベットをシフトするための「秘密のシフト番号」に同意します。この0から25までの数字が暗号化の鍵になります。 「シーザー暗号」という名前は、「3シフト」が使用されている場合のシフト暗号を表すために使用されることがあります。 プロセス
-
AVLツリーを実装するためのC++プログラム
AVLツリーは自己平衡二分探索木であり、左右のサブツリーの高さの差がすべてのノードで複数になることはありません。 ツリーの回転は、AVLツリーの要素の順序を妨げることなく構造を変更する操作です。ツリー内で1つのノードを上に移動し、1つのノードを下に移動します。これは、ツリーの形状を変更したり、小さいサブツリーを下に移動したり、大きいサブツリーを上に移動したりして高さを低くしたりするために使用され、多くのツリー操作のパフォーマンスが向上します。回転の方向は、木のノードが移動する側に依存しますが、他の人は、どの子がルートの場所をとるかに依存すると言います。これは、AVLツリーを実装するためのC+