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)}...
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...
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...
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...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT