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

特定の固定次数シーケンスのグラフを生成するC++プログラム


これはC++プログラムであり、特定の次数シーケンスの無向グラフを表します。このアルゴリズムの時間計算量はO(v * v)であり、自己エッジと多重エッジは含まれていません。

アルゴリズム

Begin
   To create the graph,
   Create the first loop to connect each vertex ‘i’.
   Create second nested loop to connect the vertex ‘i’ to every valid vertex ‘j’, next to it.
   If the degree of vertex ‘i’ and ‘j’ are more than zero, then connect them.
   Print the adjacency matrix using PrintMatrix().
End
を使用して隣接行列を印刷します

サンプルコード

#include<iostream>
#include<iomanip>
using namespace std;
void PrintMatrix(int matrix[][20], int n) {
   int i, j;
   cout<<"\n\n"<<setw(3)<<" ";
   for(i = 0; i < n; i++)
   cout<<setw(3)<<"("<<i+1<<")";
   cout<<"\n\n";
   for(i = 0; i < n; i++) {
      cout<<setw(4)<<"("<<i+1<<")";
      for(j = 0; j < n; j++) {
         cout<<setw(5)<<matrix[i][j];
      }
      cout<<"\n\n";
   }
}
int main() {
   int N, i, j, AdjMat[20][20] = {0};
   cout<<"Enter the number of vertex in the graph: ";
   cin>>N;
   int degseq[N];
   for(i = 0; i < N; i++) {
      cout<<"Enter the degree of "<<i+1<<" vertex: ";
     cin>>degseq[i];
   }
   for(i = 0; i < N; i++) {
      for(j = i+1; j < N; j++) {
         (degseq[i] > 0 && degseq[j] > 0) {
            degseq[i]--;
            degseq[j]--;
            AdjMat[i][j] = 1;
            AdjMat[j][i] = 1;
         }
      }
   }
   PrintMatrix(AdjMat, N);
}

出力

Enter the number of vertexes of the graph: 5
Enter the number of edges of the graph: 4
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): 1
V(2): 1
Enter the vertex pair for edge 4
V(1): 3
V(2): 1
(1) (2) (3) (4) (5)
(1) 0 1 1 1 1
(2) 1 0 0 1 0
(3) 1 0 0 0 0
(4) 1 1 0 0 1
(5) 1 0 0 1 0

  1. C++でのピラミッドのボリュームのプログラム

    ピラミッドのベースのタイプに応じて側面が与えられると、タスクはピラミッドの体積を計算することです。 ピラミッドは、ピラミッドの鋭いエッジを形成する共通点で外面が三角形で交わる3D図形です。ピラミッドの体積は、持つベースのタイプによって異なります。 -のように、ピラミッドを構成できるベースにはさまざまな種類があります。 三角形 -ピラミッドの体積よりも、ピラミッドの底辺が三角形になることを意味します 式-:( 1/6)* a * b * h 正方形 -ピラミッドの体積よりも、ピラミッドの底面が正方形になることを意味します 式-:(1/3)*(b ^ 2)* h 五角形 -ピラミッド

  2. QuickSort用のC++プログラム?

    クイックソートは、比較を使用してソートされていないリスト(配列)をソートするソート手法です。クイックソートは、パーティション交換ソートとも呼ばれます。 等しいソート項目の相対的な順序が保持されないため、安定したソートではありません。クイックソートは配列を操作できるため、ソートを実行するために少量の追加メモリが必要です。常に最悪の場合のパーティションを選択するわけではないことを除いて、選択ソートと非常によく似ています。したがって、選択ソートのより適切な形式と見なすことができます。 QuickSortは、最も効率的な並べ替えアルゴリズムの1つであり、配列を小さい配列に分割することに基づいていま