In: Computer Science
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.
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:-->