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 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...
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 program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
Write a paragraph for each question no less than 4 sentences, and no more than 10...
Write a paragraph for each question no less than 4 sentences, and no more than 10 sentences. Why is responsibility retained by nurses when tasks are delegated? What are the five individual power sources? Provide an example of each. Why is positive reinforcement such a powerful motivator? How does leadership differ from power? Can a leader be effective without power?
[PLEASE USE C++] Write a function to read values of a number of rows, number of...
[PLEASE USE C++] Write a function to read values of a number of rows, number of columns, 2 dimensional (2D) array elements and display the 2D array in a matrix form. Input 2 3 1 4 5 2 3 0 Where, First line of represents the number of rows. Second line of input represents the number of columns. Third line contains array elements of the 1st row and so on. Output 1 4 5 2 3 0 where There must...
Program in C++ **********Write a program to compute the number of collisions required in a long...
Program in C++ **********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT