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

重み付けされていないグラフでハミルトン閉路を見つけるためのC++プログラム


ハミルトン閉路は、ハミルトン経路の最後の頂点から最初の頂点までのエッジ(グラフ内)があるようなハミルトン経路です。無向グラフにあるのは、グラフの各頂点に1回だけアクセスするパスです。

機能と目的

Begin
   1. function isSafe() is used to check for whether it is
   adjacent to the previously added vertex and already not added.
   2. function hamiltonianCycle() solves the hamiltonian problem.
   3. function hamCycle() uses hamiltonianCycle() to solve
   the hamiltonian problem. It returns false if there is no
   Hamiltonian Cycle possible, otherwise return true and prints the path.
End

#include <iostream>
#include <cstdio>
#include <cstdlib>
#define N 5
using namespace std;
void displaytheSolution(int path[]);
bool isSafe(int n, bool g[N][N], int path[], int pos) {
   if (g [path[pos-1]][n] == 0)
      return false;
   for (int i = 0; i < pos; i++)
      if (path[i] == n)
         return false;
   return true;
}
bool hamiltonianCycle(bool g[N][N], int path[], int pos) {
   //If all vertices are included in Hamiltonian Cycle
   if (pos == N) {
      if (g[ path[pos-1] ][ path[0] ] == 1)
         return true;
      else
         return false;
   }
   for (int n = 1; n < N; n++) {
      if (isSafe(n, g, path, pos)) { //Check if this vertex can be added to Hamiltonian Cycle
         path[pos] = n;
         //recur to construct rest of the path
         if (hamiltonianCycle (g, path, pos+1) == true)
            return true;
         path[pos] = -1; //remove vertex if it doesn’t lead to the solution
      }
   }
   return false;
}
bool hamCycle(bool g[N][N]) {
   int *path = new int[N];
   for (int i = 0; i < N; i++)
   path[i] = -1;
   //put vertex 0 as the first vertex in the path. If there is a Hamiltonian Cycle, then the path can be started from any point
   //of the cycle as the graph is undirected
   path[0] = 0;
   if (hamiltonianCycle(g, path, 1) == false) {
      cout<<"\nCycle does not exist"<<endl;
      return false;
   }
   displaytheSolution(path);
   return true;
}
void displaytheSolution(int p[]) {
   cout<<"Cycle Exists:";
   cout<<" Following is one Hamiltonian Cycle \n"<<endl;
   for (int i = 0; i < N; i++)
   cout<<p[i]<<" ";
   cout<< p[0]<<endl;
}
int main() {
   bool g[N][N] = {{0, 1, 0, 1, 1},
      {0, 0, 1, 1, 0},
      {0, 1, 0, 1, 1},
      {1, 1, 1, 0, 1},
      {0, 1, 1, 0, 0},
   };
   hamCycle(g);
   return 0;
}

出力

Cycle Exists: Following is one Hamiltonian Cycle
0 4 1 2 3 0

  1. 四辺形の4番目の辺を見つけるC++プログラム

    a、b、cの3つの数字があるとします。任意の非縮退の単純な四辺形の形で閉じた柵を作りたい。すでに長さa、b、cの3つの辺があります。別の側面を見つける必要がありますd。 したがって、入力がa=12のような場合。 b =34; c =56の場合、出力は42になり、他の回答も可能です。 ステップ これを解決するには、次の手順に従います- return a + b + c - 2 例 理解を深めるために、次の実装を見てみましょう- #include<bits/stdc++.h> using namespace std; int solve(int a, int b, int c)

  2. グラフ内のスーパー頂点を見つけるためのC++プログラム

    n個の頂点を持つグラフが与えられたとします。頂点には1からnの番号が付けられ、配列edgesで指定されたエッジによって接続されます。各頂点には、配列valuesで指定された1からnまでの数値内のx値があります。ここで、グラフからスーパー頂点を見つける必要があります。頂点1からiへの最短経路にi番目の頂点と同じ「x」値を持つ頂点がない場合、頂点iは「スーパー頂点」と呼ばれます。この基準を満たすすべての頂点を印刷します。 したがって、入力がn =5のようである場合、値={1、2、2、1、3}、エッジ={{1、2}、{2、3}、{2、3}、{2、4 }、{4、5}}の場合、出力は1 345に