Question

Simplest way to detect whether a graph is a tree in C++. Reading from text file...

Simplest way to detect whether a graph is a tree in C++. Reading from text file

5
0 1 0 0 0
1 0 1 1 1
0 1 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following is the cpp program for checking whether the given graph is tree or not. I am able to run in my local system successfully. I have attached the screenshot for the same.

If you have any further doubts please feel free to comment. I will try to help you out.

============================================================================

/*
* main.cpp
*
* Created on: 10-Feb-2019
* Author: HOME
*/
#include<iostream>
#include <list>
#include <limits.h>
using namespace std;


class Graph
{
   int V; // No. of vertices
   list<int> *adj;
   bool cyclic(int v, bool visited[], int parent);
public:
   Graph(int V);
   void edgeAdd(int v, int w);
   bool checkTree();
};

Graph::Graph(int V)
{
   this->V = V;
   adj = new list<int>[V];
}

void Graph::edgeAdd(int v, int w)
{
   adj[v].push_back(w);
   adj[w].push_back(v);
}


bool Graph::cyclic(int v, bool visited[], int parent)
{
  
   visited[v] = true;

  
   list<int>::iterator i;
   for (i = adj[v].begin(); i != adj[v].end(); ++i)
   {
      
       if (!visited[*i])
       {
       if (cyclic(*i, visited, v))
           return true;
       }

         
       else if (*i != parent)
       return true;
   }
   return false;
}


bool Graph::checkTree()
{
   bool *visited = new bool[V];
   for (int i = 0; i < V; i++)
       visited[i] = false;

   if (cyclic(0, visited, -1))
           return false;

   for (int u = 0; u < V; u++)
       if (!visited[u])
       return false;

   return true;
}

int main()
{
   Graph graph(5);
   graph.edgeAdd(1, 0);
   graph.edgeAdd(0, 2);
   graph.edgeAdd(0, 3);
   graph.edgeAdd(3, 4);
   graph.checkTree()? cout << "Graph is Tree ":
               cout << "Graph is not Tree ";

   Graph newgraph(5);
   newgraph.edgeAdd(1, 0);
   newgraph.edgeAdd(0, 2);
   newgraph.edgeAdd(2, 1);
   newgraph.edgeAdd(0, 3);
   newgraph.edgeAdd(3, 4);
   newgraph.checkTree()? cout << "Graph is Tree ":
               cout << "Graph is not Tree ";

   return 0;
}
===============================================================

==================================================

Thank you !

Add a comment
Know the answer?
Add Answer to:
Simplest way to detect whether a graph is a tree in C++. Reading from text file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c program that takes a seridalized text file of a tree of up to 32 nodes...

    c program that takes a seridalized text file of a tree of up to 32 nodes and builds its stick diagram as shown. Example 1: Input text file: 10 6 1@5@@8@@ 12 @ 13 @ 20 19 16 @@@@ Output text file: 10 / 12 6 / 1 - 8 13 - - 5 20 19 - 16 Input text file: 10 8 1@7@@@@ 18 11 @ 16 13 @ 14 @@@23 21 @@@ Output text file: 10 8 18...

  • (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file...

    (in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • JAVA DATA STRUCTURES: Reading a Text file of words into two different data structures 1. Use a Binary search tree and then 2.Use a Hash Map. *USE BOTH BINARY & HASH MAP* * Get the file name as a u...

    JAVA DATA STRUCTURES: Reading a Text file of words into two different data structures 1. Use a Binary search tree and then 2.Use a Hash Map. *USE BOTH BINARY & HASH MAP* * Get the file name as a user input.* Present a menu to the user with the below options: 1) Delete the first occurrence of a given word. 2) Delete all the occurrences of a given word.

  • Java Programming Reading from a Text File Write a Java program that will use an object...

    Java Programming Reading from a Text File Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text file payroll.dat has the following: 100 Washington Marx Jung Darwin George 40 200 300 400 Kar Car Charles 50 22.532 15 30 The order of the data and its types stored in the file are as follows: Data EmployeelD Last name FirstName HoursWorked double HourlyRate Data Tvpe String String...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • Hierarchical structures: Write a java class to implement a Multi-way tree, with an appropriate constructor and...

    Hierarchical structures: Write a java class to implement a Multi-way tree, with an appropriate constructor and methods for the following:             Methods to find and insert on the tree.             Method to return the height of the tree. Methods to output the values from this tree in any one of the following ways:             Pre-order:        Left to right order of siblings             Post-order:      Right to left order of siblings             Level order:    Left to right order of siblings             Method to store the tree to a file per the...

  • Given the following character frequencies from two different input text files, will the associated Huffman tree...

    Given the following character frequencies from two different input text files, will the associated Huffman tree encoding have the same structure (ie. will a have the same bit encoding, b have the same bit encoding, etc). Explain, why or why not. File 1 frequencies: a:4, b:1, c:2, d:5 File 2 frequencies: a:5, b:3, c:4, d:6

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT