All pastes #764809 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-11-07 16:11 UTC
rendered paste body
#include <stack>
#include <iostream>

using namespace std;

void find_tour(stack<unsigned int> & tour, bool** g, unsigned int size, unsigned int u) {
   for (unsigned int v = 0; v < size; v++) {
      if (g[u][v]) {
         g[u][v] = false;
         find_tour(tour, g, size, v);
      }
   }
   tour.push(u);
}

unsigned int get_degree(bool** g, unsigned int size, unsigned int u) {
   unsigned int degree = 0;
   for (unsigned int v = 0; v < size; v++) {
      if (g[u][v]) {
         degree++;
      }
   }
   return degree;
}

int main () {
   unsigned int size = 5;
   bool** g = new bool*[size];
   for (unsigned int i = 0; i < size; i++) {
      g[i] = new bool[size];
      for (unsigned int j = 0; j < size; j++) {
         g[i][j] = true;
      }
   }
   unsigned int u = 0;
   unsigned int degree = get_degree(g, size, u);
   while (degree == 0 && u < size) {
      u++;
      degree = get_degree(g, size, u);
   }
   if (u == size) {
      cerr << "No tour found" << endl;
   }
   else {
      stack<unsigned int> tour;
      find_tour(tour, g, size, u);
      cout << "Found tour (length: " << tour.size() - 1 << "):";
      while (!tour.empty()) {
         cout << ' ' << tour.top();
         tour.pop();
      }
      cout << endl;
   }
   for (unsigned int i = 0; i < size; i++) {
      delete[] g[i];
   }
   delete[] g;
}