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 (to make program output more clear, I have put it in boldface):
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

Assumptions made:

  • The input format for adjacency relation is a string.
  • The nodes are numbered from 1 to n, where n is the number of nodes.

// A C++ program to check if a given graph is Eulerian or not
#include<iostream>
#include <list>
using namespace std;

// A class that represents an undirected graph
class Graph
{
   int V; // No. of vertices
   list<int> *adj; // A dynamic array of adjacency lists
public:
   // Constructor and destructor
   Graph(int V) {this->V = V; adj = new list<int>[V]; }
   ~Graph() { delete [] adj; } // To avoid memory leak

   // function to add an edge to graph
   void addEdge(int v, int w);

   // Method to check if this graph is Eulerian or not
   int isEulerian();

   // Method to check if all non-zero degree vertices are connected
   bool isConnected();

   // Function to do DFS starting from v. Used in isConnected();
   void DFSUtil(int v, bool visited[]);
};

void Graph::addEdge(int v, int w)
{
   adj[v].push_back(w);
   adj[w].push_back(v); // Note: the graph is undirected
}

void Graph::DFSUtil(int v, bool visited[])
{
   // Mark the current node as visited and print it
   visited[v] = true;

   // Recur for all the vertices adjacent to this vertex
   list<int>::iterator i;
   for (i = adj[v].begin(); i != adj[v].end(); ++i)
       if (!visited[*i])
           DFSUtil(*i, visited);
}

// Method to check if all non-zero degree vertices are connected.
// It mainly does DFS traversal starting from
bool Graph::isConnected()
{
   // Mark all the vertices as not visited
   bool visited[V];
   int i;
   for (i = 0; i < V; i++)
       visited[i] = false;

   // Find a vertex with non-zero degree
   for (i = 0; i < V; i++)
       if (adj[i].size() != 0)
           break;

   // If there are no edges in the graph, return true
   if (i == V)
       return true;

   // Start DFS traversal from a vertex with non-zero degree
   DFSUtil(i, visited);

   // Check if all non-zero degree vertices are visited
   for (i = 0; i < V; i++)
   if (visited[i] == false && adj[i].size() > 0)
           return false;

   return true;
}

/* The function returns one of the following values
0 --> If grpah is not Eulerian
1 --> If graph has an Euler path (Semi-Eulerian)
2 --> If graph has an Euler Circuit (Eulerian) */
int Graph::isEulerian()
{
   // Check if all non-zero degree vertices are connected
   if (isConnected() == false)
       return 0;

   // Count vertices with odd degree
   int odd = 0;
   for (int i = 0; i < V; i++)
       if (adj[i].size() & 1)
           odd++;

   // If count is more than 2, then graph is not Eulerian
   if (odd > 2)
       return 0;

   // If odd count is 2, then semi-eulerian.
   // If odd count is 0, then eulerian
   // Note that odd count can never be 1 for undirected graph
   return (odd)? 1 : 2;
}

// Function to run test cases
void test(Graph &g)
{
   int res = g.isEulerian();
   if (res == 0)
       cout << "graph is not Eulerian\n";
   else if (res == 1)
       cout << "graph has a Euler path\n";
   else
       cout << "graph has a Euler cycle\n";
}

// Driver program to test above function
int main()
{
   // Let us create and test graphs shown in above figures
   Graph g1(5);
   g1.addEdge(1, 0);
   g1.addEdge(0, 2);
   g1.addEdge(2, 1);
   g1.addEdge(0, 3);
   g1.addEdge(3, 4);
   test(g1);

   Graph g2(5);
   g2.addEdge(1, 0);
   g2.addEdge(0, 2);
   g2.addEdge(2, 1);
   g2.addEdge(0, 3);
   g2.addEdge(3, 4);
   g2.addEdge(4, 0);
   test(g2);

   Graph g3(5);
   g3.addEdge(1, 0);
   g3.addEdge(0, 2);
   g3.addEdge(2, 1);
   g3.addEdge(0, 3);
   g3.addEdge(3, 4);
   g3.addEdge(1, 3);
   test(g3);

   // Let us create a graph with 3 vertices
   // connected in the form of cycle
   Graph g4(3);
   g4.addEdge(0, 1);
   g4.addEdge(1, 2);
   g4.addEdge(2, 0);
   test(g4);

   // Let us create a graph with all veritces
   // with zero degree
   Graph g5(3);
   test(g5);

   return 0;
}


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