Link copied to clipboard. Share away!

Dismiss

Published on 11 Mar 2020

Program to check if the given graph is a tree or not is discussed here.

An undirected graph is given as input,

The given graph is said to be a tree,

- The graph is connected.
- When there are no cycles in the graph.

For example,

The above-given graph is not a tree because it has a cycle.

- Input the graph.
- Check if the graph has any cycles in it.
- If it has a cycle, then print "The given graph is not a tree".
- Else, print"The given graph is a tree".

C++

Output

Input 1 -
Enter the connections of graph 1
1 0 y
0 2 y
0 3 y
3 4 n
Output 1 -
The given graph is a tree
Input 2 -
Enter the connections of graph 2
1 2 y
0 1 y
0 2 y
0 3 y
3 4 n
Output 2 -
The given graph is not a tree

*Recommended Programs*

If you have any feedback about this
article and want to improve this, please write to **enquiry@faceprep.in**

×