貪欲なカラーリングを実行するC++プログラム
これが貪欲なカラーリングを実行するC++プログラムです
アルゴリズム:
Begin Take the number of vertices and edges as input. Create function greedyColoring() to assign color to vertices: A) Assign the first color to first vertex. B) Initialize the remaining vertices. C) Declare a temporary array to store the available colors. D) Assign color to the remaining vertices. Print the solution. End
サンプルコード
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int n,e,i,j;
vector<vector<int> > g;
vector<int> col;
bool visit[1001];
void greedyColoring()
{
col[0] = 0;
for (i=1;i<n;i++)
col[i] = -1;
bool unuse[n];
for (i=0;i<n;i++)
unuse[i]=0;
for (i = 1; i < n; i++)
{
for (j=0;j<g[i].size();j++)
if (col[g[i][j]] != -1)
unuse[col[g[i][j]]] = true;
int cr;
for (cr=0;cr<n;cr++)
if (unuse[cr] == false)
break;
col[i] = cr;
for (j=0;j<g[i].size();j++)
if (col[g[i][j]] != -1)
unuse[col[g[i][j]]] = false;
}
}
int main()
{
int a,b;
cout<<"Enter number of vertices and edges respectively:";
cin>>n>>e;
cout<<"\n";
g.resize(n);
col.resize(n);
memset(visit,0,sizeof(visit));
for(i=0;i<e;i++)
{
cout<<"\nEnter edge vertices of edge "<<i+1<<" :";
cin>>a>>b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
greedyColoring();
for(i=0;i<n;i++)
{
cout<<"Vertex "<<i+1<<" is coloured with "<<col[i]+1<<"\n";
}
} 出力
Enter number of vertices and edges respectively:7 6 Enter edge vertices of edge 1 :4 5 Enter edge vertices of edge 2 :2 3 Enter edge vertices of edge 3 :1 1 Enter edge vertices of edge 4 :1 4 Enter edge vertices of edge 5 :6 7 Enter edge vertices of edge 6 :2 2 Vertex 1 is coloured with 1 Vertex 2 is coloured with 1 Vertex 3 is coloured with 2 Vertex 4 is coloured with 2 Vertex 5 is coloured with 1 Vertex 6 is coloured with 1 Vertex 7 is coloured with 2
-
細胞着色ゲームの勝者を見つけるためのC++プログラム
2つの配列AとBがあり、どちらもそれぞれN個の要素を持っているとします。 AmalとBimalが、セル番号が1からNまでのボードでゲームをプレイしていると考えてください。N-1道路。道路は2つのセルを接続しています。つまり、i番目の道路はA[i]とB[i]を接続しています。隣接するセルに繰り返し移動することにより、他のすべてのセルからすべてのセルに到達できます。最初、セル1は黒色でマークされ、セルNは白色でマークされています。他のセルは着色されていません。アマルが最初にプレイし、代わりにプレイします。 Amalは、黒いセルに隣接し、黒く塗られている色のないセルを選択します。 Bimalは、白い
-
グラフのエッジカバーを計算するC++プログラム
グラフの頂点の数がnの場合、タスクはグラフのエッジカバーを計算することです。エッジカバーは、グラフのすべての頂点をカバーするために必要なエッジの最小数を見つけることです。 n=5のように その場合、そのグラフは次のようになります- したがって、そのエッジカバーは3 nが8である別の例を見てみましょう そして、そのエッジカバーは次のようになります:4 例 Input: n= 5 Output: 3 Input: n= 8 Output: 4 以下で使用されるアプローチは次のとおりです − ユーザーからの入力を受け取ります 頂点の数の結果の上限値を2.0