Question

In: Computer Science

C++ The program you write for this lab will read in the number of nodes and...

C++

The program you write for this lab will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following :

1. The adjacency matrix
2. Determine if there are any isolated nodes and print them
3. Determine if an Euler path exists and said so.

The sample run of the program is as follows. The output should just like this: It starts by asking the user to input the number of nodes, then ask the user to input the adjacency relation and then print out the adjacency matrix first, followed by the node that is isolated and then tells the user if an Euler path exist or not.

This is how the program should run and output

Please input the number of nodes:
6
Please input the adjacency relation:
{(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}

The Adjacency matrix is:
0 1 0 0 1 0
1 0 1 0 0 0
0 1 0 1 0 0
0 0 1 0 1 0
1 0 0 1 0 0
0 0 0 0 0 0


6 is an isolated node
An Euler path does exist in the graph.

Solutions

Expert Solution

Solution :

Following is the C++ code with comments :

#include<bits/stdc++.h>
using namespace std;
int graph[100][100]; //the adjacency matrix initially 0
int count = 0,nodes;
vector<int> edgenodes;
vector<int> isolatednodes;
void extractIntegerWords(string s) 
{ 
    for(int i=0;i<s.length();++i)
    if(s[i]>'0' && s[i]<='9')
    edgenodes.push_back(s[i]-'0');
} 
void displayMatrix(int v) {
   int i, j;
   for(i = 0; i < v; i++) {
       int sum=0;
      for(j = 0; j < v; j++) {
         cout << graph[i][j] << " ";
         if(graph[i][j])
         sum++;
      }
      if(!sum)
      isolatednodes.push_back(i+1);
      cout << endl;
   }
}
void add_edge(int u, int v) {       //function to add edge into the matrix
   graph[u-1][v-1] = 1;
   graph[v-1][u-1] = 1;
}
void traverse(int u, bool visited[]) {
   visited[u] = true; //mark v as visited
   for(int v = 0; v<nodes; v++) {
      if(graph[u][v]) {
         if(!visited[v])
            traverse(v, visited);
      }
   }
}
bool isConnected() {
   bool *vis = new bool[nodes];
   //for all vertex u as start point, check whether all nodes are visible or not
   for(int u; u < nodes; u++) {
      for(int i = 0; i<nodes; i++)
         vis[i] = false; //initialize as no node is visited
         traverse(u, vis);
         for(int i = 0; i<nodes; i++){
            if(!vis[i]) //if there is a node, not visited by traversal, graph is not connected
               return false;
         }
   }
   return true;
}
//Check if given graph contains an Eulerian path or not
bool isEulerian() {
   if(isConnected() == false) //when graph is not connected
   return 0;
   vector<int> degree(nodes, 0);
   int oddDegree = 0;
   for(int i = 0; i<nodes; i++) {
      for(int j = 0; j<nodes; j++) {
         if(graph[i][j])
            degree[i]++; //increase degree, when connected edge found
      }
      if(degree[i] % 2 != 0) //when degree of vertices are odd
         oddDegree++; //count odd degree vertices
   }
   if(oddDegree > 2) //when vertices with odd degree greater than 2
      return 0;
   return 1; //when oddDegree is 0, it is Euler circuit, and when 2, it is Euler path
}
int main() {
   int nodes;
   cout<<"Please input the number of nodes : "<<endl;
   cin>>nodes;
   cout<<"Please input the adjacency relation : "<<endl;
   string s;
   cin.ignore();
   getline(cin,s);
   extractIntegerWords(s);
   for(int i=0;i<edgenodes.size();i+=2)
   add_edge(edgenodes[i],edgenodes[i+1]);
   cout<<"\nThe adjacency matrix is : \n";
   displayMatrix(nodes);
   cout<<"\nIsolated nodes are : ";
   for(auto i : isolatednodes)
   cout<<i<<" ";
   if(isEulerian())
   cout<<"\nAn Euler path does exist in the graph.";
   else cout<<"\nAn Euler path does not exist in the graph.";   
}

Code demo for reference :


Related Solutions

Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run output Please input the number of nodes: 6 Please input the adjacency relation: {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}...
Write a C++ program that will read in the number of nodes (less than 10) and...
Write a C++ program that will read in the number of nodes (less than 10) and a adjacency relation representing a graph. The program will create an adjacency matrix from the adjacency relation. The program will then print the following items: 1. Print the adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists Sample run (to make program output more clear, I have put it in boldface): Please input...
Write a C++ program will read in the number of nodes and a binary relation representing...
Write a C++ program will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like this: It starts by...
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT