#include <iostream>
using namespace std;
int main() {
int v, row, column, c;
cout << "Number of Vertices: ";
cin >> v;
int mat[v][v];
for (int i = 0; i < v; i++)
for (int j = 0; j < v; j++)
mat[i][j] = 0;
cout << "Enter the Edges" << endl;
cout << "Enter 0 0 to end" << endl;
int edge = 1;
do {
cout << "Edge " << edge << ": ";
cin >> row >> column;
if (row > 0 && row < v && column > 0 && column < v) {
mat[row - 1][column - 1] = 1;
mat[column - 1][row - 1] = 1;
edge++;
}
else if (row != 0 || column != 0)
cout << "Invalid Input" << endl;
} while (row != 0 && column != 0);
//printing the matrix
for (int i = 0; i < v; i++) {
for (int j = 0; j < v; j++)
cout << mat[i][j] << " ";
cout << endl;
}
cout << endl;
//ODD degree
int odds = 0;
cout << "Vertices with Odd Degree: ";
for (int i = 0; i < v; i++) {
c = 0;
for (int j = 0; j < v; j++)
if (mat[i][j] != 0)
c++;
if (c % 2 != 0) {
odds++;
cout << i + 1 << " ";
}
}
cout << endl;
cout << "Number of Vertices with Odd Degree: " << odds << endl << endl;
//Degree 3
int deg3 = 0;
cout << "Vertices with Degree 3: ";
for (int i = 0; i < v; i++) {
c = 0;
for (int j = 0; j < v; j++)
if (mat[i][j] != 0)
c++;
if (c == 3) {
deg3++;
cout << i + 1 << " ";
}
}
cout << endl;
cout << "Number of Vertices with Degree 3: " << deg3 << endl << endl;
//Max Degree
int maX = 0;
for (int i = 0; i < v; i++) {
c = 0;
for (int j = 0; j < v; j++)
if (mat[i][j] != 0)
c++;
if (c > maX)
maX = c;
}
cout << "Maximum Degree is: " << maX << endl;
cout << "Vertices with (Maximum) Degree (" << maX << "): ";
for (int i = 0; i < v; i++) {
c = 0;
for (int j = 0; j < v; j++)
if (mat[i][j] != 0)
c++;
if (c == maX)
cout << i + 1 << " ";
}
cout << endl << endl;
return 0;
}
Lab-Practice
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.