プログラミング
 Computer >> コンピューター >  >> プログラミング >> プログラミング

グラフが2部グラフであるかどうかを確認するにはどうすればよいですか?


グラフのすべてのエッジが最初のセットから始まり、で終わるように、グラフの頂点を2つの独立したセットに分割できる場合、グラフは2部グラフと呼ばれます。 2番目のセット、または最初のセットに接続された2番目のセットから開始します。つまり、同じセットにエッジが見つからないと言うことができます。

グラフが2部グラフであるかどうかを確認するにはどうすればよいですか?

頂点カラーリングを使用することにより、2部グラフのチェックが可能です。頂点が同じセットにある場合、同じ色になります。別のセットの場合、色が変わります。

グラフが2部グラフであるかどうかを確認するにはどうすればよいですか?

入力と出力

Input:
The adjacency matrix.
0 1 1 1 0 0
1 0 0 1 1 0
1 0 0 1 0 1
1 1 1 0 1 1
0 1 0 1 0 1
0 0 1 1 1 0

Output:
The graph is bipartite.

アルゴリズム

isBipartite(source)

入力 −ソース頂点。
アウトプ t :グラフが2部グラフの場合はTrue。

Begin
   define an empty queue qu, and a color list coloArray
   initially any node is not colored with any color
   color the source vertex as color red
   add source in the qu
   when qu is not empty, do
      remove item from the qu and take in u
      if there is any self-loop, then
         return false
      for all vertices v, which is connected with u, do
         if v has no color, then
            if colorArray[u] = red, then
               colorArray[v] := blue
            else if colorArray[u] = blue, then
               colorArray[v] := red
            add v into the queue
         if colorArray[v] = colorArray[u], then
            return false
      done
   done
   return true
End   

#include<iostream>
#include<string>
#include<queue>
#define NODE 6
using namespace std;

/*int graph[NODE][NODE] = {
   {0, 1, 1, 1, 0, 0},
   {1, 0, 0, 1, 1, 0},
   {1, 0, 0, 1, 0, 1},
   {1, 1, 1, 0, 1, 1},
   {0, 1, 0, 1, 0, 1},
   {0, 0, 1, 1, 1, 0}
}; */

int graph[NODE][NODE] = {
   {0, 1, 0, 0, 0, 1},
   {1, 0, 1, 0, 0, 0},
   {0, 1, 0, 1, 0, 0},
   {0, 0, 1, 0, 1, 0},
   {0, 0, 0, 1, 0, 1},
   {1, 0, 0, 0, 1, 0}
};

bool isBipartite(int source) {
   queue<int> qu;
   string colorArray[NODE];

   for(int i = 0; i< NODE; i++)
      colorArray[i] = "No Color";    //initially no color is set for all vertices
   colorArray[source] = "Red";    //assign red with the source vertex
   qu.push(source);             //add source into the queue.

   while(!qu.empty()) {
      int u = qu.front();
      qu.pop();
      if(graph[u][u] == 1)    //there is a self loop
         return false;

      for(int v = 0; v < NODE; v++) {
         if(graph[u][v] != 0 && colorArray[v] == "No Color") {
            if(colorArray[u] == "Red")       //assign adjacent list with alternate color
               colorArray[v] = "Blue";
            else if(colorArray[u] == "Blue")
               colorArray[v] = "Red";
            qu.push(v);          //new adjacent node added to queue
         } else if(graph[u][v] != 0 && colorArray[v] == colorArray[u]) {
            return false;       //when u and adjacent are of same color.  
         }
      }
   }
   return true;
}

int main() {
   bool check;
   check = isBipartite(0);

   if(check)
      cout << "The graph is bipartite." << endl;
   else
      cout << "The graph is not bipartite." << endl;
}   

出力

The graph is bipartite.

  1. Windows11のプロダクトキーを見つける方法

    Windows 11のプロダクトキーを探している場合は、安全な場所に保管するだけでも、明確な場所がないことに気付くかもしれません。 Windowsのバージョンをアップグレードしてからしばらく経っている場合は、プロダクトキーがどのように変更されたかに気付かない可能性があります。幸いなことに、Windows11のプロダクトキーをすばやく簡単に見つけることができます。 プロダクトキーを持っていない可能性があります 最初に知っておくべきことは、キーがまったくない可能性があるということです。一部のバージョンのWindows11(さらに言えばWindows 10)は、デジタルライセンスでアクティブ化さ

  2. Excelで検索する方法

    Excelで検索する方法は複数あります。最初のオプションは、大量のデータスプレッドシートがあり、セルまたはセルのグループ内の特定のデータを検索する必要がある場合です。 2番目のオプションセットには、VLOOKUPやHLOOKUPなどの検索機能を使用して、1つのシートでデータを検索し、結果を2番目のセルの場所または別のワークシートに出力できるようにすることが含まれます。 この記事では、Excelで検索するためのすべての可能な方法を学び、状況に適した方法を選択できるようにします。 Excelでの検索の使用 Excelをデータ付きのスプレッドシートで開くと、ストレートワード検索を使用す