Question

In: Computer Science

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 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

ALGORITHM

  • As input of adjacency relation is given in the form of string so we can se that there is a pattern in the string to make it as a adjacency matrix.
  • The patter is every 6 position element is our x and 6+2 is our y element and we start loop from 2 example {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)} at position 2 we have x=1 and y will be at positon 4(2+2) so y=2 again adding 6 in the loop next x will be at 8th and y will be at 10th and so on.
  • Then we find indegree of each vertices to find isolated vertices and then we find eulerian path.
#include<bits/stdc++.h>
using namespace std;
void MakeAdj(string s,vector<vector<int>> &mat,int n,int ind[])
{
   for(int i=2;i<s.size()-2;i+=6)
   {
           
           int x=(int)s[i]-'0';
           int y=(int)s[i+2]-'0';
           // for undirected graph
           mat[x-1][y-1]=1;
           mat[y-1][x-1]=1;
           
           // increasing indegree to find isolated vertices
           ind[y]++;
           ind[x]++;
   }
}
bool hasEulerPath(vector<vector<int>> mat,int n)
{
        vector<int> adj; 
  
    for (int i = 0; i < n; i++) 
        adj.push_back(accumulate(mat[i].begin(),mat[i].end(), 0)); 
  
    int start = 0, odd = 0; 
    for (int i = n - 1; i >= 0; i--) 
    { 
        if (adj[i] % 2 == 1) 
        { 
            odd++; 
            start = i; 
        } 
    } 
    if (odd > 2)  
    { 
        return false; 
    } 
    
     
    stack<int> st; 
    vector<int> path; 
    int cur = start; 
  
    while (!st.empty() || accumulate(mat[cur].begin(),mat[cur].end(), 0) != 0) 
    { 
        if (accumulate(mat[cur].begin(), mat[cur].end(), 0) == 0) 
        { 
            path.push_back(cur); 
            cur = st.top(); 
            st.pop(); 
        } 
        else 
        { 
            for (int i = 0; i < n; i++) 
            { 
                if (mat[cur][i] == 1)  
                { 
                    st.push(cur); 
                    mat[cur][i] = 0; 
                    mat[i][cur] = 0; 
                    cur = i; 
                    break; 
                } 
            } 
        } 
    } 
  
    if(path.size()>0)
    return true;
    else
    return false;
}
int main()
{
        int n;
        cout<<"Please input the number of nodes:\n";
        cin>>n;
        cin.ignore();
        string s;
        cout<<"Please input the adjacency relation:\n";
        getline(cin,s);
        int ind[n+1]={0};
        vector<vector<int>> mat(n,vector<int>(n,0));
        
        // String Parsing
        MakeAdj(s,mat,n,ind);
        
        //Adjacency Matrix
        cout<<"The Adjacency matrix is:\n";
        
        for(int i=0;i<n;i++)
        {
                for(int j=0;j<n;j++)
                {
                        cout<<mat[i][j]<<" ";
                }
                cout<<endl;
        }
        
        
        //Isolated node
        for(int i=1;i<=n;i++)
        if(ind[i]==0)
                cout<<i<<" ";
        cout<<"is an isolated node\n";
        
        
        //Euler Path
        int x=hasEulerPath(mat,n);
        if(x==1)
        {
                cout<<"An Euler path does exist in the graph.\n";
        }
        else
        cout<<"An Euler path does not exist in the graph.\n";
}

OUTPUT


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 solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary...
In Java Write a method countOddInternalNodes that returns the number of internal nodes in a binary tree that contain odd numbers. You are essentially writing a method that will become part of the IntegerTree class. You may define private helper methods to solve this problem. Make your own trees in the main method of the code. I specifically kept vague the way the nodes are being added to the tree so you can practice building your own trees from scratch...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
1) Write a method that takes a string representing a positive binary number, and writes out...
1) Write a method that takes a string representing a positive binary number, and writes out the number in hex. (Extra credit opportunity. Write the method so that the input can be a binary number in two's compliment form.) 2) Write a method that takes an integer and writes out its value out in hex. You cannot use string formatting characters. 3) Write an encodeString method that encodes a string of ASCII Characters. Input: It takes two parameters: a) inputString:...
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
Write a c++ program to convert any decimal number to either binary base  or Hex base...
Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings: Convert 15 from decimal to binary.  Convert 255 from decimal to binary. Convert BAC4 from hexadecimal to binary Convert DEED from hexadecimal to binary.  Convert 311 from decimal to hexadecimal. Convert your age to hexadecimal.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT