与えられた数のエッジに対してランダムな無向グラフを生成するC++プログラム
これはC++プログラムであり、指定されたエッジ「e」に対して無向ランダムグラフを生成します。このアルゴリズムは基本的に大規模なネットワークに実装され、このアルゴリズムの時間計算量はO(log(n))です。
アルゴリズム
Begin Function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list. Initialize i = 0 while(i < e) edge[i][0] = rand()%N+1 edge[i][1] = rand()%N+1 Increment I; For i = 0 to N-1 Initialize count = 0 For j = 0 to e-1 if(edge[j][0] == i+1) Print edge[j][1] Increase count else if(edge[j][1] == i+1) Print edge[j][0] Increase count else if(j == e-1 && count == 0) Print Isolated Vertex End
例
#include<iostream>
#include<stdlib.h>
#define N 10
using namespace std;
void GenerateRandomGraphs(int e) {
int i, j, edge[e][2], count;
i = 0;
// generate a connection between two random numbers, for //sample a small case, limit the number of vertex to 10.
while(i < e) {
edge[i][0] = rand()%N+1;
edge[i][1] = rand()%N+1;
i++;
}
//Print all the connection of each vertex, irrespective of the //direction.
cout<<"\nThe generated random graph is: ";
for(i = 0; i < N; i++) {
count = 0;
cout<<"\n\t"<<i+1<<"-> { ";
for(j = 0; j < e; j++) {
if(edge[j][0] == i+1) {
cout<<edge[j][1]<<" ";
count++;
}
else if(edge[j][1] == i+1) {
cout<<edge[j][0]<<" ";
count++;
}
//Print “Isolated vertex” for the vertex having zero degree.
else if(j == e-1 && count == 0)
cout<<"Isolated Vertex!";
}
cout<<" }";
}
}
int main() {
int n, i ,e;
cout<<"Enter the number of edges for the random graphs: ";
cin>>e;
GenerateRandomGraphs(e);
} 出力
Enter the number of edges for the random graphs: 10
The generated random graph is:
1-> { 10 7 }
2-> { 10 }
3-> { 7 8 7 }
4-> { 7 6 7 }
5-> { Isolated Vertex! }
6-> { 8 4 }
7-> { 4 3 4 1 3 }
8-> { 6 3 }
9-> { Isolated Vertex! }
10-> { 2 1 } -
C++での10進数から16進数への変換プログラム
10進数を入力として指定すると、タスクは指定された10進数を16進数に変換することです。 コンピューターの16進数は16を底とし、10進数は10を底とし、0〜9の値で表されますが、16進数は0〜15から始まる数字で、10はA、11はB、12はC、 Dとして13、Eとして14、Fとして15。 10進数を16進数に変換するには、指定された手順に従います- まず、指定された数値を変換数値の基本値で除算します。例: 6789を16を底とする16進数に変換し、商を取得して格納する必要があるため、6789を16で除算します。余りが0〜9の場合はそのまま保存し、余りが10〜15の場合は、文字形式でA-
-
C++での10進数から2進数への変換プログラム
10進数を入力として指定すると、タスクは指定された10進数を2進数に変換することです。 コンピューターの10進数は10進数で表され、2進数は2進数の0と1の2つしかないため、2進数で表されますが、10進数は0〜9から始まる任意の数値にすることができます。 10進数を2進数に変換するには、次の手順に従います- まず、指定された数値を変換数値の基本値で除算します。例: 42を2を底とする2進数に変換し、商を取得して格納する必要があるため、42を2で除算します。余りが0の場合、ビットを0として格納します。それ以外の場合は1です。 取得した商を2進数の基数である2で除算し、ビットを格納し続けます