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...
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 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
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....
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10...
CODE MUST BE IN C++ write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT