Advertisement
skb50bd

GraphMatrix

Aug 2nd, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main() {
  6.     int v, row, column, c;
  7.     cout << "Number of Vertices: ";
  8.     cin >> v;
  9.  
  10.     int mat[v][v];
  11.     for (int i = 0; i < v; i++)
  12.         for (int j = 0; j < v; j++)
  13.             mat[i][j] = 0;
  14.  
  15.  
  16.     cout << "Enter the Edges" << endl;
  17.     cout << "Enter 0 0 to end" << endl;
  18.     int edge = 1;
  19.     do {
  20.         cout << "Edge " << edge << ": ";
  21.         cin >> row >> column;
  22.         if (row > 0 && row < v && column  > 0 && column < v) {
  23.             mat[row - 1][column - 1] = 1;
  24.             mat[column - 1][row - 1] = 1;
  25.             edge++;
  26.         }
  27.         else if (row != 0 || column != 0)
  28.             cout << "Invalid Input" << endl;
  29.     } while (row != 0 && column != 0);
  30.  
  31.     //printing the matrix
  32.     for (int i = 0; i < v; i++) {
  33.         for (int j = 0; j < v; j++)
  34.             cout << mat[i][j] << " ";
  35.         cout << endl;
  36.     }
  37.     cout << endl;
  38.  
  39.     //ODD degree
  40.     int odds = 0;
  41.     cout << "Vertices with Odd Degree: ";
  42.     for (int i = 0; i < v; i++) {
  43.         c = 0;
  44.         for (int j = 0; j < v; j++)
  45.             if (mat[i][j] != 0)
  46.                 c++;
  47.         if (c % 2 != 0) {
  48.             odds++;
  49.             cout << i + 1 << " ";
  50.         }
  51.     }
  52.     cout << endl;
  53.     cout << "Number of Vertices with Odd Degree: " << odds << endl << endl;
  54.  
  55.     //Degree 3
  56.     int deg3 = 0;
  57.     cout << "Vertices with Degree 3: ";
  58.     for (int i = 0; i < v; i++) {
  59.         c = 0;
  60.         for (int j = 0; j < v; j++)
  61.             if (mat[i][j] != 0)
  62.                 c++;
  63.         if (c == 3) {
  64.             deg3++;
  65.             cout << i + 1 << " ";
  66.         }
  67.     }
  68.     cout << endl;
  69.     cout << "Number of Vertices with Degree 3: " << deg3 << endl << endl;
  70.  
  71.     //Max Degree
  72.     int maX = 0;
  73.     for (int i = 0; i < v; i++) {
  74.         c = 0;
  75.         for (int j = 0; j < v; j++)
  76.             if (mat[i][j] != 0)
  77.                 c++;
  78.         if (c > maX)
  79.             maX = c;
  80.     }
  81.     cout << "Maximum Degree is: " << maX << endl;
  82.     cout << "Vertices with (Maximum) Degree (" << maX << "): ";
  83.     for (int i = 0; i < v; i++) {
  84.         c = 0;
  85.         for (int j = 0; j < v; j++)
  86.             if (mat[i][j] != 0)
  87.                 c++;
  88.         if (c == maX)
  89.             cout << i + 1 << " ";
  90.     }
  91.     cout << endl << endl;
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement