Question

In: Computer Science

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

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

NOTE: If you are stuck somewhere feel free to ask in the comments ...but do not give negative rating to the question as it affects my answering rights......I will try to help you out...

Also i assume that you know how an adjacency matrix is used to represent edges

1 represents the edge is present and 0 represents not present

I have generalised the step of finding the isolated node ..soo that it can find all the isolated nodes in the matrix

feel free to ask if you dont understand something

#include <bits/stdc++.h>
#include <iostream>
#define MAX 10
using namespace std;

void findpath(int graph[][10], int n) 
{ 
    vector<int> numofadj; 
  
    // Find out number of edges each vertex has 
    for (int i = 0; i < n; i++) 
        numofadj.push_back(accumulate(graph[i],  
                                      graph[i] + 5, 0)); 
  
    // Find out how many vertex has odd number edges 
    int startpoint = 0, numofodd = 0; 
    for (int i = n - 1; i >= 0; i--) 
    { 
        if (numofadj[i] % 2 == 1) 
        { 
            numofodd++; 
            startpoint = i; 
        } 
    } 
  
    // If number of vertex with odd number of edges 
    // is greater than two return "No Solution". 
    if (numofodd > 2)  
    { 
        cout << "No Solution" << endl; 
        return; 
    } 
  
    // If there is a path find the path 
    // Initialize empty stack and path 
    // take the starting current as discussed 
    stack<int> stack; 
    vector<int> path; 
    int cur = startpoint; 
  
    // Loop will run until there is element in the stack 
    // or current edge has some neighbour. 
    while (!stack.empty() or  
            accumulate(graph[cur],  
                       graph[cur] + 5, 0) != 0) 
    { 
        // If current node has not any neighbour 
        // add it to path and pop stack 
        // set new current to the popped element 
        if (accumulate(graph[cur], 
                       graph[cur] + 5, 0) == 0) 
        { 
            path.push_back(cur); 
            cur = stack.top(); 
            stack.pop(); 
        } 
  
        // If the current vertex has at least one 
        // neighbour add the current vertex to stack, 
        // remove the edge between them and set the 
        // current to its neighbour. 
        else 
        { 
            for (int i = 0; i < n; i++) 
            { 
                if (graph[cur][i] == 1)  
                { 
                    stack.push(cur); 
                    graph[cur][i] = 0; 
                    graph[i][cur] = 0; 
                    cur = i; 
                    break; 
                } 
            } 
        } 
    } 
  
    // print the path 
    cout<<"\nAn Euler Path exists" ;
}
int main(){
    int n;
    cout<<"Please input the number of nodes: ";
    cin>>n;
    int adj[10][10];
    memset(adj,0,sizeof(adj));
    cout<<"Enter the adjacency relations : ";
    string str;
    cin.ignore();
    getline(cin,str);
    
    for(int i=0;i<str.size();i++){
       // cout<<str[i]<<" ";
        if(str[i] == '{' || str[i] == '}'||str[i]=='('||str[i]==')'
                ||str[i]==',') continue;
        else{
            int s,d;
           
            s=str[i]-'0';
            
            i+=2;
            d=str[i]-'0';
           // cout<<s<<" "<<d;
            adj[s-1][d-1]=1;
        }
    }
   
    cout<<"\nThe adjacency list is:"<<endl;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout<<adj[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"The isolated nodes are: ";
     for(int i=0;i<n;i++){
         int flag=0;
        for(int j=0;j<n;j++){
            if(adj[i][j] == 1){ flag=1; break;}
        }
        if(flag == 1) continue;
        for(int j=0;j<n;j++){
            if(adj[j][i] == 1) {flag=1; break;}
        }
        if(flag == 0) cout<<i+1<<" ";
    }
    findpath(adj,n);
    return 0;
}

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 (to make program output more clear, I have put it in boldface): Please input...
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...
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...
write a c++ program . Ask the user to enter a number less than 100. Test...
write a c++ program . Ask the user to enter a number less than 100. Test the input to make sure it is correct, and use a while loop to continuously ask the user for correct input value if they don't follow the input rule. After receiving the correct input, test the number to see if it is even or odd. If it is odd, use a while loop to do the following: Output to the monitor all odd numbers...
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
In C++, write a program that will read up to 10 letters (characters) into an array...
In C++, write a program that will read up to 10 letters (characters) into an array and write the letters back to the screen in the reverse order. The program should prompt the user to input all values and can determine whether the input has ended by a punctuation mark such as ‘.’ (make sure that you’ll include meaningful and intuitive messages to the user). For example, if the input is abcde, the output should be edcba. Include appropriate messaging...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
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 read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
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