Advertisement
skb50bd

Eular's Path (Adjacent Matrix)

Dec 11th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. Find If There's An Eular's Path in a Adjacent Matrix of a Graph
  2.  
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     int odd, deg;
  10.     bool ifConnected;
  11.     while(true) {
  12.         int n;
  13.         cout << "Enter the Number of Vertices: ";
  14.         cin.sync();
  15.         cin >> n;
  16.         bool adjMat[50][n];
  17.  
  18.         cout << "Enter the " << n << "x" << n << " Adjacent Matrix: ";
  19.  
  20.         for(int i = 0; i < n; i++)
  21.             for(int j = 0; j < n; j++)
  22.                 cin >> adjMat[i][j];
  23.  
  24.         odd = 0;
  25.         ifConnected = true;
  26.         for(int i = 0; i < n; i++) {
  27.             deg = 0;
  28.             for(int j = 0; j < n; j++)
  29.                 if(adjMat[i][j])
  30.                     deg++;
  31.             if(deg == 0) {
  32.                 ifConnected = false;
  33.                 break;
  34.             }
  35.             else if(deg % 2)
  36.                 odd++;
  37.         }
  38.  
  39.         if(ifConnected) {
  40.             if(odd == 2)
  41.                 cout << "Yes, It has an Eular's Path" << endl << endl;
  42.             else
  43.                 cout << "No, It does not have an Eular's Path" << endl << endl;
  44.         }
  45.  
  46.         else
  47.             cout << "Graph is not connected" << endl << endl;
  48.  
  49.         cout << "Do you want to continue? (y/n): ";
  50.         char ch;
  51.         cin.sync();
  52.         cin >> ch;
  53.         if(ch == 'n' || ch == 'N') break;
  54.         cout << endl << endl;
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement